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 ...
随机推荐
- 如何使用Three.js加载obj和mtl文件
OBJ和MTL是3D模型的几何模型文件和材料文件. 在最新的three.js版本(r78)中,以前的OBJMTLLoader类已废弃. 现在要加载OBJ和MTL文件,需要结合OBJLoader和MTL ...
- L26 使用卷积及循环神经网络进行文本分类
文本情感分类 文本分类是自然语言处理的一个常见任务,它把一段不定长的文本序列变换为文本的类别.本节关注它的一个子问题:使用文本情感分类来分析文本作者的情绪.这个问题也叫情感分析,并有着广泛的应用. 同 ...
- 关于Python+selenium 定位浏览器弹窗元素
首先要确定弹窗的类型: (1)div弹窗 (2)新标签页弹窗 (3)alert弹窗 一,div弹窗div弹窗是浏览器中比较好定位的弹窗,定位的方法与普通的元素一样.不过这里会有一个坑,明明可以找到这个 ...
- [Laravel框架学习二]:Laravel的CURD和查询构造器的CURD,以及聚合函数
public function index() { //return Member::getMember();//这是调用模型的方法 return view('lpc',[ 'age'=>18, ...
- 使用dynamic和MEF实现轻量级的AOP组件 (3)
转摘 https://www.cnblogs.com/niceWk/archive/2010/07/22/1783068.html 水到渠成 在上一篇的<偷梁换柱>中,介绍了Weavabl ...
- python face_recognition模块实现人脸识别
import face_recognition #人脸识别库 pip cmake dlib import cv2 #读取图像 face_image1 = face_recognition.load_i ...
- nignx location index的用法
来源:https://blog.csdn.net/qq_32331073/article/details/81945134#_10 index指令的作用 在前后端分离的基础上,通过Nginx配置,指定 ...
- tp5 -- join注意事项
使用数据库关联查询的时候,有时候会避免不了两个表格字段名称都一样的尴尬, 这时候管理查询出来的只有其中一个表格字段名称的数据,因为在相同字段名称的情况下,数据会自动覆盖. 这时候,我们只需要给其中一个 ...
- tp5中“前置操作”和“钩子函数”的区别
1.实行顺序: 以下都是标为删除前的操作: 点击删除 -> 前置操作 -> 删除方法(用模型删除) -> 触发钩子函数(删除) -> 删除成功 2.传入的参数: ...
- Selenium常见报错问题(2)- 解决和分析StaleElementReferenceException异常
如果你在跑selenium脚本时,需要某些异常不知道怎么解决时,可以看看这一系列的文章,看看有没有你需要的答案 https://www.cnblogs.com/poloyy/category/1749 ...