從上一篇文章《深入springboot原理——一步步分析springboot啟動機制(starter機制)》
我們已經知道springboot的起步依賴與自動配置的機制。spring-boot-starter-xxx是官方提供的starter,xxx-spring-boot-starter是第三方提供的starter。starter.jar提供jar引入,autoconfigure.jar實現自動配置。下面我們就來封裝一個自己的starter。
准備要封裝的組件
新建組件com-itpsc-service
pom文件
<groupId>com.itpsc</groupId> <artifactId>com-itpsc-service</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>com-itpsc-service</name> <description>com-itpsc-service</description>
編寫UserService類
package com.itpsc.service; public class UserService { private String username; private String password; ... }
打包發布組件
idea終端里面輸入命令mvn install package打包到maven倉庫。
新建一個starter
新建一個名稱為itpsc-spring-boot-starter啟動組件
引入spring-boot-starter、spring-boot-autoconfigure、spring-boot-configuration-processor
這些Jar在編寫自動配置類、注解、生成配置元數據處理等功能依賴的jar包。
<groupId>com.itpsc.spring.boot</groupId> <artifactId>itpsc-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.0.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.0.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>2.0.4.RELEASE</version> </dependency> <dependency> <groupId>com.itpsc</groupId> <artifactId>com.itpsc.service</artifactId> </dependency> </dependencies
編寫自動配置類
UserProperties.java,使用@ConfigurationProperties注解將配置文件(yml/properties)中指定前綴的配置轉為bean。
package com.itpsc.spring.boot.starter; ... @ConfigurationProperties(prefix = "com.itpsc") public class UserProperties { private String username; private String password; ... }
UserAutoConfiguration.java,@Configuration 注釋使類成為bean的工廠。
@EnableConfigurationProperties注解使@ConfigurationProperties注解生效。
package com.itpsc.spring.boot.starter; ... @Configuration @EnableConfigurationProperties(UserProperties.class) public class UserAutoConfiguration { @Bean public UserService getBean(UserProperties userProperties) { //創建組件實例 UserService userService = new UserService(); userService.setUsername(userProperties.getUsername()); userService.setPassword(userProperties.getPassword()); return userService; } }
配置spring.factories文件
\META-INF\spring.factories該文件用來定義需要自動配置的類,springboot啟動時會進行對象的實例化,會通過加載類SpringFactoriesLoader加載該配置文件,將文件中的配置類加載到spring容器。
在src/main/resources新建META-INF文件夾,在META-INF文件夾下新建spring.factories文件。配置內容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.itpsc.spring.boot.starter.UserAutoConfiguration
打包發布starter
idea終端里面輸入命令mvn install package打包到maven倉庫。
測試starter
我們在springboot-mybatis-demo項目中引入starter
<dependency> <groupId>com.itpsc.spring.boot</groupId> <artifactId>itpsc-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
當在yml中配置username、password時就可以看到有自動提示了,這是因為引入的jar中包含了元數據文件,詳細見下文。
com:
itpsc:
username: "itpsc"
password: itpsc@123
元數據文件是在編譯器通過處理所有被@ConfigurationProperties注解的節點來自動生成的。
測試在增加一個測試方法
@Autowired private UserService userService; @Test public void testItpscStarter() { userService.print(); }
運行結果:
2019-01-23 20:22:41.615 INFO 17184 --- [ main] .i.SpringbootMybatisDemoApplicationTests : Started SpringbootMybatisDemoApplicationTests in 11.505 seconds (JVM running for 14.582) username=itpsc password=itpsc@123
從運行結果可以看出,我們封裝的starter中的jar包的bean已經完成了自動配置,說明我們的starter封裝成功了。下面補充下上文提到的元數據相關知識。
元數據
springboot jar包含元數據文件,提供所有支持的配置屬性的詳細信息。這些文件旨在允許IDE開發人員在用戶使用application.properties 或application.yml文件時提供上下文幫助和自動補全 。
主要的元數據文件是在編譯器通過處理所有被@ConfigurationProperties注解的節點來自動生成的。
配置元數據位於jar文件中的META-INF/spring-configuration-metadata.json,它們使用一個具有”groups”或”properties”分類節點的簡單JSON格式。
{ "sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties", "defaultValue": 8080, "name": "server.port", "description": "Server HTTP port.", "type": "java.lang.Integer" }, { "defaultValue": "\/", "deprecated": true, "name": "server.servlet-path", "description": "Path of the main dispatcher servlet.", "type": "java.lang.String", "deprecation": { "level": "error", "replacement": "server.servlet.path" }
這兩個json節點server.port、server.servlet-path對應可以在yml或者properties文件中定義
server:
port: 8081
context-path: /
如果不知道spring是否支持某個配置的話,可以查看元數據文件看是否有對應的節點。
作者:ITPSC
出處:http://www.cnblogs.com/hjwublog/