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 环境搭建jenkins服务

    1.下载jenkins war包 https://jenkins.io/download/ 选择Generic Java package (.war)下载2.下载apache tomcat 8 htt ...

  2. There is a chart instance already initialized on the dom!警告

    使用Echarts插件的时候,多次加载会出现There is a chart instance already initialized on the dom.这个错误,改插件已经加载完成. 并且如果你 ...

  3. Atitit s2018.2 s2 doc list on home ntpc.docx  \Atiitt uke制度体系 法律 法规 规章 条例 国王诏书.docx \Atiitt 手写文字识别 讯飞科大 语音云.docx \Atitit 代码托管与虚拟主机.docx \Atitit 企业文化 每日心灵 鸡汤 值班 发布.docx \Atitit 几大研发体系对比 Stage-Gat

    Atitit s2018.2 s2 doc list on home ntpc.docx \Atiitt uke制度体系  法律 法规 规章 条例 国王诏书.docx \Atiitt 手写文字识别   ...

  4. Resnet BN

    [深度学习]深入理解Batch Normalization批标准化 https://www.zhihu.com/topic/20084849/hot resnet(残差网络)的F(x)究竟长什么样子? ...

  5. Sublime Text 输入法跟随光标

    通过PackageControl安装“IMESupport”,重启Sublime Text3,即可解决:  注:如项目自述,仅支持Windows.

  6. tensorflow 笔记14:tf.expand_dims和tf.squeeze函数

    tf.expand_dims和tf.squeeze函数 一.tf.expand_dims() Function tf.expand_dims(input, axis=None, name=None, ...

  7. 【Git】git pull和git pull --rebase的使用

    git pull = git fetch + git mergegit pull --rebase = git fetch + git rebase 现在来看看git merge和git rebase ...

  8. mysql主从复制延迟问题的相关知识与解决方案

    一.如何监控发生了主从延迟? 在从库机器上,执行show slave status,查看Seconds_Behind_Master值,代表主从同步从库落后主库的时间,单位为秒,若同从同步无延迟,这个值 ...

  9. KL divergence

    Kullback-Leibler divergence 形式: 性质: 非负 P=Q时,D[P||Q]=0 不对称性:D(P||Q)≠D(Q||P) 自信息:符合分布 P 的某一事件 x 出现,传达这 ...

  10. 删除SQL架构的用户

    ALTER AUTHORIZATION ON SCHEMA::db_owner TO db_owner