从properties文件中加载配置项
配置项比如:
logic.mail.enable=true
对字段使用@Value注解
@Value("${logic.mail.enable}")
private Boolean enable;
对类使用@ConfigurationProperties注解
tips:
对应的字段需要有公用的set方法
@Setter
@ConfigurationProperties(prefix = "logic.mail")
public class SystemMailServiceImpl{
private Boolean enable;
}
使用这种方式建议配置Spring Boot Configuration Processor完成自动补全
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
引入该依赖后的效果:
tips:对应字段有set方法才能被应用,同时再有get方法,才可以被提示到
- 如果没有生效,请使用rebuild方法
使类实现EnvironmentAware接口
public class SystemMailServiceImpl implements EnvironmentAware{
private Boolean enable;
@Override
public void setEnvironment(Environment environment) {
this.enable = environment.getProperty("logic.mail.enable")
}
}