spring 中可以在import 的filename中使用变量
<import resource="camel-context-routes.${username}xml"/>
http://blog.csdn.net/kongxx/article/details/5842036
前一篇文章说了关于spring中PropertyPlaceholderConfigurer类的使用http://blog.csdn.net/kongxx/archive/2010/08/26/5842009.aspx
但是在有些情况下我们的属性并不是配置在properties文件中,而是通过Java启动时的-Dname=value参数设置在java系统环境中,此时如果在java里我们可以使用System.getProperty(name)来获取属性值,而在spring里我们就可以通过PropertyPlaceholderConfigurer类来获取。
1. 首先创建一个Java Bean
- package test;
- import org.apache.commons.lang.builder.ToStringBuilder;
- public class MyBean {
- private String name;
- private String prop1;
- private String prop2;
- private String prop3;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getProp1() {
- return prop1;
- }
- public void setProp1(String prop1) {
- this.prop1 = prop1;
- }
- public String getProp2() {
- return prop2;
- }
- public void setProp2(String prop2) {
- this.prop2 = prop2;
- }
- public String getProp3() {
- return prop3;
- }
- public void setProp3(String prop3) {
- this.prop3 = prop3;
- }
- @Override
- public String toString() {
- return ToStringBuilder.reflectionToString(this);
- }
- }
2. 创建spring.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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
- default-lazy-init="true">
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <value>classpath:test/spring.properties</value>
- </property>
- <property name="systemPropertiesMode">
- <value>1</value>
- </property>
- <property name="searchSystemEnvironment">
- <value>true</value>
- </property>
- <property name="ignoreUnresolvablePlaceholders">
- <value>true</value>
- </property>
- </bean>
- <bean id="myBean" class="test.MyBean">
- <property name="name"><value>${name}</value></property>
- <property name="prop1"><value>${prop1}</value></property>
- <property name="prop2"><value>${prop2}</value></property>
- <property name="prop3"><value>${prop3}</value></property>
- </bean>
- </beans>
配置文件中使用${name},${propx}来说明需要使用properties文件中的内容替换
3. 创建spring.properties文件,这里变量可以递归引用当前properties文件中定义的别的变量
- name=kongxx
- prop1=111
- prop2=${prop1}222
- prop3=${prop2}333
4. 写一个测试程序
- package test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Test {
- public static void main(String[] args) {
- System.setProperty("name", "Mandy");
- System.setProperty("prop1", "111");
- System.setProperty("prop2", "222");
- System.setProperty("prop3", "333");
- ApplicationContext ctx = new ClassPathXmlApplicationContext(
- "/test/spring.xml");
- MyBean myBean = (MyBean) ctx.getBean("myBean");
- System.out.println(myBean);
- }
- }
这里去我在启动前通过System.setProperty(key)来模拟java中通过-D传递参数的情况。运行测试程序,输出如下:
test.MyBean@1649b44[name=kongxx,prop1=111,prop2=222,prop3=333]
这里其实spring是忽略的properties文件里的配置而使用的系统环境中的值。