javascript
在Spring Boot中使用@ConfigurationProperties
在最近的博客文章中,我簡短地介紹了如何在Spring Boot應用程序中配置郵件 。 要將屬性注入配置中,我使用了Spring的@Value注釋。 但是Spring Boot提供了一種使用屬性的替代方法,該方法允許強類型的Bean來管理和驗證應用程序的配置。 在本文中,我將演示在配置應用程序時如何利用@ConfigurationProperties 。
因此,我們以郵件配置為例。 配置文件放置在一個名為mail.properties的單獨文件中。 必須使用適當的約定來命名屬性,以便可以正確地綁定它們。 讓我們看一些例子:
- protocol和PROTOCOL將綁定到bean的protocol字段
- smtp-auth , smtp_auth , smtpAuth將綁定到bean的smtpAuth字段
- smtp.auth將被綁定到……hmm到bean的smtp.auth字段!
Spring Boot使用一些寬松的規則將屬性綁定到@ConfigurationProperties bean,并支持層次結構。
因此,讓我們創建一個@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}@NotBlankprivate String host;private int port; private String from;private String username;private String password;@NotNullprivate 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注釋了一個bean,以便Spring Boot可以將屬性綁定到它。 ignoreUnknownFields = false告訴Spring Boot在bean中存在與聲明的字段不匹配的屬性時引發異常。 在開發過程中這非常方便! prefix使您可以選擇要綁定的屬性的名稱前綴。
請注意,setter和getters應該在@ConfigurationProperties bean中創建! 與@Value注釋相反,它可能給代碼帶來一些額外的噪音(在我看來,尤其是在簡單情況下)。
好的,但是我們想使用這些屬性來配置我們的應用程序。 創建@ConfigurationProperties至少有兩種方法。 我們可以將其與提供@Bean的@Configuration一起使用,也可以單獨使用它并注入@Configuration bean中。
第一種情況:
@Configuration @ConfigurationProperties(locations = "classpath:mail.properties", prefix = "mail") public class MailConfiguration {public static class Smtp {private boolean auth;private boolean starttlsEnable;// ... getters and setters}@NotBlankprivate String host;private int port; private String from;private String username;private String password;@NotNullprivate Smtp smtp;// ... getters and setters @Beanpublic JavaMailSender javaMailSender() {// omitted for readability} }在第二種情況下,我們只需對屬性bean進行注釋(如上所述),然后使用Spring的@Autowire將其注入到郵件配置bean中:
@Configuration @EnableConfigurationProperties(MailProperties.class) public class MailConfiguration {@Autowiredprivate MailProperties mailProperties;@Beanpublic 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注釋的Bean –只需向Bean添加@Configuration或@Component注釋,以便可以在組件掃描期間發現它。
總結起來, @ConfigurationProperties bean非常方便。 比使用@Value注釋更好嗎? 在某些情況下可能是,但這只是您需要做出的選擇。
- 請參閱Spring Boot的文檔,以了解有關類型安全配置屬性的更多信息: http : //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-typesafe-configuration-屬性
翻譯自: https://www.javacodegeeks.com/2014/09/using-configurationproperties-in-spring-boot.html
總結
以上是生活随笔為你收集整理的在Spring Boot中使用@ConfigurationProperties的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蚂蚁财富里关注的人哪里可以找到?
- 下一篇: 简而言之,JUnit:另一个JUnit教