SpringBoot 自定義啟動(dòng)器

Spring Boot應(yīng)用啟動(dòng)器基本的一共有44種,具體如下:

1)spring-boot-starter
這是Spring Boot的核心啟動(dòng)器,包含了自動(dòng)配置、日志和YAML。

2)spring-boot-starter-actuator
幫助監(jiān)控和管理應(yīng)用。

3)spring-boot-starter-amqp
通過(guò)spring-rabbit來(lái)支持AMQP協(xié)議(Advanced Message Queuing Protocol)。

4)spring-boot-starter-aop
支持面向方面的編程即AOP,包括spring-aop和AspectJ。

5)spring-boot-starter-artemis
通過(guò)Apache Artemis支持JMS的API(Java Message Service API)。

6)spring-boot-starter-batch
支持Spring Batch,包括HSQLDB數(shù)據(jù)庫(kù)。

7)spring-boot-starter-cache
支持Spring的Cache抽象。

8)spring-boot-starter-cloud-connectors
支持Spring Cloud Connectors,簡(jiǎn)化了在像Cloud Foundry或Heroku這樣的云平臺(tái)上連接服務(wù)。

9)spring-boot-starter-data-elasticsearch
支持ElasticSearch搜索和分析引擎,包括spring-data-elasticsearch。

10)spring-boot-starter-data-gemfire
支持GemFire分布式數(shù)據(jù)存儲(chǔ),包括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數(shù)據(jù),包括spring-data-mongodb。

13)spring-boot-starter-data-rest
通過(guò)spring-data-rest-webmvc,支持通過(guò)REST暴露Spring Data數(shù)據(jù)倉(cāng)庫(kù)。

14)spring-boot-starter-data-solr
支持Apache Solr搜索平臺(tái),包括spring-data-solr。

15)spring-boot-starter-freemarker
支持FreeMarker模板引擎。

16)spring-boot-starter-groovy-templates
支持Groovy模板引擎。

17)spring-boot-starter-hateoas
通過(guò)spring-hateoas支持基于HATEOAS的RESTful Web服務(wù)。

18)spring-boot-starter-hornetq
通過(guò)HornetQ支持JMS。

19)spring-boot-starter-integration
支持通用的spring-integration模塊。

20)spring-boot-starter-jdbc
支持JDBC數(shù)據(jù)庫(kù)。

21)spring-boot-starter-jersey
支持Jersey RESTful Web服務(wù)框架。

22)spring-boot-starter-jta-atomikos
通過(guò)Atomikos支持JTA分布式事務(wù)處理。

23)spring-boot-starter-jta-bitronix
通過(guò)Bitronix支持JTA分布式事務(wù)處理。

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鍵值存儲(chǔ)數(shù)據(jù)庫(kù),包括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
支持常規(guī)的測(cè)試依賴,包括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開發(fā),包括Tomcat和spring-webmvc。

36)spring-boot-starter-websocket
支持WebSocket開發(fā)。

37)spring-boot-starter-ws
支持Spring Web Services。

Spring Boot應(yīng)用啟動(dòng)器面向生產(chǎn)環(huán)境的還有2種,具體如下:

1)spring-boot-starter-actuator
增加了面向產(chǎn)品上線相關(guān)的功能,比如測(cè)量和監(jiān)控。

2)spring-boot-starter-remote-shell
增加了遠(yuǎn)程ssh shell的支持。

最后,Spring Boot應(yīng)用啟動(dòng)器還有一些替換技術(shù)的啟動(dòng)器,具體如下:

1)spring-boot-starter-jetty
引入了Jetty HTTP引擎(用于替換Tomcat)。

2)spring-boot-starter-log4j
支持Log4J日志框架。

3)spring-boot-starter-logging
引入了Spring Boot默認(rèn)的日志框架Logback。

4)spring-boot-starter-tomcat
引入了Spring Boot默認(rèn)的HTTP引擎Tomcat。

