这里的注解是指@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 注解的方式实现避免注入外部属性硬代码化的更多相关文章

  1. spring in action 学习十一:property placeholder Xml方式实现避免注入外部属性硬代码化

    这里用到了placeholder特有的一个语言或者将表达形式:${},spring in action 描述如下: In spring wiring ,placeholder values are p ...

  2. spring in action 学习笔记二:aop的理解

    一: aop的思想的来在哪里? 一个系统一般情况下由多个组件组成,而每一个组件除了干自己的本职工作以外,有时还会干一些杂活(如:日志(logging).事务管理(transaction manager ...

  3. Spring in Action 学习笔记二-DI

    装配bean 2015年10月9日 9:49             Sprng中,对象无需自己负责查找或创建其关联的其他对象.相关,容器负责吧需要相互协作的对象引用赋予各个对象. 创建应用对象之间协 ...

  4. spring cloud深入学习(十二)-----Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式

    Zuul的核心 Filter是Zuul的核心,用来实现对外服务的控制.Filter的生命周期有4个,分别是“PRE”.“ROUTING”.“POST”.“ERROR”,整个生命周期可以用下图来表示. ...

  5. spring in action学习笔记七:@Conditional注解的用法

    @Profile注解是@Conditional注解的一个例子.即@Profile也是用@Conditional注解来实现的. 必须让条件实现Condition这个接口. 下面的案例讲如果环境中有mag ...

  6. (转)SpringMVC学习(十二)——SpringMVC中的拦截器

    http://blog.csdn.net/yerenyuan_pku/article/details/72567761 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter, ...

  7. Spring Boot 2.X(十二):定时任务

    简介 定时任务是后端开发中常见的需求,主要应用场景有定期数据报表.定时消息通知.异步的后台业务逻辑处理.日志分析处理.垃圾数据清理.定时更新缓存等等. Spring Boot 集成了一整套的定时任务工 ...

  8. 《Spring Cloud》学习(二) 负载均衡!

    第二章 负载均衡 负载均衡是对系统的高可用.网络压力的缓解和处理能力扩容的重要手段之一.Spring Cloud Ribbon是一个基于 HTTP 和 TCP 的客户端负载均衡工具,它基于Netfli ...

  9. spring in action 学习笔记十三:SpEL语言(Spring Expression Language)

    SpEl语言的目的之一是防止注入外部属性的代码硬代码化.如@Value("#{student.name}")这个注解的意思是把Student类的name的属性值注入进去.其中stu ...

随机推荐

  1. mac上配置java jdk环境

    访问Oracle官网 http://www.oracle.com,浏览到首页的底部菜单 ,然后按下图提示操作: 2.点击“JDK DOWNLOAD”按钮: 3.选择“Accept Lisence Ag ...

  2. centOS初了解--***安装node

    在***买了一个VPS,用了差不多一年了,除了做FQ使用之外,同时也下载了一个node,用了express搭建了一个服务,同时我在博客园有博客,我也懒得转来转去了,直接做了一个重定向,跳转到了博客园. ...

  3. Struts2之类范围拦截器和方法拦截器

    1.Struts2拦截器的体系结构 Struts2拦截器最大的特点是其透明性,即用户感觉不到它的存在,但我们在使用Struts2框架时,拦截器时时刻刻都在帮助我们处理很多事情. 包括: 文件上传 表单 ...

  4. php-5.6.26源代码 - opcode列表

    文件 php-5.6.26/Zend/zend_vm_opcodes.h #ifndef ZEND_VM_OPCODES_H #define ZEND_VM_OPCODES_H BEGIN_EXTER ...

  5. 微信小程序禁止下拉_解决小程序下拉出现空白的情况

    微信小程序禁止下拉 在微信小程序中,用力往下拉动,页面顶部会出现一段空白的地方. 产品的需求不太允许这么做,会影响用户体验,查看文档发现可以使用enablePullDownRefresh这属性来实现, ...

  6. iar注释快捷键

    选中多行后注释快捷键:Ctrl+K 取消多行注释快捷键:Ctrl+Shift+K

  7. C++基础 const

    1. C中的const C中const变量只是只读变量,有自己存储空间.可能被存放在 栈.堆.数据段,所以可以修改. 2. C++中const 可能分配空间,也可能不分配空间. 当 const 为全局 ...

  8. POJ:2456-Aggressive cows

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18313 Accepted: 8716 Desc ...

  9. CFileDialog OFN_NOCHANGEDIR

    问题:CFileDialog 调用后变成了当前工作路径,变成了CFileDialog所选择的路径. 解决:在CFileDialog的dwFlags 设置标志OFN_NOCHANGEDIR就可以了,不会 ...

  10. 【Kth Smallest Element in a BST 】cpp

    题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. ...