09.05 牛人講解spring boot自動配置實現


自從用了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包。(本文的目的不是講解自動配置的原理,大家可以自行網上搜索原理)。

環境信息

  1. 開發工具:idea
  2. maven版本號:3.5.4

jar包封裝

  1. 創建一個springboot項目
牛人講解spring boot自動配置實現


  1. 填寫座標信息
牛人講解spring boot自動配置實現


  1. springboot版本2.0.4
牛人講解spring boot自動配置實現


  1. 其他默認,創建完成後,目錄如下
牛人講解spring boot自動配置實現


  1. 接下來創建我們的測試服務類TestService
牛人講解spring boot自動配置實現


 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 }
牛人講解spring boot自動配置實現


  1. 我們設想,別人使用我們這個接口的時候,將Ip和Port通過application.properties注入,接下來我們創建屬性配置實體類TestServiceProperties
牛人講解spring boot自動配置實現


 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 }
牛人講解spring boot自動配置實現


  1. 我們發現在這個類寫好後提示有錯誤
  2. 在pom文件加上依賴包,問題解決
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-configuration-processor/<artifactid>
<optional>true/<optional>
/<dependency>
  1. 新建一個自動配置類TestServiceAutoConfiguration
牛人講解spring boot自動配置實現


 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 }
牛人講解spring boot自動配置實現


  1. 相關注解含義在註釋中已經說明,到這裡,代碼已經寫好了,我們希望以註解的方式給用戶使用,自定義一個註解@EnableTestService
牛人講解spring boot自動配置實現


 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 }
牛人講解spring boot自動配置實現


  1. 然後註釋掉pom文件中的maven插件,如下圖
牛人講解spring boot自動配置實現


  1. 然後maven打包,就會生成一個jar包,這個jar包我們就可以直接用了
牛人講解spring boot自動配置實現


  1. 這裡因為在本地環境測試,我們將編譯好的jar安裝到本地maven倉庫,點擊右邊的install按鈕(你也可以導入編譯好的jar包一樣的)

jar包使用

  1. 我們開始來測試我們封裝好的jar,首先創建一個springboot項目(創建時一路默認,你的包名也可以和我一樣,無所謂的)
牛人講解spring boot自動配置實現


  1. pom文件的依賴配置如下
牛人講解spring boot自動配置實現


<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>
牛人講解spring boot自動配置實現


  1. 在main入口類加上註解
牛人講解spring boot自動配置實現


  1. 創建一個TestController類
牛人講解spring boot自動配置實現


 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 }
牛人講解spring boot自動配置實現


  1. 接下來在application.properties中配置參數
  2. 啟動該項目,在地址欄輸入 http://localhost:8080/test 回車,看控制檯打印的信息恰好是我們需要的
牛人講解spring boot自動配置實現


  1. 到這裡我們就完成了一個自動配置的封裝。
  2. 騷年們可以研究一下starter中註解@ConditionalOnProperty對使用的影響。
  3. 今天的故事講完了。
牛人講解spring boot自動配置實現

牛人講解spring boot自動配置實現


分享到:


相關文章: