大牛一文帶你讀懂:SpringBoot自動裝配的使用和理解

1 AutoConfiguration的使用之Starter的構建

1、服務類

public class HelloService {
    private String msg;
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public String sayHello(){
        return "hello" + msg;
    }
}
複製代碼

2、服務屬性配置類

@ConfigurationProperties(prefix = "tutu.hello")
public class HelloServiceProperties {
    private static final String MSG = "world";
    private String msg = MSG;

    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}
複製代碼

3、自動配置類

@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class) //判斷該類在類路徑下是否存在
@ConditionalOnProperty(prefix = "tutu", value = "enabled",havingValue = "true",matchIfMissing = true)
public class HelloServiceAutoConfiguration {
    @Autowired
    private HelloServiceProperties helloServiceProperties;

    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setMsg(helloServiceProperties.getMsg());
        return helloService;
    }
}
複製代碼

4、註冊配置

# 創建resources/META-INF/spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.test.hello.HelloServiceAutoConfiguration
複製代碼

5、在使用類中引入該starter,然後在application.properties中修改對象參數,最後利用@Resource引入對象。

2 AutoConfiguration實戰細節

2.1 自動裝配中各個註解的作用

  • @ConfigurationProperties作用:修飾屬性配置類,讀取application.properties或application.yml文件中的值然後將值賦予給該類,可以通過創建additional-spring-configuration-metadata.json添加參數正確提示。 { "group": [ { "name": "tutu", "type": "org.test.hello.HelloServiceProperties",//基於貢獻該參數的類 "sourceType": "org.test.hello.HelloServiceProperties"//同上 } ], "properties": [ { "name": "tutu.hello.msg", "type": "java.lang.String", "sourceType": "org.test.hello.HelloServiceProperties"//同上 } ], "hints": [ { "name": "tutu.hello.msg", "values": [ { "value": "none",//可以在這些值之外選擇,但是編輯器會標紅 "description": "Disable DDL handling." } ] } ] } 複製代碼
  • @Comfiguration作用:作爲@bean的對象來源,但又不同於xml配置的繁瑣,基於java代碼的對象配置更加便於維護。
  • @EnableConfigurationProperties作用:用來使得ConfigurationProperties修飾的類生效。
  • @ConditionalOnClass作用:判斷在當前classpath中是否有指定類,如果有才加載修飾的配置類。
  • @ConditionalOnProperty作用:通過在properties文件中設置havingvlaue對應的參數可以有效控制當前配置類是否生效,也就是用來控制configuration生效。
  • @ConditionalOnMissingBean作用:和@bean配合使用只有在beanfactory中不存在指定類時才創建。

2.2 基於自動裝配demo的個人理解

  1. 普通jar包和自動裝配stater包有什麼不同

大體來看其實沒有什麼不同,以上方demo爲例,ConditionalOnClass和ConditionalOnMissingBean並只是用來提高系統健壯性作用類似於異常處理的地位,如果願意可以不使用,而ConditionalOnProperty只是用來給予用戶控制當前配置類是否生效的開關,如果願意也可以不使用讓他一直處於開啓狀態,所以去掉上方三個註解並不影響系統的正常使用只是降低了系統健壯性。

但是最爲關鍵的註解是Configuration和ConfigurationProperties和EnableConfigurationProperties,這三個註解在平常項目中都可以被用到,Comfiguration更多用於配製自定義bean,ConfigurationProperties用於配製自定義參數,所以我認爲一個自動裝配的包和普通jar最大的不同就是自動裝配的包擁有默認的參數配置以及擁有自定義參數配置的能力

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章