spring的@Value注解使用
1.@Value注解作用
有@Value(“${}”)和@Value(“#{}”)两种方式:
@Value的值有两类:
① ${ property : default_value }
② #{ obj.property? :default_value }
第一个注入的是外部配置文件对应的property,第二个则是SpEL 表达式对应的内容。 那个default_value,就是前面的值为空时的默认值。注意二者的不同,#{}里面那个obj代表对象。
**
2.@Value注解作用的两种方式
**
第一种方式@Value(“${}”):
oss: appKey: 1 appSecret: 1 bucket: lynn endPoint: https://www.aliyun.com
oss.appKey =1 oss.appSecret=1 oss.bucket=lynn oss.endPoint=https:www.aliyun.com
springMVC的controller结构如下:
@Value("${oss.appKey}")
private String appKey;
@Value("${oss.appSecret}")
private String appSecret;
@Value("${oss.bucket}")
private String bucket;
@Value("${oss.endPoint}")
private String endPoint;
@Bean
public Aliyun aliyun(){
return Aliyun.options().setAppKey(appKey)
.setAppSecret(appSecret).setBucket(bucket)
.setEndPoint(endPoint).build(); }