原文地址:http://www.javashuo.com/article/p-xcgmctpe-dh.htmlhtml
咱們都知道可使用SpringBoot快速的開發基於Spring框架的項目。因爲圍繞SpringBoot存在不少開箱即用的Starter依賴,使得咱們在開發業務代碼時可以很是方便的、不須要過多關注框架的配置,而只須要關注業務便可。java
例如我想要在SpringBoot項目中集成Redis,那麼我只須要加入spring-data-redis-starter的依賴,並簡單配置一下鏈接信息以及Jedis鏈接池配置就能夠。這爲咱們省去了以前不少的配置操做。甚至有些功能的開啓只須要在啓動類或配置類上增長一個註解便可完成。web
那麼若是咱們想要本身實現本身的Starter須要作些什麼呢?下面就開始介紹如何實現本身的SpringBoot-xxx-starter。redis
首先說說原理,咱們知道使用一個公用的starter的時候,只須要將相應的依賴添加的Maven的配置文件當中便可,免去了本身須要引用不少依賴類,而且SpringBoot會自動進行類的自動配置。那麼 SpringBoot 是如何知道要實例化哪些類,並進行自動配置的呢? 下面簡單說一下。spring
第一步,SpringBoot 在啓動時會去依賴的starter包中尋找 resources/META-INF/spring.factories
文件,而後根據文件中配置的類路徑去掃描項目所依賴的Jar包,這相似於 Java 的 SPI 機制。(spi介紹參閱:Java的spi介紹和簡單應用)mongodb
第二步,根據 spring.factories
配置加載AutoConfigure
類。shell
最後,根據 @Conditional
註解的條件,進行自動配置並將Bean注入Spring Context 上下文當中。數據庫
咱們也可使用@ImportAutoConfiguration({MyServiceAutoConfiguration.class})
指定自動配置哪些類。編程
終於到了代碼實現的步驟,接下來就開始編碼咱們本身的SpringBoot-starter。json
<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-autoconfigure</artifactId> </dependency>
其中 spring-boot-configuration-processor
的做用是編譯時生成 spring-configuration-metadata.json
,此文件主要給IDE使用。如當配置此jar相關配置屬性在 application.yml
,你能夠用ctlr+鼠標左鍵點擊屬性名,IDE會跳轉到你配置此屬性的類中。
咱們平常使用的Spring官方的Starter通常採起spring-boot-starter-{name}
的命名方式,如 spring-boot-starter-web
。
而非官方的Starter,官方建議 artifactId
命名應遵循{name}-spring-boot-starter
的格式。 例如:mybatis-spring-boot-starter
。
<dependency> <groupId>com.meng.starter</groupId> <artifactId>demo07-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
這裏講一下咱們的Starter要實現的功能,很簡單,提供一個Service
,包含一個可以將配置文件中配置的字符串根據傳入的字符進行分割的方法String[] split(String separatorChar)
。
public class DemoService { private String str; public DemoService(String str) { this.str = str; } public String[] split(String tag){ return StringUtils.split(str, tag); } }
@ConfigurationProperties("demo.service") public class DemoProperties { private String str; public String getStr() { return str; } public void setStr(String str) { this.str = str; } }
AutoConfiguration
類 ,這步是關鍵點@Configuration @ConditionalOnClass(DemoService.class) @EnableConfigurationProperties(DemoProperties.class) public class DemoAutoConfiguration { @Autowired private DemoProperties demoProperties; @Bean @ConditionalOnMissingBean @ConditionalOnProperty(prefix = "demo.service", value = "enabled", havingValue = "true") DemoService demoService(){ return new DemoService(demoProperties.getStr()); } }
解釋一下代碼中用到的幾個註解:
@ConditionalOnClass
,當classpath
下發現該類的狀況下進行自動配置。@ConditionalOnMissingBean
,當Spring Context
中不存在該Bean
時。@ConditionalOnProperty(prefix = "demo.service",value = "enabled",havingValue = "true")
,當配置文件中example.service.enabled=true
時。@ConditionalOnBean:當容器中有指定的Bean的條件下
@ConditionalOnClass:當類路徑下有指定的類的條件下
@ConditionalOnExpression:基於SpEL表達式做爲判斷條件
@ConditionalOnJava:基於JVM版本做爲判斷條件
@ConditionalOnJndi:在JNDI存在的條件下查找指定的位置
@ConditionalOnMissingBean:當容器中沒有指定Bean的狀況下
@ConditionalOnMissingClass:當類路徑下沒有指定的類的條件下
@ConditionalOnNotWebApplication:當前項目不是Web項目的條件下
@ConditionalOnProperty:指定的屬性是否有指定的值
@ConditionalOnResource:類路徑下是否有指定的資源
@ConditionalOnSingleCandidate:當指定的Bean在容器中只有一個,或者在有多個Bean的狀況下,用來指定首選的Bean @ConditionalOnWebApplication:當前項目是Web項目的條件下
resources/META-INF/
下建立spring.factories
文件,並添加以下內容:# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.meng.starter.DemoAutoConfiguration
至此,咱們的一個Starter代碼部分就是完成了,下面將項目安裝到本地Maven倉庫中。
在項目根目錄執行 mvn install
進行打包安裝。
將Starter項目的依賴添加到咱們本身的SpringBoot項目中
<dependency> <groupId>com.meng.starter</groupId> <artifactId>demo07-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
在application.yml
配置文件中添加配置信息:
demo:
service:
enabled: true
str: Ctrl+Shift+Alt+N,查找類中的方法或變量
測試
@Autowired DemoService demoService; @GetMapping("/demo") public String[] demo(String tag){ return demoService.split(tag); }
Spring Boot應用啓動器基本的一共有44種,具體以下:
1)spring-boot-starter
這是Spring Boot的核心啓動器,包含了自動配置、日誌和YAML。
2)spring-boot-starter-actuator
幫助監控和管理應用。
3)spring-boot-starter-amqp
經過spring-rabbit來支持AMQP協議(Advanced Message Queuing Protocol)。
4)spring-boot-starter-aop
支持面向方面的編程即AOP,包括spring-aop和AspectJ。
5)spring-boot-starter-artemis
經過Apache Artemis支持JMS的API(Java Message Service API)。
6)spring-boot-starter-batch
支持Spring Batch,包括HSQLDB數據庫。
7)spring-boot-starter-cache
支持Spring的Cache抽象。
8)spring-boot-starter-cloud-connectors
支持Spring Cloud Connectors,簡化了在像Cloud Foundry或Heroku這樣的雲平臺上鍊接服務。
9)spring-boot-starter-data-elasticsearch
支持ElasticSearch搜索和分析引擎,包括spring-data-elasticsearch。
10)spring-boot-starter-data-gemfire
支持GemFire分佈式數據存儲,包括spring-data-gemfire。
11)spring-boot-starter-data-jpa
支持JPA(Java Persistence API),包括spring-data-jpa、spring-orm、Hibernate。
12)spring-boot-starter-data-mongodb
支持MongoDB數據,包括spring-data-mongodb。
13)spring-boot-starter-data-rest
經過spring-data-rest-webmvc,支持經過REST暴露Spring Data數據倉庫。
14)spring-boot-starter-data-solr
支持Apache Solr搜索平臺,包括spring-data-solr。
15)spring-boot-starter-freemarker
支持FreeMarker模板引擎。
16)spring-boot-starter-groovy-templates
支持Groovy模板引擎。
17)spring-boot-starter-hateoas
經過spring-hateoas支持基於HATEOAS的RESTful Web服務。
18)spring-boot-starter-hornetq
經過HornetQ支持JMS。
19)spring-boot-starter-integration
支持通用的spring-integration模塊。
20)spring-boot-starter-jdbc
支持JDBC數據庫。
21)spring-boot-starter-jersey
支持Jersey RESTful Web服務框架。
22)spring-boot-starter-jta-atomikos
經過Atomikos支持JTA分佈式事務處理。
23)spring-boot-starter-jta-bitronix
經過Bitronix支持JTA分佈式事務處理。
24)spring-boot-starter-mail
支持javax.mail模塊。
25)spring-boot-starter-mobile
支持spring-mobile。
26)spring-boot-starter-mustache
支持Mustache模板引擎。
27)spring-boot-starter-redis
支持Redis鍵值存儲數據庫,包括spring-redis。
28)spring-boot-starter-security
支持spring-security。
29)spring-boot-starter-social-facebook
支持spring-social-facebook
30)spring-boot-starter-social-linkedin
支持pring-social-linkedin
31)spring-boot-starter-social-twitter
支持pring-social-twitter
32)spring-boot-starter-test
支持常規的測試依賴,包括JUnit、Hamcrest、Mockito以及spring-test模塊。
33)spring-boot-starter-thymeleaf
支持Thymeleaf模板引擎,包括與Spring的集成。
34)spring-boot-starter-velocity
支持Velocity模板引擎。
35)spring-boot-starter-web
S支持全棧式Web開發,包括Tomcat和spring-webmvc。
36)spring-boot-starter-websocket
支持WebSocket開發。
37)spring-boot-starter-ws
支持Spring Web Services。
Spring Boot應用啓動器面向生產環境的還有2種,具體以下:
1)spring-boot-starter-actuator
增長了面向產品上線相關的功能,好比測量和監控。
2)spring-boot-starter-remote-shell
增長了遠程ssh shell的支持。
最後,Spring Boot應用啓動器還有一些替換技術的啓動器,具體以下:
1)spring-boot-starter-jetty
引入了Jetty HTTP引擎(用於替換Tomcat)。
2)spring-boot-starter-log4j
支持Log4J日誌框架。
3)spring-boot-starter-logging
引入了Spring Boot默認的日誌框架Logback。
4)spring-boot-starter-tomcat
引入了Spring Boot默認的HTTP引擎Tomcat。
5)spring-boot-starter-undertow 引入了Undertow HTTP引擎(用於替換Tomcat)。