Spring PropertyPlaceholderConfigurer Usage

來源:互聯網
上載者:User

在Spring裡有一個PropertyPlaceholderConfigurer類,可以用來處理用一個properties檔案裡的內容來替換spring設定檔裡使用${}的變數定義,比如有時候我們需要把對數控庫的配置資訊在別的properties檔案裡。

 

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) { ApplicationContext ctx = new ClassPathXmlApplicationContext( "/test/spring.xml"); MyBean myBean = (MyBean) ctx.getBean("myBean"); System.out.println(myBean); } } 

運行測試程式,輸出如下:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.