@PropertySource的写法为:@PropertySource("classpath:某个.properties文件的类路径")

首先来看一下这个案例的目录结构,重点看带红色圆圈的。如下图所示:

从上图中可以看出student.properties文件的类路径是:

com/advancedWiring/ambiguityIniAutowiring2/student.properties
student.properties的代码如下:
 #用配置文件的形式,避免注入属性值的硬代码化。
name=AbrahamLincoln
age=19

Student类的代码如下:

 package com.advancedWiring.ambiguityIniAutowiring2;

 import org.springframework.beans.factory.BeanNameAware;
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
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class Student implements BeanNameAware{
private String name;
private Integer age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return 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;
}
}
 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.core.env.Environment; /**
* Created by ${秦林森} on 2017/6/9.
*/
@Configuration
/**
* @PropertySource主要是加载.properties文件。
*/
@PropertySource("classpath:com/advancedWiring/ambiguityIniAutowiring2/student.properties")
public class StudentConfig {
@Autowired
/**
* 加载的配置文件在Environment这个接口中。
*/
Environment evn;
@Bean
public Student student(){
return new Student(evn.getProperty("name"),Integer.parseInt(evn.getProperty("age")));
}
}

测试类Test的代码如下:

 package com.advancedWiring.ambiguityIniAutowiring2;

 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* 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 :19
* 可以看出他把配置文件的值给取出来了。
*/
System.out.println("the student name is: "+student.getName()+" and age is :"+student.getAge());
}
}
 
												

spring in action 学习笔记十:用@PropertySource避免注入外部属性的值硬代码化的更多相关文章

  1. spring in action学习笔记十五:配置DispatcherServlet和ContextLoaderListener的几种方式。

    在spring in action中论述了:DispatcherServlet和ContextLoaderListener的关系,简言之就是DispatcherServlet是用于加载web层的组件的 ...

  2. spring in action 学习笔记十四:用纯注解的方式实现spring mvc

    在讲用纯注解的方式实现springmvc之前先介绍一个类:AbstractAnnotationDispatcherServletInitializer.这个类的作用是:任何一个类继承AbstractA ...

  3. spring in action学习笔记十六:配置数据源的几种方式

    第一种方式:JNDI的方式. 用xml配置的方式的代码如下: 1 <jee:jndi-lookup jndi-name="/jdbc/spittrDS" resource-r ...

  4. spring in action学习笔记一:DI(Dependency Injection)依赖注入之CI(Constructor Injection)构造器注入

    一:这里先说一下DI(Dependency Injection)依赖注入有种表现形式:一种是CI(Constructor Injection)构造方法注入,另一种是SI(Set Injection) ...

  5. Spring in Action学习笔记(1)

    Spring基础 IoC 控制反转, 也称为DI-依赖注入 一.装配bean 推荐顺序:自动装配 -> JavaConfig装配 -> XML装配 1. 自动装配 @Component 注 ...

  6. Spring in Action 学习笔记三-AOP

    面向切面的Spring 2015年10月9日 11:30             屏幕剪辑的捕获时间: 2015-10-9 14:30             屏幕剪辑的捕获时间: 2015-10-9 ...

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

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

  8. Spring in Action 学习笔记一

    Spring 核心       Spring的主要特性仅仅是 依赖注入DI和面向切面编程AOP       JavaBean 1996.12 Javav 规范针对Java定义了软件组件模型,是简单的J ...

  9. spring in action 学习笔记九:如何证明在scope为prototype时每次创建的对象不同。

    spring 中scope的值有四个:分别是:singleton.prototype.session.request.其中session和request是在web应用中的. 下面证明当scope为pr ...

随机推荐

  1. Linux下使用指定网卡进行ping操作

    目录   1. 为什么要使用知道那个网卡ping操作   2. 使用指定网卡ping操作   3. 总结 1. 为什么要使用指定网卡ping操作 现在很多服务器都拥有双网卡甚至多网卡,有些是为了保持高 ...

  2. python中enumerate函数使用

    enumerate()说明 enumerate()是python的内置函数 enumerate在字典上是枚举.列举的意思 对于一个可迭代的(iterable)/可遍历的对象(如列表.字符串),enum ...

  3. Python学习-django-Model操作

    Django之Model操作   一.字段 AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bi ...

  4. Sublime package control错误:There are no packages available for installation

    查了很多资料都没有解决. 改host---无效 复制一个文件的什么的,我看到版本比我的旧,就没有用 终于最后一个解决了.最终解决方案 解决: 更新下Package Control就好了: prefer ...

  5. POJ3682 概率DP

    King Arthur's Birthday Celebration Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3575 ...

  6. Cache、Buffer的区别

    什么是Cache?什么是Buffer?二者的区别是什么? Buffer和Cache的区别 buffer与cache操作的对象就不一样. 1.buffer(缓冲)是为了提高内存和硬盘(或其他I/O设备) ...

  7. POJ:3320-Jessica's Reading Problem(尺取法)

    Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15224 Accepted: ...

  8. 二、mysql数据库之基本操作和存储引擎

    一.知识储备 数据库服务器:一台计算机(对内存要求比较高) 数据库管理系统:如mysql,是一个软件 数据库:oldboy_stu,相当于文件夹 表:student,scholl,class_list ...

  9. 1008: [HNOI2008]越狱(计数问题)

    1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 11361  Solved: 4914[Submit][Status ...

  10. jquery跨域解决方案JSONP

    1.在互联网中我们的计算机是通过IP来定位的,但是IP比较难记忆,因此通过domain name(域名)来取代IP 2.什么是跨域? (1)默认浏览器为了安全问题,禁止了xmlhttprequest跨 ...