Spring PropertyPlaceholderConfigurer Usage - 使用系统变量替换spring配置文件中的变量

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

  1. package test;  
  2. import org.apache.commons.lang.builder.ToStringBuilder;  
  3. public class MyBean {  
  4.     private String name;  
  5.     private String prop1;  
  6.     private String prop2;  
  7.     private String prop3;  
  8.     public String getName() {  
  9.         return name;  
  10.     }  
  11.     public void setName(String name) {  
  12.         this.name = name;  
  13.     }  
  14.     public String getProp1() {  
  15.         return prop1;  
  16.     }  
  17.     public void setProp1(String prop1) {  
  18.         this.prop1 = prop1;  
  19.     }  
  20.     public String getProp2() {  
  21.         return prop2;  
  22.     }  
  23.     public void setProp2(String prop2) {  
  24.         this.prop2 = prop2;  
  25.     }  
  26.     public String getProp3() {  
  27.         return prop3;  
  28.     }  
  29.     public void setProp3(String prop3) {  
  30.         this.prop3 = prop3;  
  31.     }  
  32.     @Override  
  33.     public String toString() {  
  34.         return ToStringBuilder.reflectionToString(this);  
  35.     }  
  36. }  

 

 

 

2. 创建spring.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"  
  5.        default-lazy-init="true">  
  6.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  7.         <property name="locations">  
  8.             <value>classpath:test/spring.properties</value>  
  9.         </property>  
  10.         <property name="systemPropertiesMode">  
  11.             <value>1</value>  
  12.         </property>  
  13.         <property name="searchSystemEnvironment">  
  14.             <value>true</value>  
  15.         </property>  
  16.         <property name="ignoreUnresolvablePlaceholders">  
  17.             <value>true</value>  
  18.         </property>  
  19.     </bean>  
  20.     <bean id="myBean" class="test.MyBean">  
  21.         <property name="name"><value>${name}</value></property>  
  22.         <property name="prop1"><value>${prop1}</value></property>  
  23.         <property name="prop2"><value>${prop2}</value></property>  
  24.         <property name="prop3"><value>${prop3}</value></property>  
  25.     </bean>  
  26. </beans>  

 

配置文件中使用${name},${propx}来说明需要使用properties文件中的内容替换

 

3. 创建spring.properties文件,这里变量可以递归引用当前properties文件中定义的别的变量

  1. name=kongxx  
  2. prop1=111  
  3. prop2=${prop1}222  
  4. prop3=${prop2}333  

 

 

 

4. 写一个测试程序

  1. package test;  
  2. import org.springframework.context.ApplicationContext;  
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4. public class Test {  
  5.     public static void main(String[] args) {  
  6.         System.setProperty("name""Mandy");  
  7.         System.setProperty("prop1""111");  
  8.         System.setProperty("prop2""222");  
  9.         System.setProperty("prop3""333");  
  10.         ApplicationContext ctx = new ClassPathXmlApplicationContext(  
  11.                 "/test/spring.xml");  
  12.         MyBean myBean = (MyBean) ctx.getBean("myBean");  
  13.         System.out.println(myBean);  
  14.     }  
  15. }  

 

这里去我在启动前通过System.setProperty(key)来模拟java中通过-D传递参数的情况。运行测试程序,输出如下:

test.MyBean@1649b44[name=kongxx,prop1=111,prop2=222,prop3=333]

这里其实spring是忽略的properties文件里的配置而使用的系统环境中的值。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot,可以通过继承PropertyPlaceholderConfigurer类来获取配置文件的配置项。 首先,创建一个类,继承自PropertyPlaceholderConfigurer,重写processProperties方法,该方法在Spring应用程序上下文启动时会被调用,可以在该方法获取配置文件的属性值,例如: ```java public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private Properties properties; public MyPropertyPlaceholderConfigurer() { this.properties = new Properties(); } @Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { super.processProperties(beanFactoryToProcess, props); this.properties.putAll(props); } public String getProperty(String key) { return this.properties.getProperty(key); } } ``` 在该类,我们重写了processProperties方法,并在该方法配置文件的属性值保存到一个Properties对象。然后,提供了一个getProperty方法,可以根据属性名获取属性值。 接下来,在Spring Boot启动类,注入该类的实例,并通过该实例获取配置文件的属性值,例如: ```java @SpringBootApplication public class MyApp { @Autowired private MyPropertyPlaceholderConfigurer propertyConfigurer; public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } @PostConstruct public void init() { String propertyValue = propertyConfigurer.getProperty("my.property"); System.out.println("my.property value is: " + propertyValue); } } ``` 在上述代码,我们通过@Autowired注入了MyPropertyPlaceholderConfigurer实例,并在init方法,获取了配置文件的属性值,并打印出来。 在application.yml或application.properties,可以定义my.property属性值,例如: ```yaml my.property: my custom property value ``` 这样,当应用启动时,就会打印出"my.property value is: my custom property value"。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值