1. Custompropertyconfigurer.java
package propertyconfig;import java.util.hashmap;import java.util.map;import java.util.properties;import java.util.map.entry;import org.springframework.beans.beansexception; import org.springframework.beans.factory.config.configurablelistablebeanfactory;import org.springframework.beans.factory.config.propertyplaceholderconfigurer;import org.springframework.util.propertyplaceholderhelper;public class custompropertyconfigurer Extends propertyplaceholderconfigurer{private static map<string,string> properties = new HashMap<String,String> ();p rotected void processproperties ( Configurablelistablebeanfactory beanfactorytoprocess,properties props) throws beansexception {// cache the propertiespropertyplaceholderhelper helper = New propertyplaceholderhelper (Default_placeholder_prefix, default_placeholder_suffix, deFault_value_separator, false); for (Entry<object,object> entry:props.entryset ()) {String Stringkey = string.valueof (Entry.getkey ()); String stringvalue = string.valueof (Entry.getvalue ());stringvalue = Helper.replaceplaceholders (stringvalue, props);p roperties.put (stringkey, stringvalue);} Super.processproperties (beanfactorytoprocess, props);} Public static map<string, string> getproperties () {return properties;} Public static string getproperty (String key) {return properties.get (key);}}
2. applicationcontext.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 " xmlns:p=" http://www.springframework.org/schema/p " xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xsi:schemalocation= " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd " default-lazy-init= "true" default-autowire= "ByName" default-init-method= "" Default-destroy-method= "" > <bean id= " Propertyconfigurer " class=" propertyconfig. CUstompropertyconfigurer "> <property name = "Locations" > <list > <value>classpath:propertyconfig/project.properties</value> </list> </property> </bean> </beans>
3. Project.Properties
Site=iteyeblog=antloveurl=${site}/${blog}
4. Main.java Test class
Package Propertyconfig;import Java.util.map;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Main {public static void Main (String [] args) {new Classpathxmlapplicationcontext ("Propertyconfig/applicationcontext.xml"); map<string,string> properties = Custompropertyconfigurer.getproperties (); System.out.println (properties);}}
Spring loads the Properties property file into memory