5)spring-boot-starter-undertow
引入了Undertow HTTP引擎(用于替換Tomcat)。

自定義SpringBoot-Starter

前言

我們都知道可以使用SpringBoot快速的開發(fā)基于Spring框架的項(xiàng)目。由于圍繞SpringBoot存在很多開箱即用的Starter依賴,使得我們?cè)陂_發(fā)業(yè)務(wù)代碼時(shí)能夠非常方便的、不需要過(guò)多關(guān)注框架的配置,而只需要關(guān)注業(yè)務(wù)即可。

例如我想要在SpringBoot項(xiàng)目中集成Redis,那么我只需要加入spring-data-redis-starter的依賴,并簡(jiǎn)單配置一下連接信息以及Jedis連接池配置就可以。這為我們省去了之前很多的配置操作。甚至有些功能的開啟只需要在啟動(dòng)類或配置類上增加一個(gè)注解即可完成。

那么如果我們想要自己實(shí)現(xiàn)自己的Starter需要做些什么呢?下面就開始介紹如何實(shí)現(xiàn)自己的SpringBoot-xxx-starter。

原理

首先說(shuō)說(shuō)原理,我們知道使用一個(gè)公用的starter的時(shí)候,只需要將相應(yīng)的依賴添加的Maven的配置文件當(dāng)中即可,免去了自己需要引用很多依賴類,并且SpringBoot會(huì)自動(dòng)進(jìn)行類的自動(dòng)配置。那么 SpringBoot 是如何知道要實(shí)例化哪些類,并進(jìn)行自動(dòng)配置的呢? 下面簡(jiǎn)單說(shuō)一下。

第一步,SpringBoot 在啟動(dòng)時(shí)會(huì)去依賴的starter包中尋找 resources/META-INF/spring.factories文件,然后根據(jù)文件中配置的Jar包去掃描項(xiàng)目所依賴的Jar包,這類似于 Java 的 SPI 機(jī)制。(spi介紹參閱:Java的spi介紹和簡(jiǎn)單應(yīng)用)

第二步,根據(jù) spring.factories配置加載AutoConfigure類。

最后,根據(jù) @Conditional注解的條件,進(jìn)行自動(dòng)配置并將Bean注入Spring Context 上下文當(dāng)中。

我們也可以使用@ImportAutoConfiguration({MyServiceAutoConfiguration.class}) 指定自動(dòng)配置哪些類。

實(shí)現(xiàn)

終于到了代碼實(shí)現(xiàn)的步驟,接下來(lái)就開始編碼我們自己的SpringBoot-starter。

第一步創(chuàng)建一個(gè)SpringBoot 項(xiàng)目,并添加下面兩個(gè)依賴到pom.xml文件當(dāng)中

<dependencies>    
  <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>
</dependencies>

其中 spring-boot-configuration-processor的作用是編譯時(shí)生成 spring-configuration-metadata.json,此文件主要給IDE使用。如當(dāng)配置此jar相關(guān)配置屬性在 application.yml,你可以用ctlr+鼠標(biāo)左鍵點(diǎn)擊屬性名,IDE會(huì)跳轉(zhuǎn)到你配置此屬性的類中。

我們?nèi)粘J褂玫腟pring官方的Starter一般采取spring-boot-starter-{name}的命名方式,如 spring-boot-starter-web。

而非官方的Starter,官方建議 artifactId 命名應(yīng)遵循{name}-spring-boot-starter的格式。 例如:mybatis-spring-boot-starter 。

<groupId>com.ysc</groupId>
<artifactId>simple-spring-boot-starter</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

第二步編寫我們的Service類

這里講一下我們的Starter要實(shí)現(xiàn)的功能,很簡(jiǎn)單,提供一個(gè)Service,包含一個(gè)能夠?qū)⑴渲梦募信渲玫淖址鶕?jù)傳入的字符進(jìn)行分割的方法String[] split(String separatorChar)

