Using @ConfigurationProperties in Spring Boot

来源:互联网 发布:淘宝申请售后的时间 编辑:程序博客网 时间:2024/05/30 05:20

Spring Boot uses some relaxed rules for binding properties to @ConfigurationProperties beans and supports hierarchical structure.

So let’s create a @ConfigurationProperties bean:

01@ConfigurationProperties(locations = "classpath:mail.properties", ignoreUnknownFields = false, prefix = "mail")
02publicclassMailProperties {
03 
04    publicstaticclass Smtp {
05 
06        privatebooleanauth;
07        privatebooleanstarttlsEnable;
08 
09        // ... getters and setters
10    }
11 
12    @NotBlank
13    privateString host;
14    privateintport; 
15    privateString from;
16    privateString username;
17    privateString password;
18    @NotNull
19    privateSmtp smtp;
20 
21    // ... getters and setters
22 
23}

… that should be created from the following properties (mail.properties):

1mail.host=localhost
2mail.port=25
3mail.smtp.auth=false
4mail.smtp.starttls-enable=false
5mail.from=me@localhost
6mail.username=
7mail.password=

In the above example, we annotated a bean with @ConfigurationPropertiesso that Spring Boot can bind properties to it.ignoreUnknownFields = false tells Spring Boot to throw an exception when there are properties that do not match a declared field in the bean. This is pretty handy during the development! prefix let you select the name prefix of the properties to bind.

Please note that setters and getters should be created in @ConfigurationProperties bean! And opposite to @Valueannotation it may bring some extra noise to the code (especially in simple cases, in my opinion).

Ok, but we want to use the properties to configure our application. There are at least two ways of creating@ConfigurationProperties. We can either use it together with @Configuration that provides @Beans or we can use it separately and inject into @Configuration bean.

The first scenario:

01@Configuration
02@ConfigurationProperties(locations = "classpath:mail.properties", prefix = "mail")
03public class MailConfiguration {
04 
05    public static class Smtp {
06 
07        private boolean auth;
08        private boolean starttlsEnable;
09 
10        // ... getters and setters
11    }
12 
13    @NotBlank
14    private String host;
15    private int port; 
16    private String from;
17    private String username;
18    private String password;
19    @NotNull
20    private Smtp smtp;
21 
22    // ... getters and setters  
23 
24    @Bean
25    public JavaMailSender javaMailSender() {
26        // omitted for readability
27    }
28}

In the second scenario, we simply annotate the properties bean (as above) and use Spring’s@Autowireto inject it into mail configuration bean:

01@Configuration
02@EnableConfigurationProperties(MailProperties.class)
03public class MailConfiguration {
04 
05    @Autowired
06    private MailProperties mailProperties;
07 
08    @Bean
09    public JavaMailSender javaMailSender() {
10        // omitted for readability
11    }
12}

Please note @EnableConfigurationPropertiesannotation. This annotation tells Spring Boot to enable support for@ConfigurationProperties of a specified type. If not specified, you may otherwise see the following exception:

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

Just a note: there is other way (there is always other way with Spring Boot!) to make that @ConfigurationPropertiesannotated beans will be added – just add @Configuration or @Component annotation to the bean, so it can be discovered during component scan.

To sum up, @ConfigurationProperties beans are pretty handy. Is it better than using @Value annotation? In certain scenarios probably yes, but this is just the choice you need to make.

0 0
原创粉丝点击