pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${guava.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
    </dependencies>  
    
    <distributionManagement>
        <repository>
            <id>uniweibo</id>
            <name>uniweibo</name>
            <url>http://10.0.0.40:8081/repository/uniweibo/</url>
        </repository>
    </distributionManagement>    
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>    

上面的, 可根据自己的 starter 的实际依赖来调整.

Java代码

properties 的配置

@ConfigurationProperties(prefix = AppConstant.PROPERTY_PREFIX)
public class WxProperties {
}

@ConfigurationProperties 使用指定的前缀作为你的 starter 服务的配置. 这样子就可以在 application.properties 中, 以 AppConstant.PROPERTY_PREFIX (自定义的一个字符串常量) 作为前缀来配置你的服务.

bean 的配置

/**
 * @author emacsist
 */
@Configuration
@EnableConfigurationProperties(WxProperties.class)
@ConditionalOnClass(WXService.class)
@ConditionalOnProperty(name = AppConstant.PROPERTY_PREFIX + ".enable", havingValue = "true", matchIfMissing = true)
public class WxNoticeAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean(WXService.class)
    public WXService wxService() {
        WXService wxService = new WXService();
        log.info("init {} module ok", WXService.class.getName());
        return wxService;
    }
}

@ConditionalOnClass(WXService.class) 表示存在 WXService.class 时才加载这个配置.

@ConditionalOnProperty(name = AppConstant.PROPERTY_PREFIX + ".enable", havingValue = "true", matchIfMissing = true) 表示配置文件 前缀.enable 的值为 true 时加载, 如果没指定, 则默认为 true.

可以在 application.properties 中配置 AppConstant.PROPERTY_PREFIX.enable=false 来显式禁止自动配置.

配置 META-INF

src/main/resources 目录下, 添加一个 META-INF 目录. 然后在 META-INF 目录下创建 spring.factories 文件. 内容如下:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.uniweibo.wxnotice.autoconfig.WxNoticeAutoConfiguration

修改为你的自动配置的class即可.

打包安装

time mvn -T 1C -U  clean package -Dmaven.test.skip=true install deploy

上面的 deploy 命令一般只在有内网 nexus Maven 仓库才会到. 即上面的 pom 里的 <distributionManagement> 元素的配置.