欢迎访问 生活随笔!

凯发ag旗舰厅登录网址下载

当前位置: 凯发ag旗舰厅登录网址下载 > 前端技术 > javascript >内容正文

javascript

springboot @value、 @configurationproperties 与 @enableconfigurationproperties 使用 -凯发ag旗舰厅登录网址下载

发布时间:2025/1/21 javascript 15 豆豆
凯发ag旗舰厅登录网址下载 收集整理的这篇文章主要介绍了 springboot @value、 @configurationproperties 与 @enableconfigurationproperties 使用 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

文章目录

    • @value 用法
      • @value("#{}")
      • @value("${}")
    • configurationproperties 用法
      • 使用@component加载到容器中
      • 使用@bean加载到容器中
    • @enableconfigurationproperties用法

@value 用法

@value("#{}")与@value("${}")的区别

@value("#{}")

@value(“#{}”) 表示spel表达式通常用来获取bean的属性,或者调用bean的某个方法。当然还有可以表示常量

@value("#{1}") private int number; //获取数字 1 @value("#{'spring expression language'}") //获取字符串常量 private string str; @value("#{datasource.url}") //获取bean的属性 private string jdbcurl;

@value("${}")

用 @value(“${xxxx}”)注解从配置文件读取值的用

@value("${upload.path}") private string uploadpath;

configurationproperties 用法

使用@component加载到容器中

特殊场景下,我们想把配置文件的信息,读取并自动封装成实体类,这样我们就可以少写很多配置,这时候,就可以使用@configurationproperties,它可以把同类的配置信息自动封装成实体类。

conn.username=admin conn.password=kyjufskifas2jsfs conn.remoteaddress=192.168.1.1 @component @configurationproperties(prefix="conn") public class connsettings {private string username;private string remoteaddress;private string password ;public string getusername() {return username;}public void setusername(string username) {this.username = username;}public string getremoteaddress() {return remoteaddress;}public void setremoteaddress(string remoteaddress) {this.remoteaddress = remoteaddress;}public string getpassword() {return password;}public void setpassword(string password) {this.password = password;}}

使用@bean加载到容器中

@bean@configurationproperties(prefix = "conn")public connsettings connsettings(){return new connsettings();}

@enableconfigurationproperties用法

@enableconfigurationproperties注解的作用是:使 @configurationproperties 注解的类生效。

如果一个配置类只配置@configurationproperties注解,而没有使用@component,那么在ioc容器中是获取不到properties 配置文件转化的bean。@enableconfigurationproperties 相当于把使用 @configurationproperties 的类进行了一次注入。

@configurationproperties(prefix="conn") public class connsettings {private string username;private string remoteaddress;private string password ;public string getusername() {return username;}public void setusername(string username) {this.username = username;}public string getremoteaddress() {return remoteaddress;}public void setremoteaddress(string remoteaddress) {this.remoteaddress = remoteaddress;}public string getpassword() {return password;}public void setpassword(string password) {this.password = password;}}@enableconfigurationproperties(connsettings.class) public class connconfiguration {}

总结

以上是凯发ag旗舰厅登录网址下载为你收集整理的springboot @value、 @configurationproperties 与 @enableconfigurationproperties 使用的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。

网站地图