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替代与校验 猜您想看
-
如何用Python搞定微信公众号邮箱检测
使用Pytho...
2023年05月22日 -
Linux中安装google的libphonenumber c++库方法是什么
一、下载lib...
2023年05月23日 -
python实现kalman滤波的方法
Kalman滤...
2023年05月22日 -
Java怎么找到数组中最大的元素
一、介绍在Ja...
2023年05月26日 -
如何在Linux中使用Python编程语言?
如何在Linu...
2023年04月15日 -
leetCode中回文数的示例分析
一、什么是回文...
2023年05月26日