这里用到了placeholder特有的一个语言或者将表达形式:${},spring in action 描述如下:
In spring wiring ,placeholder values are property names wrapped with ${...},as an exampl,you can resolve the constructor arguments for a BlankDisc in xml like this :
<bean id="sgtPeppers"
class="soundsystem.BlankDisc"
c:_titile="${disc.title}"
c:_artist="${disc.artist}"/>
下面这个案例用${...}从配置文件读取内容,
记得一定要在配置文件写上下面的语句:
<!-- 加载.properties文件-->
<context:property-placeholder location="classpath:com/advancedWiring/ambiguityIniAutowiring2/student.properties"/>
案例的目录结构如下图所示:
案例的代码如下:
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.
*/
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;
}
}
student.properties文件的代码如下:
#用配置文件的形式,避免注入属性值的硬代码化。
name=AbrahamLincoln
age=21
ambiguity.xml的代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<context:annotation-config/>
<!-- 加载.properties文件-->
<context:property-placeholder location="classpath:com/advancedWiring/ambiguityIniAutowiring2/student.properties"/>
<bean id="student"
class="com.advancedWiring.ambiguityIniAutowiring2.Student"
c:name="${name}"
c:age="${age}">
</bean>
</beans>
测试类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) { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("com/advancedWiring/ambiguityIniAutowiring2/ambiguity.xml");
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());
}
}