前言
当我们想把配置的内容,动态赋值到某个配置类上的时候,可以使用@EnableConfigurationProperties + @ConfigurationProperties注解
代码准备
创建配置文件prop.properties
name=ada
age=18
email=123@qq.com
创建配置类
@ComponentScan("com.test.pops")
@PropertySource("classpath:prop.properties")
public class AppConfig {
}
创建启动类
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
}
}
查看@ConfigurationProperties源码
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface ConfigurationProperties {
/**
* The prefix of the properties that are valid to bind to this object. Synonym for
* {@link #prefix()}. A valid prefix is defined by one or more words separated with
* dots (e.g. {@code "acme.system.feature"}).
* @return the prefix of the properties to bind
*/
@AliasFor("prefix")
String value() default "";
/**
* The prefix of the properties that are valid to bind to this object. Synonym for
* {@link #value()}. A valid prefix is defined by one or more words separated with
* dots (e.g. {@code "acme.system.feature"}).
* @return the prefix of the properties to bind
*/
@AliasFor("value")
String prefix() default "";
/**
* Flag to indicate that when binding to this object invalid fields should be ignored.
* Invalid means invalid according to the binder that is used, and usually this means
* fields of the wrong type (or that cannot be coerced into the correct type).
* @return the flag value (default false)
*/
boolean ignoreInvalidFields() default false;
/**
* Flag to indicate that when binding to this object unknown fields should be ignored.
* An unknown field could be a sign of a mistake in the Properties.
* @return the flag value (default true)
*/
boolean ignoreUnknownFields() default true;
}
由源码我们可以得知这个注解可以作用于类上,作用于方法上
作用于类上
@EnableConfigurationProperties注解不指定value
创建TypeConfig
@ConfigurationProperties
@Component
public class TypeConfig {
private String name;
private Integer age;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
配置类指定value
@ComponentScan("com.test.pops")
@PropertySource("classpath:prop.properties")
@EnableConfigurationProperties
public class AppConfig {
}
运行main方法,查看运行结果
@EnableConfigurationProperties注解指定value
TypeConfig去除@Component注解
@ConfigurationProperties
public class TypeConfig {
private String name;
private Integer age;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
配置类指定value
@ComponentScan("com.test.pops")
@PropertySource("classpath:prop.properties")
@EnableConfigurationProperties({TypeConfig.class})
public class AppConfig {
}
运行main方法,查看运行结果
@ConfigurationProperties设置prefix
改造TypeConfig
@ConfigurationProperties(prefix = "prop")
public class TypeConfig {
private String name;
private Integer age;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
修改配置文件prop.properties
name=ada
age=18
email=123@qq.com
prop.name=tom
prop.age=17
prop.email=456@qq.com
运行main方法,查看运行结果
使用@ConstructorBinding注解绑定到构造器上
改造TypeConfig
@ConfigurationProperties(prefix = "prop")
public class TypeConfig {
private String name;
private Integer age;
private String email;
public TypeConfig() {
}
@ConstructorBinding
public TypeConfig(String name, Integer age, String email) {
this.name = name;
this.age = age;
this.email = email;
}
}
运行main方法,查看运行结果
PS : @ConstructorBinding注解只能在@EnableConfigurationProperties注解指定value的时候使用
作用于方法上
修改配置类prop.properties
name=ada
age=18
email=123@qq.com
prop.name=tom
prop.age=17
prop.email=456@qq.com
method.name=bob
method.age=16
method.email=789@qq.com
创建MethodConfig
@Configuration
public class MethodConfig {
@Bean
@ConfigurationProperties(prefix = "method")
public Person person() {
return new Person();
}
public static class Person {
private String name;
private Integer age;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
}
运行main方法,查看运行结果