在最近的博客文章中,我简短地介绍了如何在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