自從用了spring boot,都忘記spring mvc中的xml配置是個什么東西了,再也回不去。為啥spring boot這么好用呢, 約定大於配置的設計初衷, 讓我們只知道維護好application.properties(或application.yml)文件就可以了,我們在配置文件里可以設置數據源參數,可以設置服務端口,可以設置redis的地址等等。我們經常會看一些包含starter名字的jar包,如spring-boot-starter-data-redis,引入這些jar包,我們就可以簡單快速配置了。那么我們自己開發了一個接口服務給別人調用,我們是不是可以把它封裝成一個starter jar包呢?讓別人在application.properties定義,實現自動配置呢?答案是允許的,下面跟我一起寫一個自動配置jar包。(本文的目的不是講解自動配置的原理,大家可以自行網上搜索原理)。
-
環境信息
開發工具:idea
maven版本號:3.5.4
-
jar包封裝
創建一個springboot項目
填寫坐標信息
springboot版本2.0.4
其他默認,創建完成后,目錄如下
接下來創建我們的測試服務類TestService
1 package com.tanghuachun.teststarter; 2 3 public class TestService { 4 5 private String ip; 6 private int port; 7 8 public TestService(String ip, int port){ 9 this.ip = ip; 10 this.port = port; 11 } 12 13 public void printConfInfo(){ 14 System.out.println("騷年,你配置的IP為:" + ip + ",端口為:" + port); 15 } 16 }
我們設想,別人使用我們這個接口的時候,將Ip和Port通過application.properties注入,接下來我們創建屬性配置實體類TestServiceProperties
1 package com.tanghuachun.teststarter; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 5 @ConfigurationProperties(prefix = "test-config")//取配置文件前綴為test-config配置 6 public class TestServiceProperties { 7 private String host; 8 private int port; 9 10 public String getHost() { 11 return host; 12 } 13 14 public void setHost(String host) { 15 this.host = host; 16 } 17 18 public int getPort() { 19 return port; 20 } 21 22 public void setPort(int port) { 23 this.port = port; 24 } 25 }
我們發現在這個類寫好后提示有錯誤
在pom文件加上依賴包,問題解決
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
新建一個自動配置類TestServiceAutoConfiguration
1 package com.tanghuachun.teststarter; 2 3 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 4 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 5 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 6 import org.springframework.boot.context.properties.EnableConfigurationProperties; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.Configuration; 9 10 @Configuration 11 @ConditionalOnClass(TestService.class)// 存在TestService這個類才裝配當前類 12 @ConditionalOnProperty(name = "test-config.enabled", havingValue = "true", matchIfMissing = true)//配置文件存在這個test-config.enabled=true才啟動,允許不存在該配置 13 @EnableConfigurationProperties(TestServiceProperties.class) 14 public class TestServiceAutoConfiguration { 15 @Bean 16 @ConditionalOnMissingBean // 沒有TestService這個類才進行裝配 17 public TestService testService(TestServiceProperties testServiceProperties) { 18 return new TestService(testServiceProperties.getHost(), testServiceProperties.getPort()); 19 } 20 }
相關注解含義在注釋中已經說明,到這里,代碼已經寫好了,我們希望以注解的方式給用戶使用,自定義一個注解@EnableTestService
1 package com.tanghuachun.teststarter; 2 import org.springframework.context.annotation.Import; 3 import java.lang.annotation.*; 4 5 @Inherited 6 @Documented 7 @Target(ElementType.TYPE) 8 @Retention(RetentionPolicy.RUNTIME) 9 @Import(TestServiceAutoConfiguration.class) 10 //相當於使用定義spring.factories完成Bean的自動裝配 11 public @interface EnableTestService { 12 //@Import(TestServiceAutoConfiguration.class) 需要在調用者的Main 類加上該注解就能等效於spring.factories 文件配置 13 }
然后注釋掉pom文件中的maven插件,如下圖
然后maven打包,就會生成一個jar包,這個jar包我們就可以直接用了
這里因為在本地環境測試,我們將編譯好的jar安裝到本地maven倉庫,點擊右邊的install按鈕(你也可以導入編譯好的jar包一樣的)
-
jar包使用
我們開始來測試我們封裝好的jar,首先創建一個springboot項目(創建時一路默認,你的包名也可以和我一樣,無所謂的)
pom文件的依賴配置如下
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.tanghuachun</groupId> <artifactId>test-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
在main入口類加上注解
創建一個TestController類
1 package com.tanghuachun.testmain; 2 3 import com.tanghuachun.teststarter.TestService; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.web.bind.annotation.GetMapping; 6 import org.springframework.web.bind.annotation.RestController; 7 8 @RestController 9 public class TestController { 10 @Autowired 11 private TestService testService; 12 13 @GetMapping(value = "/test") 14 public String test(){ 15 testService.printConfInfo(); 16 return "OK"; 17 } 18 }
接下來在application.properties中配置參數
啟動該項目,在地址欄輸入 http://localhost:8080/test 回車,看控制台打印的信息恰好是我們需要的
到這里我們就完成了一個自動配置的封裝。
騷年們可以研究一下starter中注解@ConditionalOnProperty對使用的影響。
今天的故事講完了。