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. mongodb配置文件解说(转载)

    启动方式 ./bin/mongod -f mongodb.conf 会看到 about to fork child process, waiting until server is ready for ...

  2. odoo jQuery is not defined

    The steps1.Query your db as this query.select id, create_date, store_fname, datas_fname from ir_atta ...

  3. python中MetaClass的一些用法

    元类在很多编程语言中都有这样的概念,我们都知道,类可以创建对象,类本身也是对象,既然是对象,那么它肯定也是被创造出来的,元类就专门用来创造类对象,于是,这就给我们提供了一种操纵或者监听类的能力. 平时 ...

  4. window/linux composer安装/卸载

    packagist库:https://packagist.org/ window  安装 参考地址:https://www.kancloud.cn/thinkphp/composer/35668 1. ...

  5. 【2019年04月22日】A股最便宜的股票

      太钢不锈(SZ000825) - 当前便宜指数:170.67 - 滚动扣非市盈率PE:4.37 - 滚动市净率PB:0.98 - 动态年化股息收益率:4.79%- 太钢不锈(SZ000825)的历 ...

  6. Nginx 反向代理获取设备真实的IP地址

    package com.das.common.util; import org.apache.commons.lang3.StringUtils; import org.springframework ...

  7. FastJSON 设置默认参数,全局配置方式 和 一些使用方式、坑

    如果要被序列化的对象含有一个date属性或者多个date属性按照相同的格式序列化日期的话,那我们可以使用下面的语句实现: 在应用的的Main方法体里配置全局参数: JSONObject.DEFFAUL ...

  8. 关于Kafka broker IO的讨论

    Apache Kafka是大量使用磁盘和页缓存(page cache)的,特别是对page cache的应用被视为是Kafka实现高吞吐量的重要因素之一.实际场景中用户调整page cache的手段并 ...

  9. WPS 关闭 wpscenter.exe 服务

    1.Ctrl + Shift + Esc 打开任务管理,结束wps相关的进程 2.新建文本文件,并命名为:wpscenter.exe 3.重命名 C:\Program Files1\WPS Offic ...

  10. [原]Jenkins(十七) jenkins再出发之配置SVN

    创建一个demo project 配置SVN: 配置build project