可以读取SpringBoot中配置文件application.properties中的属性值
application.properties
#阿里云 OSS
#不同的服务器,地址不同
aliyun.oss.file.endpoint=XXX
aliyun.oss.file.keyid=XXX
aliyun.oss.file.keysecret=XXX
#bucket可以在控制台创建,也可以使用java代码创建
aliyun.oss.file.bucketname=XXX
/**
* spring初始化bean的时候,如果bean实现了InitializingBean接口,会自动调用afterPropertiesSet方法。
* ConstantPropertiesUtils实现InitializingBean接口之后,在application.properties中的属性值
* 通过@Value注解赋给当前类的属性之后,就会自动执行afterPropertiesSet方法
*
*/
@Component
public class ConstantPropertiesUtils implements InitializingBean{
@Value("${aliyun.oss.file.endpoint}")
private String endpoint;
@Value("${aliyun.oss.file.keyid}")
private String keyId;
@Value("${aliyun.oss.file.keysecret}")
private String keySecret;
@Value("${aliyun.oss.file.bucketname}")
private String bucketName;
public static String END_POINT;
public static String ACCESS_KEY_ID;
public static String ACCESS_KEY_SECRET;
public static String BUCKET_NAME;
@Override
public void afterPropertiesSet() throws Exception {
END_POINT = endpoint;
ACCESS_KEY_ID = keyId;
ACCESS_KEY_SECRET = keySecret;
BUCKET_NAME = bucketName;
}
}