Spring Boot1.4版本后@ConfigurationProperties注解舍弃location参数后的解决方式
来源:互联网 发布:java自学视频百度云盘 编辑:程序博客网 时间:2024/05/16 12:28
之前可以通过这样的方式来实现Bean和自定义配置文件的关联
@Component//可以使用locations指定读取的properties文件路径,如果不指定locations就会读取默认的properties配置文件@ConfigurationProperties(prefix = "author",location="classpath:author.properties")public class AuthorSettings { private String name; private Long age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Long getAge() { return age; } public void setAge(Long age) { this.age = age; }}
但是Spring Boot在1.4版本后舍弃了@ConfigurationProperties注解的location参数,导致不能自定义配置文件的位置,用起来比较麻烦。
官方给的解释是为了鼓励大家多使用Environment
按照官方的意思,重新换种方式实现加载自定义配置文件(依然比较麻烦)
新建一个类,监听ApplicationEnvironmentPreparedEvent事件,通过ResourceLoader加载自定义的配置文件
public class LoadAdditionalProperties implements ApplicationListener<ApplicationEnvironmentPreparedEvent> { private ResourceLoader loader = new DefaultResourceLoader(); @Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent applicationEnvironmentPreparedEvent) { try { Resource resource = loader.getResource("classpath:author.properties"); PropertySource<?> propertySource = new PropertySourcesLoader().load(resource); applicationEnvironmentPreparedEvent.getEnvironment().getPropertySources().addLast(propertySource); }catch(IOException e){ throw new IllegalStateException(e); } }}
然后在main方法类中
@RestController@SpringBootApplicationpublic class Ch623Application{// @Autowired// private DemoConfig demoConfig; @Autowired private Environment env; @RequestMapping("/") public String index() { return env.getProperty("author.age"); } public static void main(String[] args) { new SpringApplicationBuilder(Ch623Application.class).listeners(new LoadAdditionalProperties()).run(args); }}
author.name=magicnianauthor.age=25
访问http://localhost:8080/即可看到结果
参考:https://github.com/spring-projects/spring-boot/issues/6220#issuecomment-228412077
0 0
- Spring Boot1.4版本后@ConfigurationProperties注解舍弃location参数后的解决方式
- spring boot1.5以上版本@ConfigurationProperties取消location注解后的替代方案
- spring boot1.5.X以上版本@ConfigurationProperties取消location注解后的替代方案
- spring boot1.5.X以上版本@ConfigurationProperties注解没有location属性后的替代用法
- spring boot1.5以上版本@ConfigurationProperties取消location注解后的替代方案
- FFmpeg3.3中codec被舍弃后,如何获取源码流的codecpar参数
- 在Spring Boot中使用 @ConfigurationProperties 注解
- spring boot:使用@ConfigurationProperties注解加载配置文件
- 学习Spring boot 注解 @Value @Component @ConfigurationProperties
- spring boot @ConfigurationProperties @EnableConfigurationProperties 配置注解使用
- 注解方式定义的spring component打jar后,扫描失败的可能原因
- 注解方式定义的spring component打jar后,扫描失败的可能原因
- spring@Controller注解接受参数的方式
- Spring AOP注解的方式校验参数
- spring事物注解不起作用的解决方式
- spring boot 打成jar包后 通过命令行传入的参数 3中实现方式
- 使用Spring的AOP后,Struts2的@InputConfig注解不起作用
- spring转springboot后,使用注解方式配置spring相关配置
- 用openresty + lua + jqurey 制作一个随机发牌网页
- 【JVM规范笔记一】JVM虚拟机结构
- 数字精确运算BigDecimal长常用用法
- postman测接口 get/post方式
- TextView文字下划线的添加
- Spring Boot1.4版本后@ConfigurationProperties注解舍弃location参数后的解决方式
- 学习笔记-----HTML与CSS之间的关系
- JDK动态代理与Dubbo自实现动态代理的研究
- Linux学习(一)
- MVC5 + EF6 入门完整教程
- RecyclerView之使用ItemTouchHelper实现交互动画
- list对象取出重复的数据
- telerik如何在项目中被引用
- js,html 监听页面滚动高度 点击返回顶部