springboot注入application.properties内的变量:
【@ConfigurationProperties(prefix = “oss”)】
@ConfigurationProperties 这个注解,在内部指定注解前缀prefix为oos,当springboot启动的时候,就会在springboot配置文件中寻找以oos开始,然后以下面实体类字段名将配置信息注入到当前类中;
例如:AliyunAuto实体类中的appKey属性在springboot配置文件中为oos.appKey;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "oss")
public class AliyunAuto {
private String appKey;
private String appSecret;
private String bucket;
private String endPoint;
public String getAppKey() {
return appKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
public String getAppSecret() {
return appSecret;
}
public void setAppSecret(String appSecret) {
this.appSecret = appSecret;
}
public String getBucket() {
return bucket;
}
public void setBucket(String bucket) {
this.bucket = bucket;
}
public String getEndPoint() {
return endPoint;
}
public void setEndPoint(String endPoint) {
this.endPoint = endPoint;
}
@Override
public String toString() {
return "AliyunAuto{" + "appKey='" + appKey + '\'' + ", appSecret='" +
appSecret + '\'' + ", bucket='" + bucket + '\'' + ", endPoint='" +
endPoint + '\'' + '}';
}
}