https://gitee.com/emperors/spring-boot-integration/tree/master/hello-spring-boot-startergit
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <version.lombok>1.16.18</version.lombok> <version.spring-boot>1.5.10.RELEASE</version.spring-boot> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${version.lombok}</version> <scope>provided</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${version.spring-boot}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
注:關於artifactId 官方命名格式爲: spring-boot-starter-{Nameing} 非官方建議命名格式:{Nameing}-spring-boot-starterweb
配置。spring
@Data @ConfigurationProperties("starter") public class HelloStarterProperties { /** * starter 描述 */ private String descr; }
服務。app
/** * <p> 對外服務。</p> * * @author panqingcui <br> * @create 2018-06-28 10:29 <br> */ @Data public class HelloStarterService { private String descr; public String helloStartar(){ return "HELLO "+descr; } }
自動配置類ide
/** * <p> 自動配置。</p> * * @author panqingcui <br> * @create 2018-06-28 10:32 <br> */ @Configuration //標識 當前類是配置類 @EnableConfigurationProperties(value = HelloStarterProperties.class) //啓動配置文件,value用來指定咱們要啓用的配置類,能夠有多個,多個時咱們能夠這麼寫value={xxProperties1.class,xxProperteis2.class....} @ConditionalOnClass(HelloStarterService.class) //該註解的參數對應的類必須存在,不然不解析該註解修飾的配置類 @ConditionalOnProperty(prefix = "starter", value = "enable", matchIfMissing = false) //表示只有咱們的配置文件是否配置了以starter爲前綴的資源項值,而且在該資源項值爲enable,若是沒有配置咱們默認設置爲enable public class HelloStarterServiceAutoConfiguration { @Autowired private HelloStarterProperties helloStarterProperties; @Bean @ConditionalOnMissingBean(HelloStarterService.class)//該註解表示,若是存在它修飾的類的bean,則不須要再建立這個bean public HelloStarterService helloService() { HelloStarterService helloStarterService = new HelloStarterService(); helloStarterService.setDescr(helloStarterProperties.getDescr()); return helloStarterService; } }
自動加載配置spring-boot
resources 下新建META-INF/spring.factoriesui
添加內容:spa
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.pqc.hello.starter.HelloStarterServiceAutoConfiguration
最簡單的starter建立完成。添加依賴。在外部能夠直接引用。code
<dependency> <groupId>com.pqc.test</groupId> <artifactId>hello-spring-boo-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
引用服務ci
@SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } @RestController class AaaController { @Autowired DiscoveryClient discoveryClient; @Autowired HelloStarterService helloStarterService; @GetMapping("/service-b") public String test(){ return helloStarterService.helloStartar(); } @GetMapping("/service-a") public String dc() { String services = "Services: " + discoveryClient.getServices(); System.out.println(services); return services; } } }
下面會研究。starter的註解方式,引用一個註解就能夠搞定。