结论: 这个注解主要是为了将配置文件中的属性映射到实体类上,并且支持嵌套映射。

代码说明:

@ConfigurationProperties(prefix = "person")
@Data
public class Person {
String name;
Integer age;
//Dog dog;
}
@SpringBootApplication
@EnableConfigurationProperties({Person.class})
public class CustomApplication implements ApplicationRunner,ApplicationContextAware { ApplicationContext applicationContext; public static void main(String[] args) {
SpringApplication.run(CustomApplication.class, args);
} /*
*这个地方也可以直接注入Person
* @autowired
* private Person person;
*
*/
@Override
public void run(ApplicationArguments args) throws Exception {
Person bean = applicationContext.getBean(Person.class);
System.out.println(bean);
} @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
# 这是我的application.yml文件
person:
name: zhangsan
age: 12

启动chen程序后控制台打印:Person(name=zhangsan, age=12)

证明:配置中的值被映射到了实体类中。

嵌套映射:

新增实体类:

@Data
public class Dog {
private String name;
}

同时将personle类中的注解打开,我这里就不贴重复的代码了,yml文件中新增如下

person:
name: zhangsan
age: 12
dog:
name: wangwang

运行程序结果如下:

Person(name=zhangsan, age=12, dog=Dog(name=wangwang))

证明嵌套映射没有问题。

之前一直不明白为什么@ConfigurationProperties跟@EnableConfigurationProperties需要同时使用。跟踪@EnableConfigurationProperties源码发现如下代码:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
// 导入了EnableConfigurationPropertiesImportSelector,我们继续跟踪这个类
@Import({EnableConfigurationPropertiesImportSelector.class})
public @interface EnableConfigurationProperties {
Class<?>[] value() default {};
}
public static class ConfigurationPropertiesBeanRegistrar implements ImportBeanDefinitionRegistrar {
public ConfigurationPropertiesBeanRegistrar() {
} public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
this.getTypes(metadata).forEach((type) -> {
this.register(registry, (ConfigurableListableBeanFactory)registry, type);
});
}
........

其实就是将我们在@EnableConfigurationProperties({Person.class})定义的class注册到spring容器中,对于我们的案例来说,就是将person类注册到容器中。为了验证我的想法,我将@EnableConfigurationProperties注解注释,并给person类型添加了@component注解

代码如下:

@ConfigurationProperties(prefix = "person")
@Data
@Component // 新增注解
public class Person {
String name;
Integer age;
Dog dog;
} @SpringBootApplication
//@EnableConfigurationProperties({Person.class}) 去除这个注解
public class CustomApplication implements ApplicationRunner,ApplicationContextAware { ApplicationContext applicationContext; public static void main(String[] args) {
SpringApplication.run(CustomApplication.class, args);
} @Autowired
Person person; @Override
public void run(ApplicationArguments args) throws Exception {
Person bean = applicationContext.getBean(Person.class);
System.out.println(person);
} @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}

程序照样正常运行,验证正确~

spring学习笔记(一)@ConfigurationProperties注解的更多相关文章

  1. Spring学习笔记3——使用注解的方式完成注入对象中的效果

    第一步:修改applicationContext.xml 添加<context:annotation-config/>表示告诉Spring要用注解的方式进行配置 <?xml vers ...

  2. spring学习笔记三:Component注解(把POJO类实例化到spring的IOC容器中)

    Component注解:把普通的POJO 类实例化到spring的IOC容器中,就是定义成<bean id="" class=""> 项目目录树: ...

  3. Spring学习笔记之依赖的注解(2)

    Spring学习笔记之依赖的注解(2) 1.0 注解,不能单独存在,是Java中的一种类型 1.1 写注解 1.2 注解反射 2.0 spring的注解 spring的 @Controller@Com ...

  4. 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  5. Spring学习笔记2——表单数据验证、文件上传

    在上一章节Spring学习笔记1——IOC: 尽量使用注解以及java代码中,已经搭建了项目的整体框架,介绍了IOC以及mybatis.第二节主要介绍SpringMVC中的表单数据验证以及文件上传. ...

  6. 不错的Spring学习笔记(转)

    Spring学习笔记(1)----简单的实例 ---------------------------------   首先需要准备Spring包,可从官方网站上下载.   下载解压后,必须的两个包是s ...

  7. 【Spring学习笔记-MVC-9】SpringMVC数据格式化之日期转换@DateTimeFormat

    作者:ssslinppp       1. 摘要 本文主要讲解Spring mvc数据格式化的具体步骤: 并讲解前台日期格式如何转换为java对象: 在之前的文章<[Spring学习笔记-MVC ...

  8. 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回

    作者:ssslinppp      时间:2015年5月26日 15:32:51 1. 摘要 本文讲解如何利用spring MVC框架,实现ajax异步请求以及json数据的返回. Spring MV ...

  9. 【Spring学习笔记-MVC-4】SpringMVC返回Json数据-方式2

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  10. 【Spring学习笔记-MVC-3】SpringMVC返回Json数据-方式1

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

随机推荐

  1. CentOS8.1.1911正式发布!

    前阵子,CentOS官方宣布:CentOS8.1.1911正式发布!已经安装CentOS8.0的朋友,可以执行yum update更新(笔者更新了2次),体验下新版本!如是新安装,可以从官方网站下载h ...

  2. JAVA—SQL注入

    之前看到的一道java面试题,Statement与PreparedStatement的区别,什么是SQL注入,如何防止SQL注入 前面部分比较好回答 1.PreparedStatement支持动态设置 ...

  3. Cobalt Strike系列教程第六章:安装扩展

    Cobalt Strike系列教程分享如约而至,新关注的小伙伴可以先回顾一下前面的内容: Cobalt Strike系列教程第一章:简介与安装 Cobalt Strike系列教程第二章:Beacon详 ...

  4. The equation SGU - 106

    题目链接:https://codeforces.com/problemsets/acmsguru/problem/99999/106 这个题是关于EXGCD特别好的一个题目.题目大意:有一个等式ax+ ...

  5. 6. webRTC

    webrtc网上封装的很多,demo很多都是一个页面里实现的,今天实现了个完整的 , A 发视频给 B. 1.) A 方 <!DOCTYPE html> <html id=" ...

  6. 深度剖析前端JavaScript中的原型(JS的对象原型)

          这张图片有点劝退了,哈哈哈~    通过原型机制,JavaScript 中的对象从其他对象继承功能特性:这种继承机制与经典的面向对象编程语言的继承机制不同.本文将探讨这些差别,解释原型链如 ...

  7. NER命名实体识别,实体级level的评估,精确率、召回率和F1值

    pre = "0 0 B_SONG I_SONG I_SONG 0 B_SONG I_SONG I_SONG 0 0 B_SINGER I_SINGER I_SINGER 0 O O O B ...

  8. 15分钟从零开始搭建支持10w+用户的生产环境(三)

    上一篇文章介绍了这个架构中,选择MongoDB做为数据库的原因,及相关的安装操作. 原文地址:15分钟从零开始搭建支持10w+用户的生产环境(二)   三.WebServer 在SOA和gRPC大行其 ...

  9. thymeleaf 模板语法

    模板语法 如何在 script 标签体内部使用 th 获取后端数据 添加如下属性 <script type="text/javascript" th:inline=" ...

  10. python读取txt批量创建文件

    python读取txt批量创建文件 pythonbatchfile 前几天有个小问题, 需要批量建立很多文件夹,, 所以手动写了个小的脚本, 后续可以直接使用 读取目录文件, 然后直接创建相应的文件 ...