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 ...
随机推荐
- js中break跳出多层循环
// 当执行多重循环的时候break的情况 outer: for(var i=0;i<10;i++){ inter: for(var j=0;j<10;j++){ if(i>5){ ...
- hasattr() & getattr() & setattr()
Python的hasattr() getattr() setattr() 函数使用方法详解 感谢作者 ---> 原文链接 hasattr(object, name) 判断一个对象里面是否有n ...
- Android 中的反调试技术
比较简单的有下面这两种 调试端口检测, 23946(0x5D8A) Demo: void CheckPort23946ByTcp() { FILE* pfile=NULL; char buf[0x10 ...
- 18 Django-组件拾遗
一 Django的form组件 forms组件 二 Django的model form组件 这是一个神奇的组件,通过名字我们可以看出来,这个组件的功能就是把model和form组合起来,先来一个简单的 ...
- [EXCEL]使用技巧随记
1.比对两列中是否有重复项(B列中是否和A列重复) =IF(COUNTIF(A:A,B1)=0,"不重复","重复") Excel中用vlookup函数来对比两 ...
- 非阻塞IO模板
服务端 from socke import * server = socket(AF_INET, SOCK_STREAM) server.bind(('127.0.0.1',8083)) server ...
- Java与Scala的两种简易版连接池
Java版简易版连接池: import java.sql.Connection; import java.sql.DriverManager; import java.util.LinkedList; ...
- 超轻量级异步JS框架,别再让嵌套影响我们的优雅代码!
1.异步JS的重要性 随着Web平台地位的提升,霸占着浏览器的JavaScript语言也成为了世界上最流行的语言之一,甚至通过Node.js进入了服务器编程领域.JavaScript的一个重要特性便是 ...
- 就算WORD高手也无法解释的Word的一些疑惑.,一些已经解决,一些没有解决
如下功能如何用? 1.选项->保存->显示其他保存位置(即使可能需要登录)? 解答:您能告诉我吗? 2.字体->为字体调整字间距? 解答:自动调整某些字符之前的距离,使得更加美观.例 ...
- 【Python】重定向 Stream 到文件
Python 系统模块 sys 中有三个变量 stdin . stdout 与 stderr ,分别对应标准输入流.输出流与错误流.stdin 默认指向键盘, stdout 与 stderr 默认指向 ...