自定义bean属性
1.定义bean属性
// 通过@ConfigurationProperties加载properties文件内的配置,
// 通过prefix属性指定properties的配置的前缀,通过locations指定properties文件的位置
@ConfigurationProperties(prefix = "com.dudu")
public class ConfigBean {
private String name;
private String want;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWant() {
return want;
}
public void setWant(String want) {
this.want = want;
}
}
// 1.4版本的可以 通过@ConfigurationProperties加载properties文件内的配置,
// 通过prefix属性指定properties的配置的前缀,通过locations指定properties文件的位置
//1.5版本后没有locations属性了,需要配合使用后@Configuration
// 和@PropertySource("classpath:test.properties")来指定properties文件的位置
@Configuration
@ConfigurationProperties(prefix = "com.md")
@PropertySource("classpath:test.properties")
public class ConfigTestBean {
private String name;
private String want;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWant() {
return want;
}
public void setWant(String want) {
this.want = want;
}
}
2.在属性文件配置
3.启用bean属性配置
@EnableConfigurationProperties({ConfigBean.class, ConfigTestBean.class})
在配置文件提示自定义属性
1.加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
2.mvn compile