學記:為spring boot寫一個自動配置


  spring boot遵循“約定優於配置”的原則,使用annotation對一些常規的配置項做默認配置,減少或不使用xml配置,讓你的項目快速運行起來。spring boot的神奇不是借助代碼的生成來實現的,而是通過條件注解來實現的。

  自動配置AutoConfiguration是實現spring boot的重要原理,理解AutoConfiguration的運行原理特別重要,自己寫一個AutoConfiguration可以加深我們對spring boot的理解。

1、定義Type-safe Configuration Properties

@ConfigurationProperties("author")//1
public class AuthorPropertis {
    private static final String NAME = "pyj";

    private static final String PWD = "12324";

    private String name = NAME;//2

    private String pwd = PWD;

    private String[] arrayProps;//3

    private Properties properties = new Properties();//4

    private List<Map<String, Object>> listProp1 = new ArrayList();//5

    private List<String> listProp2 = new ArrayList();//6

    private Map<String, Object> mapProps = new HashMap();//7 

    getter and setter...
}

1、@ConfigurationProperties映射application.yml以author為前綴的配置

2、author.name的默認值是pyj

3-7:對於容器對象的屬性注入

2、定義一個簡單的bean

public class HelloService {

    private String msg;
  getter and setter...
 }

注意:不用糾結HelloService的內容,它就是一個簡單的bean。

3、定義AutoConfiguration

@Configuration//1
@ConditionalOnClass(HelloService.class)//2
@EnableConfigurationProperties(AuthorPropertis.class)//3
@ConditionalOnProperty(prefix = "author",value = "enabled",matchIfMissing = true)//4
public class AuthorAutoConfiguration {

    private final AuthorPropertis authorPropertis;

    public AuthorAutoConfiguration(AuthorPropertis authorPropertis) {//5
        this.authorPropertis = authorPropertis;
    }

    @Bean//6
    @ConditionalOnMissingBean(HelloService.class)//7
    public HelloService helloService() throws JsonProcessingException {
        HelloService helloService = new HelloService();
        helloService.setMsg(authorPropertis.getName() +" :" + authorPropertis.getPwd());

        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println("arrayProps: " + objectMapper.writeValueAsString(authorPropertis.getArrayProps()));
        System.out.println("listProp1: " + objectMapper.writeValueAsString(authorPropertis.getListProp1()));
        System.out.println("listProp2: " + objectMapper.writeValueAsString(authorPropertis.getListProp2()));
        System.out.println("mapProps: " + objectMapper.writeValueAsString(authorPropertis.getMapProps()));
        System.out.println("Props: " + objectMapper.writeValueAsString(authorPropertis.getProperties()));
        return helloService;
    }
}

1、@Configuration聲明為一個配置類

2、@ConditionalOnClass表示HelloService在類路徑下的時候調用

3、@EnableConfigurationProperties使用AuthorPropertis的配置

4、@ConditionalOnProperty指定的author是否有默認值

5、注入AuthorPropertis 

6-7、當不存在HelloService的bean時候,新建一個HelloService的bean 

4、在spring.factories注冊

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.github.gin.springboot.config.AuthorAutoConfiguration

  在resources新建META-INF目錄,再新建spring.factories,注冊上AuthorAutoConfiguration,讓spring boot知道為你自動配置。

  如果不明白這個的原理,可以看看@EnableAutoConfiguration通過@Import({EnableAutoConfigurationImportSelector.class})來讀取spring.factories

5、配置文件application.yml

debug: true

author:
  name: pyj
  pwd: 1234
  properties:
    reasonable: 'true'
    returnPageInfo: 'true'
  listProp1:
    - name: abc
      value: sgaw
    - name: efg
      value: sagsag
  listProp2:
    - config2Value1
    - config2Vavlue2
  mapProps:
    reasonable: true
    returnPageInfo: true
  arrayProps: 1,2,3,4,5

6、測試

@RestController
public class TestController {

    @Autowired
    HelloService helloService;

    @RequestMapping("/")
    public String index(){
        return "hello world!";
    }

    @RequestMapping("/hello")
    public String hello(){
        return helloService.getMsg();
    }
}

運行mvn spring-boot:run,如果看到結果,那么證明是沒問題的

 

  上面的AuthorPropertis 、HelloService 、AuthorAutoConfiguration 可以作為一個獨立的starter pom為其他項目提供一個通用的服務。雖然spring boot覆蓋了大部分的使用場景,但也不是全部都有,但我們也可以利用AutoConfiguration起到同樣的效果。

   實例代碼地址:有愛自取


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM