咱們在使用Spring Cloud全家桶構建微服務應用時,常常能看到spring-boot-xxx-starter的依賴,像spring-boot-starter-web、spring-cloud-starter-feign、spring-boot-starter-test、mybatis-spring-boot-starter,彷彿只要帶上starter的東西,你就擁有了這個組件的一切,包括全部的配置,引用類都搞定了,這樣一個神奇的拿來就用的東西,是怎麼實現的呢?咱們本身能不能把本身的工具包作成一個starter?java
groupId:這個標籤的命名沒作太多要求,基本上使用公司域名+項目名方式,如官方通常使用org.springframework.cloud,第三方通常用本身公司域名,如org.mybatis.spring.boot。 artifactId:這個標籤的命名Spring官方給了建議命名方式,Spring官方本身發佈的組件,命名方式是spring-boot-starter-xxx,xxx表示組件名稱,像上文說起的spring-boot-starter-web和spring-cloud-starter-feign;第三方開發的組件,命名方式是xxx-spring-boot-starter,如mybatis-spring-boot-starter。git
以maven工程爲例,Spring Boot Starter用多模塊方式創建工程,工程內有autoconfigure模塊和starter模塊。 autoconfigure模塊爲自動配置模塊,裏面包含配置加載,所有的功能代碼實現及須要引用的jar包,負責對內功能實現,全部的代碼開發都在這個模塊中完成。 starter模塊提供自動配置模塊的依賴,裏面沒有代碼,是個空jar包,只有對autoconfigure模塊的全部引用,是一個依賴集,它的目的是簡化使用該組件時的依賴,只要添加starter模塊,就能使用整個starter組件。github
咱們以經常使用的RocketMQ客戶端組件爲例,搭建一個本身定義的starter,RocketMQ是由阿里巴巴團隊開發並捐贈給apache團隊的優秀消息中間件,承受過歷年雙十一大促的考驗。web
<groupId>com.hy.demo</groupId> <artifactId>rocketmq</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>rocketmq-spring-boot-autoconfigure</module> <module>rocketmq-spring-boot-starter</module> </modules> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-client</artifactId> <version>4.5.2</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.8.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> </dependencies>
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.hy.demo.rocketmq.MQConfig
Spring Boot會檢查你發佈的jar中是否存在META-INF/spring.factories文件,自動配置類只能經過這種方式加載spring
<parent> <artifactId>rocketmq</artifactId> <groupId>com.hy.demo</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>rocketmq-spring-boot-starter</artifactId> <dependencyManagement> <dependencies> <dependency> <groupId>com.hy.demo</groupId> <artifactId>rocketmq</artifactId> <version>1.0-SNAPSHOT</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.hy.demo</groupId> <artifactId>rocketmq-spring-boot-autoconfigure</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
在IDEA上對該工程進行編譯,打包,命令:apache
clean install -DskipTests=true
api
四、打包部署完成後,在應用模塊裏添加該starter的依賴便可mybatis
<dependency> <groupId>com.hy.demo</groupId> <artifactId>rocketmq-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
注:由於RocketMQ組件較爲通用,目前提供基本的幾種發送和接收消息的方式,支持事務消息,文章內就不一一解釋代碼功能,附上這次源碼地址:架構
rocketmq-spring-boot-starter源碼示例併發
Spring的幾個註解: @Import用來整合全部在@Configuration註解中定義的Bean配置; @EventListener 事件監聽,裏面寫的ContextStartedEvent,表示監聽Spring上下文啓動完成後的事件; @Configuration至關於xml的beans標籤; @Bean標註在方法上,等同於xml的bean;
自定義註解@MQConsumer和註解@MQTransactionProducer是如何起做用的? 工程裏定義了com.hy.demo.rocketmq.config.RocketMQAnnotationScan類對這兩個註解進行掃描,利用註解@EventListener(ContextStartedEvent.class),監聽Spring上下文初始化事件,而後從Spring容器內讀取全部帶這兩個註解的類,把RocketMQ相關的配置信息加載進去,因爲事務消息生產者類org.apache.rocketmq.client.producer.TransactionMQProducer的特殊性(它須要在初始化時注入TransactionListener監聽類,與應用模塊有必定耦合性),因此增長了一個Map集合存儲應用模塊內全部使用了@MQTransactionProducer註解的實例。 專一Java高併發、分佈式架構,更多技術乾貨分享與心得,請關注公衆號:Java架構社區