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

[java] view plaincopyprint?
  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. }

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文件

[xhtml] view plaincopyprint?
  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>

<?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文件中定义的别的变量

[java] view plaincopyprint?
  1. name=kongxx
  2. prop1=111
  3. prop2=${prop1}222
  4. prop3=${prop2}333

name=kongxx prop1=111 prop2=${prop1}222 prop3=${prop2}333

4. 写一个测试程序

[java] view plaincopyprint?
  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. }

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文件里的配置而使用的系统环境中的值。

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

  1. Linux下sed命令替换配置文件中某个变量的值(改变包含字符的一行的值)之二——只改变第一出现的那一行

    一.背景 在之前的文章中有介绍过<Linux下sed命令替换配置文件中某个变量的值(改变包含字符的一行的值)> 但是这种方法存在一定的问题,就是假如某个变量在一个文件中出现两次,却只想更改 ...

  2. Linux下sed命令替换配置文件中某个变量的值(改变包含字符的一行的值)

    背景 项目初期,创业初期,对于部署还停留在比较简单的时期等等...部署代码需要脚本部署,那么不同环境部署代码的配置也不一样,同一个环境下,多个节点的配置不一样.同一个节点中,多个服务会用到同一个配置项 ...

  3. 「Shell」- 使用环境变量,替换在文件中的变量(envsubst) @20210401

    问题描述 在部署服务时,脚本的部分内容是是动态生成的.我们可以使用 sed 进行替换,但是我们发现了更好的方法. 通过使用 envsubst 命令,可以替换在文件中的变量引用(类似于其他编程的模板引擎 ...

  4. python中的变量的引用_python中的变量引用小结

    python的变量都可以看成是内存中某个对象的引用.(变量指向该内存地址存储的值) 1.python中的可更改对象和不可更改对象 python中的对象可以分为可更改(mutable)对象与不可更改(i ...

  5. js delete删除对象属性,delete删除不了变量及原型链中的变量

    js delete删除对象属性,delete删除不了变量及原型链中的变量 一.delete删除对象属性 function fun(){this.name = 'gg';}var obj = new f ...

  6. python变量的作用_Python中的变量

    Python中的变量是用来表示一个值的标识符.变量代表了计算机内存中的一个地址.变量允许在程序中访问其他对象,调用函数或执行其他运算. 1.变量命名规则 变量是Python中的标识符,它应该遵循标识符 ...

  7. Spring PropertyPlaceholderConfigurer Usage

    在Spring里有一个PropertyPlaceholderConfigurer类,可以用来处理用一个properties文件里的内容来替换spring配置文件里使用${}的变量定义,比如有时候我们需 ...

  8. linux 变量替换字符串,shell中常用的变量处理、字符串操作(之一)

    在shell中,当我们已经获取到某个变量的变量值的时候,可能还需要对变量值进行一定的处理,才能到我们最终想要的结果,今天我们就来聊聊shell中常见的变量处理方法,我们通常会对变量进行如下处理. 一. ...

  9. 使用变量_在 Linux 中使用变量 | Linux 中国

    让我们来看看所有这些 $ 值可以告诉你什么.-- Sandra Henry-stocker 变量通常看起来像 $var 这样,但它们也有 $1.$*.$? 和 $$ 这种形式.让我们来看看所有这些 $ ...

最新文章

  1. alert在asp.net中如何使用??
  2. Matlab与线性代数--矩阵的正交分解
  3. 史上第一次,AI能够自学翻译地球上的任何语言了
  4. hdu1572 水搜索
  5. uboot中IDE移植
  6. java中那些类是线程安全的?
  7. 从键盘获取字符串,并把字符串转数字
  8. 【转】SharePoint 2010 Search Service -- 管理中心配置
  9. Linux crypto相关知识的汇总 Linux加密框架crypto对称算法和哈希算法加密模式
  10. 有机食品海报这样设计,收获了意想不到的效果…
  11. 苹果官网买的认证翻新机可靠吗?
  12. matlab 图像分块及恢复
  13. java中html在哪里找_java – 如何找出在我的servlet中推送哪个HTML按钮?
  14. SQL中 and or in的用法
  15. mysql post 注入工具类_【Mysql sql inject】POST方法BASE64编码注入write-up
  16. Gprinter Android SDK V1.0 使用说明
  17. win10远程桌面Android软件,Android端Win10远程桌面更新:支持Windows虚拟桌面
  18. 代码走查(codereview)如何执行才能提升代码质量
  19. 常见的四种EDI传输协议
  20. pythonhistogram设置_python numpy histogram用法及代码示例

热门文章

  1. App设计灵感之十二组精美的智能家居操作App设计案例
  2. 【数据结构与算法】之深入解析“复制带随机指针的链表”的求解思路与算法示例
  3. LeetCode Algorithm 1267. 统计参与通信的服务器
  4. 459. Repeated Substring Pattern 重复的子字符串
  5. Python创建单例模式的5种方法
  6. 了解单片机及单片机的控制原理和 DX516 的用法,控制一个 LED 灯的亮
  7. Dictionary Aizu - ALDS1_4_C
  8. 【Qt】modbus之串口模式写操作
  9. 【Linux】一步一步学Linux——netstat命令(166)
  10. 【Linux系统编程】进程的控制:结束进程、等待进程结束