@ConfigurationProperties + @EnableConfigurationProperties
1、ConfigurationProperties
在类上通过@ConfigurationProperties注解声明当前类为属性读取类。
举例:
@ConfigurationProperties(prefix = "jdbc")
prefix="jdbc" 读取属性文件中,前缀为jdbc的值。
在类上定义各个属性,名称必须与属性文件中 jdbc. 后面部分一致。
需要注意的是,如果我们没有指定属性文件的地址,SpringBoot 默认读取 application.properties/application.yml 中的属性文件名。
@Data
@ConfigurationProperties(prefix = "jdbc")
class JdbcProperties { private String url;
private String driverClaprivate;
private String username;
private String password;
}
2、EnableConfigurationProperties
@ConfigurationProperties 注解我们可以理解成用来把 properties 配置文件转化为 Bean 使用的,而 @EnableConfigurationProperties 注解的作用是让 @ConfigurationProperties 注解生效。
如果只配置 @ConfigurationProperties 注解,在 IOC 容器中是获取不到 properties 配置文件转化的 Bean 的。
那么我们如何获取我们使用了 @ConfigurationProperties 注解的类呢?
2.1 @Autowired 注入
@Autowired
private JdbcProperties prop;
2.2 构造函数注入
private JdbcProperties prop;
public JdbcConfig(Jdbcproperties prop){
this.prop = prop;
}
2.3 声明有@Bean的方法参数注入
@Bean
public TestBean dataSource(JdbcProperties jdbcProperties) {
syso(jdbcProperties.getUsername());// syso 简写
return new TestBean;
}
通过上方三种方式,都可以在 JdbcProperties jdbcProperties 中直接拿到注入的数据。
@ConfigurationProperties + @EnableConfigurationProperties的更多相关文章
- @Import @bean,@Conditional @ConfigurationProperties @EnableConfigurationProperties 注解使用
一分钟学会spring注解之@Import注解http://blog.51cto.com/4247649/2118354 @Autowired与@Resource 注解的使用 https://www. ...
- springboot @ConfigurationProperties @EnableConfigurationProperties @Bean @ Component
https://www.cnblogs.com/duanxz/p/4520571.html https://juejin.im/post/5cbeaa26e51d45789024d7e2 1. Bea ...
- @Configuration,@ConfigurationProperties,@EnableConfigurationProperties
@Configuration API: https://www.javadoc.io/doc/org.springframework/spring-context/5.0.7.RELEASE @Con ...
- springboot注解之@Import @Conditional @ImportResource @ConfigurationProperties @EnableConfigurationProperties
1.包结构 2.主程序类 1 @SpringBootApplication(scanBasePackages={"com.atguigu"}) 2 public class Mai ...
- 在Spring Boot中使用 @ConfigurationProperties 注解, @EnableConfigurationProperties
但 Spring Boot 提供了另一种方式 ,能够根据类型校验和管理application中的bean. 这里会介绍如何使用@ConfigurationProperties.继续使用mail做例子. ...
- @EnableConfigurationProperties、@EnableAsync、 @EnableTransactionManagement
★@ConfigurationProperties和@EnableConfigurationProperties配合使用 @ConfigurationProperties注解主要用来把properti ...
- SpringBoot之ConfigurationProperties 源码解读
前言 ConfigurationProperties 是SpringBoot引入的一个和外部配置文件相关的注解类.它可以帮助我们更好的使用外置的配置文件属性. 源码解析 属性注入到Java类 @Tar ...
- spring@value取不到值的几种情况
一,spring组件重写构造方法,在构造方法中引用@value为null 由于spring实例化顺序为先执行构造方法,再注入成员变量,所以序为先执行构造方法,再注入成员变量,所以ing实例化顺取值为n ...
- Spring系列之——springboot解析resources.application.properties文件
摘要:本文通过讲解如何解析application.properties属性,介绍了几个注解的运用@Value @ConfigurationProperties @EnableConfiguration ...
随机推荐
- POSTMAN发送WebService接口
WebService是一种跨编程语言和跨操作系统平台的远程调用技术 http://www.oorsprong.org/websamples.countryinfo/countryinfoservice ...
- 让我头疼一下午的Excel合并单元格
Excel导出常见问题 excel导出其实不算什么难事 在网上copy下模板代码,填充自己的业务数据,提供一个http接口基本就可以得到你要导出的数据了. 但是,凡事都有例外,截止今天,excel导出 ...
- Openwrt 刷机后配置WAN口,安装luci和设置中文、安装挂载USB存储。
官方版本的ROM编译时时没有把luci和uhttpd打包进去的,所以,要ssh登录到路由器后手动安装,默认用户名root,密码是空. 如果你的路由器是挂载在其他路由下面的,DHCP可以获取到IP,能正 ...
- Mybatis(五) 延迟加载和缓存机制(一级二级缓存)
踏踏实实踏踏实实,开开心心,开心是一天不开心也是一天,路漫漫其修远兮. --WH 一.延迟加载 延迟加载就是懒加载,先去查询主表信息,如果用到从表的数据的话,再去查询从表的信息,也就是如果没用到从表的 ...
- jQuery - Detect value change on hidden input field
You can simply use the below function, You can also change the type element. $("input[type=hidd ...
- 安全工具-Arachni
Arachni是一个多功能.模块化.高性能的Ruby框架,旨在帮助渗透测试人员和管理员评估web应用程序的安全性.同时Arachni开源免费,可安装在windows.linux以及mac系统上,并且可 ...
- V-Charts中使用extend属性定制词云图
[本文出自天外归云的博客园] 简介 在Vue中使用E-Charts可以用V-Charts,词云图在V-Charts官网中介绍比较简单,如果想更多定制的话,官网上说要在extend属性中进行扩展. V- ...
- extjs ajax 同步 及 confirm 确认提示框问题
//上传文件 uploadModel: function() { if(Ext.getCmp('exup').getForm().isValid()) { var ssn = this.upPanel ...
- win10 Faster-RCNN训练自己数据集遇到的问题集锦 (转)
题注: 在win10下训练实在是有太多坑了,在此感谢网上的前辈和大神,虽然有的还会把你引向另一个坑~~. 最近,用faster rcnn跑一些自己的数据,数据集为某遥感图像数据集——RSOD,标注格式 ...
- js this 引起的祸
注意:这里的 this 指向已经改变了. 所以不能再次使用 this 需在上面自定义变量. 那些年踩过的坑,记录一下.