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

原创
2017/01/17 10:16
阅读数 3.3K

    场景:在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;
    }

}

如果你有更简单的方式,欢迎交流。

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
2 收藏
0
分享
返回顶部
顶部