Spring源码窥探之:@Value
1. 首先定义实体
/**
* @author 70KG
* @Title: Apple
* @Description: 苹果实体
* @date 2018/10/22下午9:26
* @From www.nmyswls.com
*/
@Data
public class Apple { @Value("${apple.color}")
private String color; @Value("红富士")
private String name; }
2. 属性文件test.properties放在resource下
apple.color=red
3. spring的配置类
@PropertySource(value = "classpath:/test.properties")将属性文件读取到内存中
/**
* @author 70KG
* @Title: AppleConfig
* @Description: 配置类
* @date 2018/10/22下午9:28
* @From www.nmyswls.com
*/
@Configuration
@PropertySource(value = "classpath:/test.properties")
public class AppleConfig { @Bean
public Apple apple() {
return new Apple();
} }
4. 测试
/**
* @author 70KG
* @Title: Test01
* @Description: test
* @date 2018/10/22下午9:30
* @From www.nmyswls.com
*/
public class Test01 { @Test
public void test01() {
AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(AppleConfig.class);
// 从容器中获取所有的bean
String[] names = app.getBeanDefinitionNames();
for (String name : names) {
System.out.println("bean的名字--->" + name);
}
Apple apple = (Apple) app.getBean("apple");
System.out.println(apple); System.out.println("==========IOC容器创建完成=========="); ConfigurableEnvironment environment = app.getEnvironment();
String property = environment.getProperty("apple.color");
System.out.println("获取配置文件中的属性---->" + property);
} }
5. 测试结果
bean的名字--->org.springframework.context.annotation.internalConfigurationAnnotationProcessor
bean的名字--->org.springframework.context.annotation.internalAutowiredAnnotationProcessor
bean的名字--->org.springframework.context.annotation.internalRequiredAnnotationProcessor
bean的名字--->org.springframework.context.annotation.internalCommonAnnotationProcessor
bean的名字--->org.springframework.context.event.internalEventListenerProcessor
bean的名字--->org.springframework.context.event.internalEventListenerFactory
bean的名字--->appleConfig
bean的名字--->apple
Apple(color=red, name=红富士)
==========IOC容器创建完成==========
获取配置文件中的属性---->red
前面的都是bean的后置处理器
Spring源码窥探之:@Value的更多相关文章
- Spring源码窥探之:注解方式的AOP原理
AOP入口代码分析 通过注解的方式来实现AOP1. @EnableAspectJAutoProxy通过@Import注解向容器中注入了AspectJAutoProxyRegistrar这个类,而它在容 ...
- Spring源码窥探之:声明式事务
1. 导入驱动,连接池,jdbc和AOP的依赖 <!-- c3p0数据库连接池 --> <dependency> <groupId>c3p0</groupId ...
- Spring源码窥探之:AOP注解
AOP也就是我们日常说的@面向切面编程,看概念比较晦涩难懂,难懂的是设计理念,以及这样设计的好处是什么.在Spring的AOP中,常用的几个注解如下:@Aspect,@Before,@After,@A ...
- Spring源码窥探之:@Profile
Spring为我们提供的多环境启动 1. 配置类,注入三个不同环境的数据源,并加上注解 /** * description: 以下准备了三套不同环境的数据源 * * @author 70KG * @d ...
- Spring源码窥探之:Spring AOP初步使用
AOP即面向切面编程.它的底层实际是用了spring的动态代理,具体是JDK的代理还是CGLIB的代理,就视情况而定了.本博客园仅仅作为平时记录,显得有些杂乱无章,如果想了解动态代理,设计模式,请访问 ...
- Spring源码窥探之:BeanPostProcessor
Spring的Bean后置处理器 1. 实体类 /** * @author 70KG * @Title: Train * @Description: * @date 2018/7/23下午11:31 ...
- Spring源码窥探之:Spring AOP初步
AOP(Aspect Oriented Programming):即我们常说的面向切面编程. 什么是AOP?AOP是在我们原来写的代码的基础上,进行一定的包装,比如在方法执行前.方法返回后.方法抛出异 ...
- Spring源码窥探之:单实例Bean的创建过程
finishBeanFactoryInitialization(beanFactory);初始化剩下的所有的单实例(非懒加载)Bean(Instantiate all remaining (non-l ...
- Spring源码窥探之:扩展原理BeanDefinitionRegistryPostProcessor
BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProcessor,其中有两个接口,postProcessBeanDefinitionRegi ...
随机推荐
- 问题二:appium 搞定权限弹框的一个小办法
public void permission() { for (int i=0; i <= 10; i++) { if (getPageSource().contains("允许&qu ...
- LeetCode 131. 分割回文串(Palindrome Partitioning)
131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...
- 对于并发任务,应该使用 Task 替代 BackgroundWorker
背景 EF + Oracle,并发存储监控记录,使用 BackgroundWorker 时产生错误如下: public void MonitorLogging(DateTime DateStart, ...
- C语言单链表简单实现(简单程序复杂化)
PS: goto还是很好玩的. #include <stdio.h> #include <stdlib.h> typedef struct _node{ int value; ...
- 不一样的go语言-go缺少的语言特性
相信很多从其他语言转向go语言的人,或者是几乎所有初学go语言的人(我相信目前还没有学校将go作为教学语言,所以未来的IT工程师的第一语言都不会是go),都会在学习的过程中将go与自己的第一语言. ...
- vscode 连接远程服务器 sftp
1.在vscode 应用商店搜索 sftp 下载安装 2.ctrl+shift+p 搜索sftp:config 生成sftp.json 3.配置你的服务器信息{ "name": & ...
- 【ABAP】第一章-基础
1. ABAP语法基础 1.1 基本数据类型 C.N.D.T.I.F.P.X.string.Xstring P:默认为8字节,最大允许16字节.最大整数位:16*2 = 32 - 1 = 31 -14 ...
- 2019年北航OO第4单元(UML)总结
1 架构设计 经过了接近一学期的程序设计训练,在这一单元的第一次作业中我就非常注重架构的设计,竭力避免像之前一样陷入"第一次作业凑合,第二次作业重构"的不健康的迭代模式.整体上来说 ...
- 关于ES6的对象扩展运算符
对象的扩展运算符(...),用于取出参数对象中的所有可遍历属性,然后拷贝到当前对象之中 对象扩展运算符: 1. 复制对象 let obj1 = { x: 1, y: 2, z: 3 } let obj ...
- Linux (x86) Exploit 开发系列教程之三(Off-By-One 漏洞 (基于栈))
off by one(栈)? 将源字符串复制到目标缓冲区可能会导致off by one 1.源字符串长度等于目标缓冲区长度. 当源字符串长度等于目标缓冲区长度时,单个NULL字节将被复制到目标缓冲区上 ...