参考文章springboot(四)——@EnableConfigurationProperties是如何起作用的你知道吗

前言

用springboot开发的过程中,我们会用到@ConfigurationProperties注解,主要是用来把properties或者yml配置文件转化为bean来使用的,而@EnableConfigurationProperties注解的作用是@ConfigurationProperties注解生效。
如果只配置@ConfigurationProperties注解,在IOC容器中是获取不到properties配置文件转化的bean的,当然在@ConfigurationProperties加入注解的类上加@Component也可以使交于springboot管理

举个栗子

第一步:创建一个类TestConfigurationProperties

@ConfigurationProperties(prefix = "properties")
public class TestConfigurationProperties {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}// 省略set和get方法
}

第二步:创建TestAutoConfiguration类

@Configuration
@EnableConfigurationProperties(TestConfigurationProperties.class) // 将TestConfigurationProperties放入IOC容器中
@ConditionalOnClass(User.class) // 类路径中有user类时,才会加载这个配置类
@ConditionalOnProperty(prefix = "test", value = "enabled", havingValue = "true") // 配置文件中有test.enable=true的配置,才会加载这个配置类
public class TestAutoConfiguration {@Autowiredprivate TestConfigurationProperties testConfigurationProperties;@Beanpublic User user(){User user = new User();user.setName(testConfigurationProperties.getName());return user;}
}

第三步:配置文件加入属性

properties.name=test
test.enable=true

第四步:跑一下,打印出User这个类

@RestController
@RequestMapping("/api/test")
@Slf4j
public class TestController {@AutowiredTestConfigurationProperties testConfigurationProperties;@AutowiredUser user;@RequestMapping(value = "/testConfigurationProperties")public String testConfigurationProperties() {log.info("test testConfigurationProperties.............{}", testConfigurationProperties.getName());log.info("user:{}", user);return "SUCCESS";}
}

输出

2019-04-21/16:11:36.638||||||||^_^|[http-nio-8088-exec-1] INFO  com.stone.zplxjj.controller.TestController 37 - test testConfigurationProperties.............test
2019-04-21/16:11:36.639||||||||^_^|[http-nio-8088-exec-1] INFO  com.stone.zplxjj.controller.TestController 38 - user:User(id=null, name=test)

注解的原理解析

参考文章springboot(四)——@EnableConfigurationProperties是如何起作用的你知道吗

springboot中的注解-EnableConfigurationProperties相关推荐

  1. SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介绍

    SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介绍 本篇博文将介绍几种如何处理url中的参数的注解@PathVaribale/@Requ ...

  2. java参数值注入_在springboot中使用注解将值注入参数的操作

    后端的许多管理系统需要登陆者的信息,如shiro登陆后,会将登陆者的信息存储在shiro的session,在使用时需要多行代码获取用户信息.可以把获取在shiro中的登陆者信息封装在一个类中,使用时获 ...

  3. springboot中得注解_Spring以及SpringBoot中的常用的注解小结

    一.注解的基本概念 Annotation(注解)就是Java提供了一种元程序中的元素关联任何信息和着任何元数据(metadata)的途径和方法.Annotion(注解)是一个接口,程序可以通过反射来获 ...

  4. SpringBoot中常见注解

    @Configuration用在类上,让Spring注入被注解的类的实例. @EnableConfigurationProperties(CustomProperty.class) 使使用@Confi ...

  5. SpringBoot中condition注解的使用

    在项目中,有时会遇到我们的Configuration.Bean.Service等等的bean组件需要依条件按需加载的情况. springboot中提供了一系列@Condition* 注解来处理有条件注 ...

  6. springboot中@scheduled注解使用备注

    参考文章: https://cloud.tencent.com/developer/article/1497610 https://blog.csdn.net/Rice_kil/article/det ...

  7. @transaction使自定义注解失效_【完美】SpringBoot中使用注解来实现 Redis 分布式锁...

    一.业务背景 有些业务请求,属于耗时操作,需要加锁,防止后续的并发操作,同时对数据库的数据进行操作,需要避免对之前的业务造成影响. 二.分析流程 使用 Redis 作为分布式锁,将锁的状态放到 Red ...

  8. SpringBoot中Configure注解和Bean注解的使用

    2019独角兽企业重金招聘Python工程师标准>>> Configure注解该类为配置类,Bean注解bean对象如下demo package com.yudian.springb ...

  9. 【完美】SpringBoot中使用注解来实现 Redis 分布式锁

    作者:jingQ https://www.sevenyuan.cn/ 一.业务背景 有些业务请求,属于耗时操作,需要加锁,防止后续的并发操作,同时对数据库的数据进行操作,需要避免对之前的业务造成影响. ...

  10. springboot中 @Imput与@EnableConfigurationProperties区别

    @Import和@EnableConfigurationProperties是Spring Boot中两个不同的注解,它们的作用分别如下: @Import注解的作用是将指定的类或配置类导入到当前类中, ...

最新文章

  1. 什么是‘YTowOnt9‘?
  2. 重构遗留代码(1):金牌大师
  3. 解决报错OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
  4. usleep延时0.毫秒_LabVIEW从0到1系列视频培训_第4讲全集_操作例程说明
  5. Servlet_urlpartten配置
  6. SAP CRM Reference类型下拉菜单里的值是从哪里取出的
  7. C3P0连接池详细配置
  8. Thinkphp 生成数据表字段缓存
  9. 系统动力学建模代做,vensim建模代做,流图建模,不确定分析。
  10. 软件安装包制作工具installshield 2020 R1的安装教程
  11. ffmpeg推拉流优化方案
  12. 集成运算放大器及其应用
  13. 国内服务器免备案教程 可使用80端口进入后台
  14. Substance Painter 画高度贴图
  15. 面试题:深拷贝和浅拷贝(超级详细,有内存图)
  16. 一点点读懂cpufreq(一)
  17. 【商业源码】生日大放送-Newlife商业源码分享
  18. apriori java_频繁模式挖掘apriori算法介绍及Java实现
  19. css3之[2D转化,动画,3D转化] 彩蛋之3D立方体
  20. react脚手架创建命令教程

热门文章

  1. VTK(五)---内窥镜漫游(基于VMTK血管中心线提取)
  2. python中fac函数_python系列-基础
  3. android图像与动画处理,在Android和iPhone上对照片进行动画处理的7种最佳应用 | MOS86...
  4. kaggle:Costa Rican Household Poverty Level Prediction(2):Base line
  5. Pandas学习笔记- DataFrame
  6. 2021-09-14基于用 户 行为 序列建模的推荐算法研究
  7. 185.部门工资前三高的员工
  8. 两个子数组和的差最小
  9. 调制方式演变(二)--DQPSK,4 /π​-DQPSK,FSK,MSK
  10. length与length()