spring in action 学习十二:property placeholder 注解的方式实现避免注入外部属性硬代码化
这里的注解是指@PropertySource这个注解。用@PropertySource这个注解加载.properties文件。
案例的目录结构如下:

student.properties的代码如下:
#用配置文件的形式,避免注入属性值的硬代码化。
name=AbrahamLincoln
age=21
Student的代码如下:
package com.advancedWiring.ambiguityIniAutowiring2; import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Component
public class Student implements BeanNameAware{
private String name;
private Integer age; public String getName() {
return name;
}
@Value("${name}")//注入值
public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
}
@Value("${age}")//注入值
public void setAge(Integer age) {
this.age = age;
} @Override
public void setBeanName(String name) {
System.out.println("the Student bean name is :"+name);
} /**
* write this constructor mainly to inject external property value.
* @param name the student's name
* @param age the student's age
*/
public Student( String name, Integer age) {
this.name = name;
this.age = age;
} public Student() {
}
}
StudentConfig的代码如下:
package com.advancedWiring.ambiguityIniAutowiring2; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.*;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Configuration
/**
* @PropertySource主要是加载.properties文件。
*/
@ComponentScan
@PropertySource("classpath:com/advancedWiring/ambiguityIniAutowiring2/student.properties")
public class StudentConfig { }
测试类的代码如下:
package com.advancedWiring.ambiguityIniAutowiring2; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; /**
* Created by ${秦林森} on 2017/6/9.
*/
public class Test {
public static void main(String[] args) { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(StudentConfig.class);
Student student = ac.getBean(Student.class);
/**
* 输出结果是:the student name is: AbrahamLincoln and age is :21
* 可以看出他把配置文件的值给取出来了。
*/
System.out.println("the student name is: "+student.getName()+" and age is :"+student.getAge());
}
}
spring in action 学习十二:property placeholder 注解的方式实现避免注入外部属性硬代码化的更多相关文章
- spring in action 学习十一:property placeholder Xml方式实现避免注入外部属性硬代码化
这里用到了placeholder特有的一个语言或者将表达形式:${},spring in action 描述如下: In spring wiring ,placeholder values are p ...
- spring in action 学习笔记二:aop的理解
一: aop的思想的来在哪里? 一个系统一般情况下由多个组件组成,而每一个组件除了干自己的本职工作以外,有时还会干一些杂活(如:日志(logging).事务管理(transaction manager ...
- Spring in Action 学习笔记二-DI
装配bean 2015年10月9日 9:49 Sprng中,对象无需自己负责查找或创建其关联的其他对象.相关,容器负责吧需要相互协作的对象引用赋予各个对象. 创建应用对象之间协 ...
- spring cloud深入学习(十二)-----Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式
Zuul的核心 Filter是Zuul的核心,用来实现对外服务的控制.Filter的生命周期有4个,分别是“PRE”.“ROUTING”.“POST”.“ERROR”,整个生命周期可以用下图来表示. ...
- spring in action学习笔记七:@Conditional注解的用法
@Profile注解是@Conditional注解的一个例子.即@Profile也是用@Conditional注解来实现的. 必须让条件实现Condition这个接口. 下面的案例讲如果环境中有mag ...
- (转)SpringMVC学习(十二)——SpringMVC中的拦截器
http://blog.csdn.net/yerenyuan_pku/article/details/72567761 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter, ...
- Spring Boot 2.X(十二):定时任务
简介 定时任务是后端开发中常见的需求,主要应用场景有定期数据报表.定时消息通知.异步的后台业务逻辑处理.日志分析处理.垃圾数据清理.定时更新缓存等等. Spring Boot 集成了一整套的定时任务工 ...
- 《Spring Cloud》学习(二) 负载均衡!
第二章 负载均衡 负载均衡是对系统的高可用.网络压力的缓解和处理能力扩容的重要手段之一.Spring Cloud Ribbon是一个基于 HTTP 和 TCP 的客户端负载均衡工具,它基于Netfli ...
- spring in action 学习笔记十三:SpEL语言(Spring Expression Language)
SpEl语言的目的之一是防止注入外部属性的代码硬代码化.如@Value("#{student.name}")这个注解的意思是把Student类的name的属性值注入进去.其中stu ...
随机推荐
- Guava Cache 工具类 [ GuavaCacheUtil ]
pom.xml <dependency> <groupId>com.google.guava</groupId> <artifactId>guava&l ...
- Android和IOS网页不一致汇总
1.input type=text 内容输入框的不一致,ios会默认给输入框添加自己的样式,导致在横向的输入框长度精准控制的时候,ios的输入框一般都比android上要长一点,还有内部阴影 解决此问 ...
- JavaScript对象回收机制
js维护了一张对象引用表: 当一个对象被创建以后,栈内就有一个a,a这个对象就指向了对这个地址,当a=new Person()执行后,引用次数加1.当a=null置空,引用次数减1.由系统来维护对象引 ...
- 4W条人才表循环处理业务sql优化过程
场景: 使用windows服务定时更新合同数据:执行存储过程(pas_RefreshContractStatus),但存储过程里面有一个需要更新4W条人才表循环处理业务 问题: 循环更新4W条人才表状 ...
- Redis 持久化操作
hash类型 类比:mysql数据库存储数据 持久化操作 以本身的数据以文件形式保存到硬盘中 手动快照持久化 i 备份机制(频率) vi redis.conf save 900 1 900s如果一个 ...
- 4.1 基本类型和引用类型的值【JavaScript高级程序设计第三版】
ECMAScript 变量可能包含两种不同数据类型的值:基本类型值和引用类型值.基本类型值指的是简单的数据段,而引用类型值指那些可能由多个值构成的对象. 在将一个值赋给变量时,解析器必须确定这个值是基 ...
- 判断移动端和pc端最简单的方法
<!DOCTYPE html><html><head> <title></title> <script type="text ...
- Codeforces Round #459 (Div. 2) D. MADMAX DFS+博弈
D. MADMAX time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- Android面试收集录1 Activity+Service
1.Activity的生命周期 1.1.首先查看一下Activity生命周期经典图片. 在正常情况下,一个Activity从启动到结束会以如下顺序经历整个生命周期: onCreate()->on ...
- 11.1,nginx集群概念
集群介绍 为什么要用集群