Using @ConfigurationProperties in spring-boot




The function of the @ConfigurationProperties annotation is to map the properties of the configuration file into a POJO entity class according to a prefix. As long as the property names are consistent, it can be automatically injected into it, which is very convenient to use. This is easy to be confused with the @Configuration annotation. @Configuration can also Annotate a configuration class, the difference is that it needs to declare the bound fields again for each property, which is slightly more complicated, so it is recommended to use the @ConfigurationProperties annotation.


Let's look at a simple example. I have a configuration file that contains properties of various data structures, as follows:


#Simple properties
[email protected]
mail.port=9000
[email protected]

#List properties
mail.recipients[0][email protected]
mail.recipients[1][email protected]

#Map Properties
mail.additionalHeaders.redelivery=true
mail.additionalHeaders.secure=true

#Object properties
mail.credentials.username=john
mail.credentials.password=password
mail.credentials.authMethod=SHA1


#List<Object>

mail.cs[0].username=cs1
mail.cs[0].password=cs1pwd
mail.cs[0].authMethod=SHA1


mail.cs[1].username=cs2
mail.cs[1].password=cs2pwd
mail.cs[1].authMethod=SHA2


#Map<String,Object>

mail.mp.k1.username=k1
mail.mp.k1.password=pwdk1
mail.mp.k1.authMethod=SHA3


mail.mp.k2.username=k2
mail.mp.k2.password=pwdk2
mail.mp.k2.authMethod=SHA3




Now we can map it to a configuration class through the @ConfigurationProperties annotation, which is very convenient to use:

@Configuration
@PropertySource("classpath:mail.properties")
@ConfigurationProperties(prefix = "mail")
public class ConfigProperties {


    public static class Credentials{

        private String authMethod;
        private String username;
        private String password;

        public String getAuthMethod() {
            return authMethod;
        }

        public void setAuthMethod(String authMethod) {
            this.authMethod = authMethod;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        @Override
        public String toString() {
            return "Credentials{" +
                    "authMethod='" + authMethod + '\'' +
                    ", username='" + username + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    }


    private String host;
    private int port;
    private  String from;
    private  Credentials credentials;
    private List<String> recipients;//Recipients
    private Map<String,String> additionalHeaders;
    private Map<String,Credentials> mp;
    private List<Credentials>  cs;
	
	// getter setter omitted
	
	}






Each attribute in this class corresponds to the attribute in the configuration. Note that the field name must be the same to assign a value:
in order to verify whether it is successful, we build a controller class and verify it through http://localhost:8777/test
, see whether succeed:

=============Simple Property Access =================
[email protected]
[email protected]
9000
=============List[String] access =================
[[email protected], [email protected]]

=============Map[String,String] access =================
{secure=true, redelivery=true}

=============Object Access =================
Credentials{authMethod='SHA1', username='john', password='password'}

=============List[Object] access =================
Credentials{authMethod='SHA1', username='cs1', password='cs1pwd'}
Credentials{authMethod='SHA2', username='cs2', password='cs2pwd'}

=============Map[String,Object] access =================
k2 Credentials{authMethod='SHA3', username='k2', password='pwdk2'}
k1 Credentials{authMethod='SHA3', username='k1', password='pwdk1'}
(Note that the Set property is not supported)



It can be seen that it has been successfully injected, and it is very simple to use. It is not necessary to inject various data structures into the bean through a lot of xml like in spring, which makes the code a lot simpler.


The project has been shared on github, and interested friends can star: https://github.com/qindongliang/spring-boot-properties

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326653328&siteId=291194637