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的更多相关文章

  1. @Import @bean,@Conditional @ConfigurationProperties @EnableConfigurationProperties 注解使用

    一分钟学会spring注解之@Import注解http://blog.51cto.com/4247649/2118354 @Autowired与@Resource 注解的使用 https://www. ...

  2. springboot @ConfigurationProperties @EnableConfigurationProperties @Bean @ Component

    https://www.cnblogs.com/duanxz/p/4520571.html https://juejin.im/post/5cbeaa26e51d45789024d7e2 1. Bea ...

  3. @Configuration,@ConfigurationProperties,@EnableConfigurationProperties

    @Configuration API: https://www.javadoc.io/doc/org.springframework/spring-context/5.0.7.RELEASE @Con ...

  4. springboot注解之@Import @Conditional @ImportResource @ConfigurationProperties @EnableConfigurationProperties

    1.包结构 2.主程序类 1 @SpringBootApplication(scanBasePackages={"com.atguigu"}) 2 public class Mai ...

  5. 在Spring Boot中使用 @ConfigurationProperties 注解, @EnableConfigurationProperties

    但 Spring Boot 提供了另一种方式 ,能够根据类型校验和管理application中的bean. 这里会介绍如何使用@ConfigurationProperties.继续使用mail做例子. ...

  6. @EnableConfigurationProperties、@EnableAsync、 @EnableTransactionManagement

    ★@ConfigurationProperties和@EnableConfigurationProperties配合使用 @ConfigurationProperties注解主要用来把properti ...

  7. SpringBoot之ConfigurationProperties 源码解读

    前言 ConfigurationProperties 是SpringBoot引入的一个和外部配置文件相关的注解类.它可以帮助我们更好的使用外置的配置文件属性. 源码解析 属性注入到Java类 @Tar ...

  8. spring@value取不到值的几种情况

    一,spring组件重写构造方法,在构造方法中引用@value为null 由于spring实例化顺序为先执行构造方法,再注入成员变量,所以序为先执行构造方法,再注入成员变量,所以ing实例化顺取值为n ...

  9. Spring系列之——springboot解析resources.application.properties文件

    摘要:本文通过讲解如何解析application.properties属性,介绍了几个注解的运用@Value @ConfigurationProperties @EnableConfiguration ...

随机推荐

  1. CentOS 7.4上网速度慢,修改DNS!

    修改下DNS,vi /etc/resolv.conf 原来配置: nameserver 223.5.5.5 修改为: nameserver 114.114.114.114 

  2. SNF快速开发平台2018-移动端代码生成器已发布

    各位小伙伴期待已久的移动端已经推出,在此基础上我们又进行配套了代码生成器,以达到高速开发,简化代码等优点. 当然也需要有强大的组件库及标准程序的支撑,同时要有强大的后台做后盾来达到移动端强大功能的施展 ...

  3. c groups

    https://www.kernel.org/doc/Documentation/cgroup-v1/cgroups.txt https://developer.ibm.com/hadoop/2017 ...

  4. 严选 | Elasticsearch史上最全最常用工具清单【转】

    1.题记 工欲善其事必先利其器,ELK Stack的学习和实战更是如此,特将工作中用到的“高效”工具分享给大家. 希望能借助“工具”提高开发.运维效率! 2.工具分类概览 2.1 基础类工具 1.He ...

  5. stl综合

    区别: List封装了链表,Vector封装了数组, list和vector得最主要的区别在于vector使用连续内存存储的,他支持[]运算符,而list是以链表形式实现的,不支持[]. Vector ...

  6. 2.静态AOP实现-装饰器模式

    通过装饰器模式实现在RegUser()方法本身业务前后加上一些自己的功能,如:BeforeProceed和AfterProceed,即不修改UserProcessor类又能增加新功能 定义1个用户接口 ...

  7. MySQL yum 在线与本地包方式安装

     以下实践操作是在 liux-centos7 上安装配置 先检测是否安装mysql,然后在拆卸mysql # 检测[root@localhost ~]# yum list installed | gr ...

  8. 21备忘录模式Memento

    一.什么是备忘录模式 Memento模式也叫备忘录模式,是行为模式之 一,它的作用是保存对象的内部状态,并在需要 的时候(undo/rollback)恢复对象以前的状态. 二.备忘录模式的应用场景 如 ...

  9. Windows10 + IntelliJ IDEA 2017.3.2 + wamp2e + Yii + PHPunit 搭建测试环境

    一.环境 系统: windows10 WampServer:  wampserver2.2e-php5.3.13-httpd2.2.22-mysql5.5.24-32b.exe IDE:  Intel ...

  10. CocoaPods更新过程中出现的坑及解决方法

    如果CocoaPods很久没有更新,那么在search.install.update,都会强制升级他的repo,然后如果安装他正常的安装程序,从github上下的时候,你会发现,这个过程不管你FQ不F ...