场景:在Spring架构体系下,你是如何在工具类中获取静态资源配置信息的?总之,我之前是直接通过读取properties文件实现的,但那种方式,总感觉怪怪的,那么,我们就用Spring支持的方式来实现吧。其实,也不算原创,说好听点就是借鉴,只不过被我脱水了,只剩下干货了。
资源注入类:
@Configuration
@ConfigurationProperties(locations = "classpath:/config/qcloud.properties",
ignoreUnknownFields = true,
prefix = "qcloud")
public class QCloudProperties {
public static class properties{
}
private String appid;
private String secretId;
private String secretKey;
private String bucketName;
private String bucketLocation;
public QCloudProperties() {
}
//getter and setter
}
工具类:
@Component
public class QCloudFileUtils {
@Resource
private QCloudProperties qCloudPropertiesAutowired;
private static QCloudProperties qCloudProperties;
@PostConstruct
public void init() {
qCloudProperties = this.qCloudPropertiesAutowired;
}
public static boolean upload() {
String appid = qCloudProperties.getAppid();
return false;
}
}