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

代码说明:

@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. AJ学IOS 之微博项目实战(11)发送微博自定义TextView实现带占位文字

    AJ分享,必须精品 一:效果 二:代码: 由于系统自带的UITextField:和UITextView:不能满足我们的需求,所以我们需要自己设计一个. UITextField: 1.文字永远是一行,不 ...

  2. Android Them+SharedPreferences 修改程序所有view字体颜色、大小和页面背景

    有这么一个需求,可以对页面的样式进行选择,然后根据选择改变程序所有字体颜色和页面背景.同时下一次启动程序,当前设置依然有效. 根据需求,我们需要一种快速,方便,有效的方式来实现需求,然后可以通过And ...

  3. 一文回顾Reids五大对象(数据类型)

    Redis 是一个高性能的分布式内存型数据库,在国内外各大互联网公司中都有着广泛的使用,即使是一些非互联网公司中也有着非常重要的适用场景,所以对 Redis 的掌握也成为后端工程师必备的基础技能,在面 ...

  4. F - What Is Your Grade?

    “Point, point, life of student!” This is a ballad(歌谣)well known in colleges, and you must care about ...

  5. Spring IoC getBean 方法详解

    前言 本篇文章主要介绍 Spring IoC 容器 getBean() 方法. 下图是一个大致的流程图: 正文 首先定义一个简单的 POJO,如下: public class User { priva ...

  6. SQL入门,就这么简单

    随着时代的发展,人类活动产生的信息越来越多,大家常说,现在这个时代是大数据时代.在这样一个前提下,数据的存储成为我们必须要认真对待和研究的问题了.SQL(Structured Query Langua ...

  7. Windows10 下安装SourceTree 跳过注册 Bitbucket

    前提:安装Git 下载Windows系统可执行文件SourceTreeSetup-3.1.3.exe 双击安装,会出现如下界面: 关闭这个界面 C:\Users\当前用户文件夹\AppData\Loc ...

  8. Linux学习笔记(六)压缩和解压缩命令

    压缩和解压缩命令 zip unzip gzip gunzip bzip2 bunzip2 tar zip (.zip格式的压缩文件) 英文原意:package and compress (archiv ...

  9. WEBMIN(CVE-2019-15107) 学习

    简单介绍: Webmin是目前功能最强大的基于Web的Unix系统管理工具.管理员通过浏览器访问Webmin的各种管理功能并完成相应的管理动作.目前Webmin支持绝大多数的Unix系统,这些系统除了 ...

  10. DataTable 与XML 交互

    一.将DataTable的内容写入到XML文件中 /// <summary> /// 将DataTable的内容写入到XML文件中 /// </summary> /// < ...