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

Gin.p發表於2016-12-17

  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起到同樣的效果。

   例項程式碼地址:有愛自取

相關文章