日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

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

發布時間:2024/4/17 windows 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

[java] view plaincopyprint?
  • 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);??
  • ????}??
  • }??
  • 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;}@Overridepublic String toString() {return ToStringBuilder.reflectionToString(this);} }?

    ?

    ?

    2. 創建spring.xml文件

    [xhtml] view plaincopyprint?
  • <?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>??
  • <?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?
  • name=kongxx??
  • prop1=111??
  • prop2=${prop1}222??
  • prop3=${prop2}333??
  • name=kongxx prop1=111 prop2=${prop1}222 prop3=${prop2}333?

    ?

    ?

    4. 寫一個測試程序

    [java] view plaincopyprint?
  • 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);??
  • ????}??
  • }??
  • 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配置文件中的变量的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。