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 ...
随机推荐
- php imagick生成gif动画的方法
>php imagick生成gif动画的方法<pre><?php$image=new Imagick();$animation = new Imagick(); //建立一个对 ...
- docker+k8s基础篇二
Docker+K8s基础篇(二) docker的资源控制 A:docker的资源限制 Kubernetes的基础篇 A:DevOps的介绍 B:Kubernetes的架构概述 C:Kubernetes ...
- 【剑指offer】面试题 31. 栈的压入、弹出序列
面试题 31. 栈的压入.弹出序列 NowCoder LeetCode 题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如 ...
- vue封装一个简单的div框选时间的组件
记录一下我前段时间封装的一个vue组件吧.技术需要积累,有时间我把我之前写的还不错的组件都开源出来.并尝试vue和react 两种方式的组件封装.今天简单写下鼠标框选div选中效果的封装吧. div框 ...
- Jenkins + GitLab + SpringBoot 实现持续集成脚本
Linux脚本 #!/bin/bash jar_name=hq-api.jar cd /usr/local/app/hq-api echo "Stopping SpringBoot Appl ...
- showModalDialog的使用方法
基本介绍: showModalDialog() (IE 4+ 支持) showModelessDialog() (IE 5+ 支持) window.showModalDial ...
- 主机与虚拟机ping不通问题
在win10 上利用vm安装linux虚拟机,物理机与虚拟机之间不能ping通,除了检查物理机与虚拟机防火墙状态外,还要注意物理机设置的ip与虚拟机设置的ip,要在同一个ip段
- 如何在textarea多行文本框中设置滚动条样式
其中设置滚动条的组成都有以下部分 ::-webkit-scrollbar 滚动条整体部分 ::-webkit-scrollbar-thumb 滚动条里面的小方块,能向上向下移动(或往左往右移动,取决 ...
- Programming Principles and Practice Using C++ Notes1
序 0.4 创造性和问题求解 首要目标是帮助你学会用代码表达你的思想2,而不是叫你如何获得这些思想.沿着这样一个思路,给出很多实列,展示如何求解问题. 我们认为程序设计本事是问题求解的一种描述形式: ...
- CSS两列布局
方法1:左边设置绝对定位,右边设置左外边距,大小和左边的宽度相等 //CSS部分: .contain{ position :relative; height: 300px; } .left{ posi ...