qindongliang1922 阅读(104) 评论(0)



@ConfigurationProperties注解的作用是可以根据一个前缀将配置文件的属性映射成一个POJO实体类,只要属性名一致就能自动注入进去,使用起来非常方便,这一点容易与@Configuration注解混淆,@Configuration也可以注解一个配置类,不一样的是它需要为每个属性再次声明绑定的字段,稍微复杂,所以推荐使用@ConfigurationProperties注解。


下面看一个简单的例子,我有一个配置文件,下面包含了多种数据结构的属性,如下:


#Simple properties
mail.host=mailer@mail.com
mail.port=9000
mail.from=mailer@mail.com

#List properties
mail.recipients[0]=admin@mail.com
mail.recipients[1]=owner@mail.com

#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




现在我们就可以通过@ConfigurationProperties注解将其映射成一个配置类,这样使用起来就非常方便了:

@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;//接受者
    private Map<String,String> additionalHeaders;
    private Map<String,Credentials> mp;
    private List<Credentials>  cs;
	
	//getter setter 省略
	
	}






这个类里面的每个属性和配置里面的属性对应,注意字段名必须是一样的才能赋值:
为了验证是否成功,我们建一个controller类,通过http://localhost:8777/test
来验证一下,看是否成功:

==============简单属性访问================
mailer@mail.com
mailer@mail.com
9000
==============List[String]访问================
[admin@mail.com, owner@mail.com]

==============Map[String,String]访问================
{secure=true, redelivery=true}

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

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

==============Map[String,Object]访问================
k2 Credentials{authMethod='SHA3', username='k2', password='pwdk2'}
k1 Credentials{authMethod='SHA3', username='k1', password='pwdk1'}
(注意不支持Set属性) 



可以看到,已经成功注入,使用起来非常简洁,不在像spring里面还得通过一大堆xml来注入各种数据结构到Bean里面,使得代码精简了不少。


工程已经分享到github上了,感兴趣的朋友可以star:https://github.com/qindongliang/spring-boot-properties