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 {}
SpringBoot @Value、 @ConfigurationProperties 与 @EnableConfigurationProperties 使用相关推荐
- @Value,@ConfigurationProperties,@EnableConfigurationProperties,@PropertySource,@PropertySources
@Value 注解可以用来将外部的值动态注入到 Bean 中,在 @Value 注解中,可以使用 ${} 或 #{}.${} 与 #{} 的区别如下: (1)@Value("${}" ...
- springboot之@ConfigurationProperties加载配置文件
person.properties文件中 person.age=12 person.boss=false person.last-name="张三" person.maps.k1= ...
- SpringBoot配置@ConfigurationProperties与@Value的区别
引言 之前创建完成项目之后使用了yml配置文件.并且使用了一个@ConfigurationProperties注解,这次博客主要说说关于@ConfigurationProperties和Spring的 ...
- SpringBoot:@ConfigurationProperties注解使用与源码
文章目录 1.美图 2.概述 2.1 源码 3.猜想 4.@SpringBootApplication 5.@EnableAutoConfiguration 1.美图 2.概述 我们在使用Spring ...
- @ConfigurationProperties 与 @EnableConfigurationProperties
@EnableConfigurationProperties注解的作用是:使使用 @ConfigurationProperties 注解的类生效. 如果一个配置类只配置@ConfigurationPr ...
- springboot中 @Imput与@EnableConfigurationProperties区别
@Import和@EnableConfigurationProperties是Spring Boot中两个不同的注解,它们的作用分别如下: @Import注解的作用是将指定的类或配置类导入到当前类中, ...
- @EnableConfigurationProperties 注解和@ConfigurationProperties注解实现配置绑定
ConfigurationProperties注解主要用来把properties配置文件转化为bean来使用的,而@EnableConfigurationProperties注解的作用是@Config ...
- Spring Boot 关于 @EnableConfigurationProperties 注解 —— 使用 @ConfigurationProperties 注解的类生效。
先说作用: @EnableConfigurationProperties注解的作用是:使用 @ConfigurationProperties 注解的类生效. 说明: 如果一个配置类只配置@Config ...
- DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描...
DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之Spr ...
最新文章
- 他凭什么年薪35W,如何抓住2018年大数据AI浪潮?
- #用construct2做游戏
- H.264中POC类型之探讨
- 为什么GEO2R/GEOquery的结果可能是错的?
- y空间兑换代码_【CV学习笔记】色彩空间
- 读取 Excel 之 Epplus
- 【暖*墟】#树链剖分# 树链剖分学习与练习
- SpringCloud实战(一)基于nacos实现订单+视频服务的调用
- flask基础根据知了课堂整理
- java 时间乱码,Java乱码解决方案
- 为什么经转速环PI之后的输出量是电流(基于MTPA分析,内含代码)
- heartbeat如何监控程序_Nagios监控Heartbeat
- HTML中如何使用JavaScript创建链接?
- SpringCloud五大神兽之Eureka服务注册(三)——Eureka的自我保护
- Linux 下查看硬盘 smart 信息
- 面试题:十瓶牛奶每天至少喝一瓶,直到喝完到底有多少种喝法
- python 回溯算法总结
- python查看excel所有sheetname
- 2020.03.18模拟赛17(第二题)
- 现在自学Java可以找到工作吗?