SpringBoot将静态资源配置注入到工具类中
2017-03-21 00:00
489 查看
de >@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 }de>
工具类:
de >@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; } de>
SpringBoot读取application.properties文件,通常有3种方式
1. @Value 例如:
@Value("${spring.profiles.active}")
private String profileActive;------相当于把properties文件中的spring.profiles.active注入到变量profileActive中
2. @ConfigurationProperties 例如:
@Component
@ConfigurationProperties(locations = "classpath:application.properties",prefix="test")
public class TestProperties {
String url;
String key;
}
其他类中使用时,就可以直接注入该TestProperties 进行访问相关的值
3. 使用Enviroment 例如:
private Enviroment env;
env.getProperty("test.url");
而env方式效率较低
注:@ConfigurationProperties也可用于其他.properties文件,只要locations指定即可
相关文章推荐
- SpringBoot如何优雅的将静态资源配置注入到工具类中
- 静态工具类中使用注解注入service
- 静态工具类中使用注解注入service
- spring 注入静态工具类
- spring静态注入组件——工具类常用
- 静态工具类中注入对象
- spring注入bean进入工具类的静态方法
- Spring注入service为null另类解决办法 工具类 一般类 静态 非controller
- 静态工具类中使用注解注入service(静态方法调用有注解的非静态方法)
- 静态工具类中使用注解注入service
- spring 在静态工具类中使用注解注入bean
- Spring 在静态工具类中使用注解注入bean
- spring 在静态工具类中使用注解注入bean
- 静态工具类中使用注解注入service
- spring boot 两种方式注入到静态工具类里面
- Spring注入service为null另类解决办法 工具类 一般类 静态 非controller
- jsp页面中静态方法调用使用service的类,注入静态工具类使用service
- 静态工具类中使用注解注入service
- 静态工具类中使用注解注入service
- Springboot 静态资源类里静态属性注入