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的更多相关文章

  1. Spring源码窥探之:注解方式的AOP原理

    AOP入口代码分析 通过注解的方式来实现AOP1. @EnableAspectJAutoProxy通过@Import注解向容器中注入了AspectJAutoProxyRegistrar这个类,而它在容 ...

  2. Spring源码窥探之:声明式事务

    1. 导入驱动,连接池,jdbc和AOP的依赖 <!-- c3p0数据库连接池 --> <dependency> <groupId>c3p0</groupId ...

  3. Spring源码窥探之:AOP注解

    AOP也就是我们日常说的@面向切面编程,看概念比较晦涩难懂,难懂的是设计理念,以及这样设计的好处是什么.在Spring的AOP中,常用的几个注解如下:@Aspect,@Before,@After,@A ...

  4. Spring源码窥探之:@Profile

    Spring为我们提供的多环境启动 1. 配置类,注入三个不同环境的数据源,并加上注解 /** * description: 以下准备了三套不同环境的数据源 * * @author 70KG * @d ...

  5. Spring源码窥探之:Spring AOP初步使用

    AOP即面向切面编程.它的底层实际是用了spring的动态代理,具体是JDK的代理还是CGLIB的代理,就视情况而定了.本博客园仅仅作为平时记录,显得有些杂乱无章,如果想了解动态代理,设计模式,请访问 ...

  6. Spring源码窥探之:BeanPostProcessor

    Spring的Bean后置处理器 1. 实体类 /** * @author 70KG * @Title: Train * @Description: * @date 2018/7/23下午11:31 ...

  7. Spring源码窥探之:Spring AOP初步

    AOP(Aspect Oriented Programming):即我们常说的面向切面编程. 什么是AOP?AOP是在我们原来写的代码的基础上,进行一定的包装,比如在方法执行前.方法返回后.方法抛出异 ...

  8. Spring源码窥探之:单实例Bean的创建过程

    finishBeanFactoryInitialization(beanFactory);初始化剩下的所有的单实例(非懒加载)Bean(Instantiate all remaining (non-l ...

  9. Spring源码窥探之:扩展原理BeanDefinitionRegistryPostProcessor

    BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProcessor,其中有两个接口,postProcessBeanDefinitionRegi ...

随机推荐

  1. QT5 添加resource的方法

    在工程中右键点击xxx.qrc文件,选择"open in editor",然后可以用"Add ""Remove"按钮进行资源文件的添加.

  2. Jetson TX2

    NVIDIA Jetson TX2作为一个嵌入式平台的深度学习端,具备不错的GPU性能,可以发现TX2的GPU的计算能力是6.2.这意味着TX2对半精度运算有着良好的支持,因此,完全可以在桌面端训练好 ...

  3. [转帖]直击案发现场!TCP 10倍延迟的真相是?

    直击案发现场!TCP 10倍延迟的真相是? http://zhuanlan.51cto.com/art/201911/605268.htm 内核参数调优 非常重要啊. 什么是经验?就是遇到问题,解决问 ...

  4. Spring笔记4

    Spring中的JdbcTemplate JdbcTemplate:他是spring框架中提供的一个对象,是对原始Jdbc API对象的简单封装. JdbcTemplate的作用:用于和数据库交互的, ...

  5. PB 奇葩BUG

    1. 在数据窗口的Edit 中选则CHeckBox 后,必须给默认值,否则会导致数据保存不了,也无提示. 2.当使用数据窗口的 setfile()函数时 如果 条件中有两个LIKE 则会报错,一个LI ...

  6. 阿里云最新Maven仓库地址 从此 我的maven依赖下载666~

    配置指南 maven配置指南 打开maven的配置文件(windows机器一般在maven安装目录的conf/settings.xml),在<mirrors></mirrors> ...

  7. ESP8266 智能家居简单实现

    本文转自CSDN,地址 https://blog.csdn.net/jsagacity/article/details/78531819 全文如下 : 前段时间,公司利用 ESP8266 这个WiFi ...

  8. Unity UnityWebRequest实现与后端的交互

    一般我们与后端对接的时候会用到UnityWebRequest这里简单使用这个与后端进行交互这个是总类 using UnityEngine;using System.Collections;using ...

  9. springboot笔记06——使用Thymeleaf模板引擎

    前言 Springboot 推荐使用Thymeleaf做视图层.Thymeleaf支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式.浏览器解释 html 时会忽略 ...

  10. PHP利用preg_split函数格式化日期

    // 2018/2/13 00:26:00.000 function format_date($date, $format='Y-m-d H:i:s') { if(empty($date)) retu ...