您的位置:首页 > 编程语言 > Java开发

SpringBoot如何优雅的将静态资源配置注入到工具类中

2017-01-17 00:00 555 查看
场景:在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;
}

}

如果你有更简单的方式,欢迎交流。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: