Spring loads the Properties property file into memory

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.