Spring uses Propertyplaceholderconfigurer extensions to meet parameter configurations for different environments

Source: Internet
Author: User

Spring uses Propertyplaceholderconfigurer extensions to meet parameter configurations for different environments,

From: http://www.javaarch.net/jiagoushi/548.htm


Propertyplaceholderconfigurer is spring, which provides us with the unified management of some environment variables (database connection parameters, file paths, etc.), and then specifies the corresponding variables in the bean. But often the development environment, the test environment, the configuration of these parameters of the build environment is different, then how do we use the propertyplaceholderconfigurer extension to meet the configuration requirements of different environments, without needing to modify the code or configuration in different environments.


1. We extend the propertyplaceholderconfigurer, You can use the default configuration in properties or the configuration of different environments, and then take this value of the system environment variable as our development environment, test environment, production environment Production.mode.

	Import java.io.IOException;
	Import Java.util.Map.Entry;
	Import java.util.Properties;

	Import Java.util.Set;
	Import Org.apache.commons.lang.StringUtils;
	Import Org.springframework.beans.factory.InitializingBean;
	Import Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; /** * The appropriate configuration can be enabled in different operating modes */public class Mutilpropertyplaceholderconfigurer extends Propertyplaceholderconfigure
		R implements Initializingbean {private static final String Production_mode = "Production.mode";
		Cache all property configuration private properties properties;
		/** * @return The mode/public String GetMode () {return properties.getproperty (Production_mode); } @Override Protected Properties Mergeproperties () throws IOException {Properties mergeproperties = Super.mergepro
			Perties ();
			According to the routing principle, extract the final effective properties This.properties = new properties ();
			Gets the routing rule, the System property setting mode precedence String mode = System.getproperty (Production_mode); if (Stringutils.isemPty (Mode)) {String str = mergeproperties.getproperty (Production_mode); mode = str!= null?
			STR: "ONLINE";
			} properties.put (Production_mode, MODE);
			string[] modes = Mode.split (",");
			Set<entry<object, object>> es = Mergeproperties.entryset ();
				For (Entry<object, object> entry:es) {string key = (String) entry.getkey ();
				int idx = Key.lastindexof ('_'); String Realkey = idx = = 1?
				Key:key.substring (0, IDX);
					if (!properties.containskey (Realkey)) {Object value = null;
						for (String md:modes) {value = Mergeproperties.get (Realkey + "_" + MD);
							if (value!= null) {Properties.put (Realkey, value);
						Break
						} if (value = = null) {value = Mergeproperties.get (Realkey);
						if (value!= null) {Properties.put (Realkey, value);
						else {throw new runtimeexception ("Impossible empty property for" + Realkey);
		}} return properties;
	}	/** * Open this method to the required business * * @param key * @return/public string GetProperty (string key) {return resolve
		Placeholder (key, properties);
 @Override public void Afterpropertiesset () throws Exception {//TODO auto-generated method stub}}

Then we can configure this in properties: that is, the default configuration is online production mode, so long as there is no configuration in the system variable Production.mode, then we take the online configuration, that is, the following parameters take the suffix of _online configuration.
	Production.mode=online


	#lucene Index data dir
	lucene.index.dir_dev=e:\\logs\\lucene
	lucene.index.dir_ Online=/home/admin/data

	#velocity
	file.resource.loader.cache_dev=false
	file.resource.loader.modificationcheckinterval_dev=2
	file.resource.loader.cache_online=true
	File.resource.loader.modificationcheckinterval_online=-1
The configuration for spring corresponds to:

	<!--velocity--> <import resource= "classpath*:*. xml"/> <bean "id=" Velocityconfigurer "class=" Ngframework.web.servlet.view.velocity.VelocityConfigurer "> <property name=" Resourceloaderpath "> < value>web-inf/velocity/</value> </property> <property name= "Velocityproperties" > <props&gt
				; <prop key= "Directive.foreach.counter.name" >velocityCount</prop> <prop key= " Directive.foreach.counter.initial.value ">1</prop> <prop key=" input.encoding ">GBK</prop> < Prop key= "output.encoding" >GBK</prop> <prop key= "File.resource.loader.cache" >${ file.resource.loader.cache}</prop> <prop key= "File.resource.loader.modificationCheckInterval" >${ file.resource.loader.modificationcheckinterval}</prop> <prop key= "Velocimacro.library.autoreload" > false</prop> <prop key= "Velocimacro.library" >macro.vm</prop> &LT;/PROPS&GT </property> </bean>

This parameter includes database connection string, file path, etc. can be so matched, because velocity in the test environment does not need cache, can modify that is effective, but the online environment plus cache can improve performance, so, the default use of online configuration, However, add-dproduction.mode=dev to the VM parameters in the test environment, the configuration of the _dev suffix is used in the development environment, and the online code is not changed. Very convenient.

Preferably the placeholder custom configuration bean on the rack.

	<bean id= "placeholder"
		class= "Org.springweb.core.MutilPropertyPlaceholderConfigurer" >
		< Property Name= "Locations" >
			<list>
				<value>classpath:jdbc.properties</value>
				<value>classpath*:*-placeholder.properties</value>
			</list>
		</property>
	</bean>

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.