2007-03-30
Spring属性占位符PropertyPlaceholderConfigurer的使用
PropertyPlaceholderConfigurer可以将${...}替换为指定的properties文件或system properties中的值
这样一来,我们就可以不再配置文件中为bean注入数值,而改用properties文件,可以降低耦合性,以后如果需要修改诸如数值,只修改properties文件就可以了
实体类:
package Bean.propertyone;




public class Chinese ...{
private String name;
private String age;
public String getAge() ...{
return age;
}
public void setAge(String age) ...{
this.age = age;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}

}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="propertyConfigure" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:Bean/propertyone/person.properties">
</property>
</bean>
<bean id="chinese" class="Bean.propertyone.Chinese">
<property name="age" value="${person.age}"></property>
<property name="name" value="${person.name}"></property>
</bean>
</beans>
properties文件:
person.age=26
person.name=gaoxiang
测试代码:

public static void main(String[] args) throws Exception ...{
String path=new Test().getClass().getResource("/").getPath();
String realpath=path.substring(1, path.length());
ApplicationContext context=new FileSystemXmlApplicationContext(realpath+"/propertyone.xml");
Chinese p=(Chinese)context.getBean("chinese");
System.out.println(p.getName()+p.getAge());
}
运行结果:
gaoxiang26
发表评论
- 浏览: 84446 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
Static初始化代码快访问st ...
当然啦,final型常量只能被赋值一次
-- by Chihom -
Tomcat 5.5不能使用EL表达 ...
請在web.xml修改XSD<?xml version="1.0" enc ...
-- by trowa -
一个通用的泛型GenericH ...
谢谢!使用中!
-- by sangood -
Spring AOP四种创建通知( ...
我一直想知道如何拦截某个类中特定的方法,而不是所有的方法?
-- by yourgame -
HashTable和HashMap的区 ...
这么多总结,还算你的这个不错,谢谢,学习
-- by lysmart_8






评论排行榜