spring学习笔记(一)@ConfigurationProperties注解
结论: 这个注解主要是为了将配置文件中的属性映射到实体类上,并且支持嵌套映射。
代码说明:
@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注解的更多相关文章
- Spring学习笔记3——使用注解的方式完成注入对象中的效果
第一步:修改applicationContext.xml 添加<context:annotation-config/>表示告诉Spring要用注解的方式进行配置 <?xml vers ...
- spring学习笔记三:Component注解(把POJO类实例化到spring的IOC容器中)
Component注解:把普通的POJO 类实例化到spring的IOC容器中,就是定义成<bean id="" class=""> 项目目录树: ...
- Spring学习笔记之依赖的注解(2)
Spring学习笔记之依赖的注解(2) 1.0 注解,不能单独存在,是Java中的一种类型 1.1 写注解 1.2 注解反射 2.0 spring的注解 spring的 @Controller@Com ...
- 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- Spring学习笔记2——表单数据验证、文件上传
在上一章节Spring学习笔记1——IOC: 尽量使用注解以及java代码中,已经搭建了项目的整体框架,介绍了IOC以及mybatis.第二节主要介绍SpringMVC中的表单数据验证以及文件上传. ...
- 不错的Spring学习笔记(转)
Spring学习笔记(1)----简单的实例 --------------------------------- 首先需要准备Spring包,可从官方网站上下载. 下载解压后,必须的两个包是s ...
- 【Spring学习笔记-MVC-9】SpringMVC数据格式化之日期转换@DateTimeFormat
作者:ssslinppp 1. 摘要 本文主要讲解Spring mvc数据格式化的具体步骤: 并讲解前台日期格式如何转换为java对象: 在之前的文章<[Spring学习笔记-MVC ...
- 【Spring学习笔记-MVC-5】利用spring MVC框架,实现ajax异步请求以及json数据的返回
作者:ssslinppp 时间:2015年5月26日 15:32:51 1. 摘要 本文讲解如何利用spring MVC框架,实现ajax异步请求以及json数据的返回. Spring MV ...
- 【Spring学习笔记-MVC-4】SpringMVC返回Json数据-方式2
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- 【Spring学习笔记-MVC-3】SpringMVC返回Json数据-方式1
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
随机推荐
- C语言小练习之学生信息管理系统
C语言小练习之学生信息管理系统 main.c文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2 ...
- AJ学IOS(23)UI之控制器管理
AJ分享,必须精品 控制器以及view的多种创建方式 控制器view的加载 通过storyboard创建 1:先加载storyboard⽂件(Test是storyboard的⽂文件名) UIStory ...
- D - Catch That Cow BFS
农夫知道一头牛的位置,想要抓住它.农夫和牛都于数轴上 ,农夫起始位于点 N(0<=N<=100000) ,牛位于点 K(0<=K<=100000) .农夫有两种移动方式: 1. ...
- BI报表分析和数据可视化,推荐这三个开源工具!
开源篇 一.Superset 1.技术架构:Python + Flask + React + Redux + SQLAlchemy 2.使用人群: (1)开发/分析人员做好看板,业务人员浏览看板数据 ...
- Linux-Discuz安装LAMP
1.下载,解压Discuz cd /data/discuz wget http://download.comsenz.com/DiscuzX/3.2/Discuz_X3.2_SC_GBK.zip un ...
- Trie树-提高海量数据的模糊查询性能
今天这篇文章源于上周在工作中解决的一个实际问题,它是个比较普遍的问题,无论做什么开发,估计都有遇到过.具体是这样的,我们有一份高校的名单(2657个),需要从海量的文章标题中找到包含这些高校的标题,其 ...
- ST3 package control
view-> showconsole (ctrl+`) import urllib.request,os,hashlib; h = 'df21e130d211cfc94d9b0905775 ...
- python学习24之异常
'''''''''1.低级错误:纯语法错误2.中级错误:代码存在隐性错误,逻辑缺陷3.高级错误:软件面对不确定性的异常错误''''''一.捕获异常1.基本异常捕获语句try: #异常捕捉语句的开始 代 ...
- 突然地心血来潮,为 MaixPy( k210 micropython ) 添加看门狗(WDT) C 模块的开发过程记录,给后来的人做开发参考。
事情是前几天群里有人说做个看门狗不难吧,5分钟的事情,然后我就怼了几句,后来才发现,原来真的没有看门狗模块鸭. 那好吧,那我就写一下好了,今天是(2020年4月30日)想着最后一天了,不如做点什么有价 ...
- 标准库hashlib模块
hashlib模块用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512, MD5 算法(都是基于hash的算法, ...