Springboot1.x和2.x 通过@ConfigurationProperties对bean刷新自定义属性的实现方法和用法区别
一、Springboot1.x @ConfigurationProperties的用法
Springboot1.x中通过@ConfigurationProperties注解来实现对bean的自定义属性的注入,需要在配置文件中配置相应的属性,然后通过@ConfigurationProperties注解将配置文件中的属性注入到bean中,具体代码如下:
@ConfigurationProperties(prefix = "my")
public class MyProperties {
private String name;
private int age;
//getter、setter省略
}其中,@ConfigurationProperties注解的prefix属性值指定配置文件中的属性的前缀,然后在配置文件中配置相应的属性,如:
my.name=张三
my.age=18二、Springboot2.x @ConfigurationProperties的用法
Springboot2.x中也可以通过@ConfigurationProperties注解来实现对bean的自定义属性的注入,与Springboot1.x的用法类似,但Springboot2.x在此基础上增加了一个refresh属性,可以对bean的自定义属性进行刷新,具体代码如下:
@ConfigurationProperties(prefix = "my",refresh = true)
public class MyProperties {
private String name;
private int age;
//getter、setter省略
}在Springboot2.x中,通过@ConfigurationProperties注解的refresh属性设置为true,可以实现对bean的自定义属性的实时刷新,即在配置文件中修改属性后,bean中的属性值也会实时刷新,而不需要重启应用。
三、Springboot1.x和2.x @ConfigurationProperties的区别
从上面的代码可以看出,Springboot1.x和2.x中通过@ConfigurationProperties注解实现对bean的自定义属性的注入的用法基本一致,但Springboot2.x新增了一个refresh属性,可以实现对bean的自定义属性的实时刷新,这也是Springboot1.x和2.x @ConfigurationProperties用法的区别。
上一篇
单片机实现远程智能自动浇花系统 下一篇
如何进行SAP替代与校验 猜您想看
-
如何为电脑设置笔记本电池的使用时间?
。笔记本电池的...
2023年05月03日 -
在PHP中转换日期和时间格式
PHP中转换日...
2023年05月05日 -
如何禁用Windows的屏幕保护程序
一、什么是Wi...
2023年05月15日 -
AopContext.currentProxy()如何解决cache不生效问题
使用AopCo...
2023年07月23日 -
Sqoop数据迁移工具如何使用
1、什么是Sq...
2023年05月26日 -
如何使用@SentinelResource注解灵活的定义控制资源以及如何配置控制策略
1、@Sent...
2023年05月23日