在Spring Boot中使用 @ConfigurationProperties 注解


Spring Boot 提供了另一種方式 ,能夠根據類型校驗和管理application中的bean。 這里會介紹如何使用@ConfigurationProperties
繼續使用mail做例子。配置放在mail.properties文件中。屬性必須命名規范才能綁定成功。舉例:
1 protocol and PROTOCOL will be bind to protocol field of a bean
2 smtp-auth , smtp_auth , smtpAuth will be bind to smtpAuth field of a bean
3 smtp.auth will be bind to … hmm to smtp.auth field of a bean!

Spring Boot 使用一些松的規則來綁定屬性到@ConfigurationProperties bean 並且支持分層結構(hierarchical structure)。
開始創建一個@ConfigurationProperties bean:

@ConfigurationProperties(locations = "classpath:mail.properties", ignoreUnknownFields = false, prefix = "mail") public class MailProperties { public static class Smtp { private boolean auth; private boolean starttlsEnable; // ... getters and setters } @NotBlank private String host; private int port; private String from; private String username; private String password; @NotNull private Smtp smtp; // ... getters and setters }

…從如下屬性中創建 ( mail.properties ):

mail.host=localhost mail.port=25 mail.smtp.auth=false mail.smtp.starttls-enable=false mail.from=me@localhost mail.username= mail.password=

上例中我們用@ConfigurationProperties注解就可以綁定屬性了。ignoreUnknownFields = false告訴Spring Boot在有屬性不能匹配到聲明的域的時候拋出異常。開發的時候很方便! prefix 用來選擇哪個屬性的prefix名字來綁定。
請注意setters 和 getters 需要在@ConfigurationProperties bean中創建! 與@Value注解相反, 這帶來了代碼中的一些困擾 (特別是簡單的業務中,個人觀點).
OK,但是我們需要用屬性來配置 application. 有至少兩種方式來創建@ConfigurationProperties。即可以搭配@Configuration 注解來提供 @Beans 也可以單獨使用並注入 @Configuration bean。

方案1:
@Configuration @ConfigurationProperties(locations = "classpath:mail.properties", prefix = "mail") public class MailConfiguration { public static class Smtp { private boolean auth; private boolean starttlsEnable; // ... getters and setters } @NotBlank private String host; private int port; private String from; private String username; private String password; @NotNull private Smtp smtp; // ... getters and setters @Bean public JavaMailSender javaMailSender() { // omitted for readability } }
方案2

我們和上面例子一樣注解屬性,然后用 Spring的@Autowire來注入 mail configuration bean:

@Configuration @EnableConfigurationProperties(MailProperties.class) public class MailConfiguration { @Autowired private MailProperties mailProperties; @Bean public JavaMailSender javaMailSender() { // omitted for readability } }

請注意@EnableConfigurationProperties注解。 這個注解告訴Spring Boot 使能支持@ConfigurationProperties。如果不指定會看到如下異常:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [demo.mail.MailProperties] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

注意: 還有其他辦法 (Spring Boot 總是有其他辦法!) 讓@ConfigurationProperties beans 被添加 – 用@Configuration或者 @Component注解, 這樣就可以在 component scan時候被發現了。

總結:

@ConfigurationProperties很方便使用。 比用@Value注解好嗎? 在特定的方案中是的,這只是一個選擇問題。



作者:crocodile_b
鏈接:http://www.jianshu.com/p/df57fefe0ab7
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。


免責聲明!

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



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