一、非静态属性
最为简单,把类注册到容器中,使用@Value或@ConfigurationProperties把配置文件中属性绑定。
二、静态属性
spring不支持静态属性的注入,需要自行实现。
1、@PostConstruct
执行顺序constructor -> @Autowired -> @PostConstruct
@Component
public class HConfig {
public static String url;
@Autowired
Environment environment;
@PostConstruct
public void post() {
HConfig.url = environment.getProperty("hconfig.url");
}
}
2、非静态的setter方法
方式一
@Component
@ConfigurationProperties(prefix = "hconfig")
public class HConfig {
public static String url;
public static String getUrl() {
return url;
}
public void setUrl(String url) {
HConfig.url = url;
}
}
方式二
@Component
public class HConfig {
public static String url;
public static String getUrl() {
return url;
}
@Value("${hconfig.url}")
public void setUrl(String url) {
HConfig.url = url;
}
}
3、实现InitializingBean接口,重写afterPropertiesSet方法
@Component
public class HConfig implements InitializingBean {
public static String url;
@Autowired
Environment environment;
@Override
public void afterPropertiesSet() throws Exception {
HConfig.url = environment.getProperty("hconfig.url");
}
}
三、@Value与@ConfigurationProperties区别
@Value | @ConfigurationProperties | |
---|---|---|
注入方式(Dependency Injection) | 手动一个一个绑定 | 批量自动注入 |
松散绑定(Relaxed Binding) | 不支持 | 支持 |
所属包(Package) | org.springframework.beans.factory.annotation(Spring的) | org.springframework.boot.context.properties(Spring Boot的) |
SpEL(Spring Expression Language) | 支持 | 不支持 |
JSR-303数据校验(Java Specification Requests) | 不支持 | 支持 |
复杂类型注入 | 不支持 | 支持 |
注入原理 | 通过属性的setter方法 | 通过属性的setter方法 |
注入条件 | 需要被IOC容器管理 | 需要被IOC容器管理 |
优先级 | 可同时存在,互补,优先级低 | 可同时存在,互补,优先级高 |
1、复杂类型,除了8中基本数据类型、String、Date之外的数据类型;
2、松散绑定:即配置文件中属性,在驼峰之外的命名,还可以写如下
//java文件中
@Component
@ConfigurationProperties(prefix = "open.api")
public class OpenApiConfig {
public String openId;
public String appKey;
public String getOpenId() {
return openId;
}
public void setOpenId(String openId) {
this.openId = openId;
}
public String getAppKey() {
return appKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
}
// 配置文件中
// 驼峰
open.api.openId=12345
// 下滑线
open.api.open_id=12345
// 短横线,spring boot中配置提示默认使用
open.api.open-id=12345
// 全大写
open.api.OPENID=12345
// 其他,混合类型,只要满足不要出现openId(忽略大小写)这个几个英文字母之外的的英文字母、中文汉字就行,符号随便写(配置文件中的特殊符号需要转义)
open.api.oP、EN、------ID=12345
3、JSR-303数据校验(Java Specification Requests),以@Email为例
@Component
@ConfigurationProperties(prefix = "open.api")
@Validated
public class OpenApiConfig {
@Email
public String openId;
public String appKey;
public String getOpenId() {
return openId;
}
@Value("${open.api.openId}")
public void setOpenId(String openId) {
this.openId = openId;
}
public String getAppKey() {
return appKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
}
JSR-303其他注解:请见包javax.validation.constraints
4、SpEL(Spring Expression Language)
①、使用字面量
@Value("#{99090}")
public String appKey;
②、引用其他bean
@Value("#{asyncController}")
public AsyncController asyncController;
③、引用其他bean的某个属性值,asyncController为一个bean,i为其公开的属性
@Value("#{asyncController.i}")
public String appKey;
④、调用静态方法,#{T(全类名).静态方法(参数列表)}
@Value("#{T(java.util.UUID).randomUUID()}")
public String appKey;
⑤、调用非静态方法,asyncController为一个bean,doSomething()为其公开的方法
@Value("#{asyncController.doSomething()}")
public String appKey;
⑥、使用运算符
@Value("#{2*7}")
public String appKey;