public class StarterService {
  private String config;    
  public StarterService(String config) { 
       this.config = config;    
  }        
  public String[] split(String separatorChar) { 
       return StringUtils.split(this.config, separatorChar);    
  }    
}

第三步編寫配置文件讀取類

@ConfigurationProperties("example.service")
public class StarterServiceProperties {    
  private String config;        
  public void setConfig(String config) {
        this.config = config;    
  }        
  public String getConfig() {
        return config;    
  } 
}

第四步,編寫AutoConfigure類 ,這步是關(guān)鍵點(diǎn)

  @Configuration@ConditionalOnClass(StarterService.class)
  @EnableConfigurationProperties(StarterServiceProperties.class)
  public class StarterAutoConfigure {
    @Autowired    
    private StarterServiceProperties properties;     
    @Bean    
    @ConditionalOnMissingBean    
    @ConditionalOnProperty(prefix = "example.service", value = "enabled", havingValue = "true")    
    StarterService starterService (){
        return new StarterService(properties.getConfig());    
    }
 }

解釋一下代碼中用到的幾個(gè)注解:

   `@ConditionalOnClass`,當(dāng)`classpath`下發(fā)現(xiàn)該類的情況下進(jìn)行自動(dòng)配置。
   `@ConditionalOnMissingBean`,當(dāng)`Spring Context`中不存在該`Bean`時(shí)。
   `@ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true")`,
    當(dāng)配置文件中`example.service.enabled=true`時(shí)。

下面列舉SpringBoot中的所有@Conditional注解及作用

@ConditionalOnBean:當(dāng)容器中有指定的Bean的條件下  
@ConditionalOnClass:當(dāng)類路徑下有指定的類的條件下  
@ConditionalOnExpression:基于SpEL表達(dá)式作為判斷條件
 @ConditionalOnJava:基于JVM版本作為判斷條件  
@ConditionalOnJndi:在JNDI存在的條件下查找指定的位置  
@ConditionalOnMissingBean:當(dāng)容器中沒(méi)有指定Bean的情況下  
@ConditionalOnMissingClass:當(dāng)類路徑下沒(méi)有指定的類的條件下  
@ConditionalOnNotWebApplication:當(dāng)前項(xiàng)目不是Web項(xiàng)目的條件下  
@ConditionalOnProperty:指定的屬性是否有指定的值  
@ConditionalOnResource:類路徑下是否有指定的資源  
@ConditionalOnSingleCandidate:當(dāng)指定的Bean在容器中只有一個(gè),或者在有多個(gè)Bean的情況下,用來(lái)指定首選的Bean
 @ConditionalOnWebApplication:當(dāng)前項(xiàng)目是Web項(xiàng)目的條件下 

最后一步,在resources/META-INF/下創(chuàng)建spring.factories文件,并添加如下內(nèi)容:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.autocinfigure.StarterAutoConfigure

至此,我們的一個(gè)Starter代碼部分就是完成了,下面將項(xiàng)目安裝到本地Maven倉(cāng)庫(kù)中。

發(fā)布

在項(xiàng)目根目錄執(zhí)行 mvn install 進(jìn)行打包安裝。

測(cè)試

將Starter項(xiàng)目的依賴添加到我們自己的SpringBoot項(xiàng)目中

<dependency>    
  <groupId>com.ysc</groupId>    
  <artifactId>simple-spring-boot-starter</artifactId>    
  <version>1.0-SNAPSHOT</version>
 </dependency>

application.yml 配置文件中添加配置信息:

example
  service
    enabled: true
    config: abc-des-dde,SSS-DRS-RE,SDR-SDFR-XXX

在本地使用JUnit進(jìn)行代碼測(cè)試

    @Autowiredprivate StarterService starterService; @Testpublic 
    void starterTest() {    
      String[] splitArray = starterService.split(",");
      System.out.println(splitArray);
    }

好,到這我們的一個(gè)自定義starter就完成了

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容