Depending on the spring configuration file Propertiesfactorybean and Propertyplaceholderconfigurer can choose different loading methods, I am using System.setproperty (key, value), The value can be taken directly from the code using System.getproperty (key)
First, Propertyplaceholderconfigurer
Propertyplaceholderconfigurer is the solution to the properties file placeholder problem, implementing the Propertiesloadersupport class
<bean class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" ><property name= " Locations "><list><value>classpath:jdbc.properties</value><value>classpath: redis.properties</value></list></property><!--Ignore unresolved--><property name= " Ignoreunresolvableplaceholders "value=" true "/></bean>
Package Com.phil.common.prop;import Java.util.map.entry;import Java.util.properties;import Org.apache.log4j.Logger ; Import Org.springframework.beans.factory.initializingbean;import org.springframework.stereotype.component;/** * Load Properties File * @author Phil * @date August 13, 2017 * */@Componentpublic class Initializingproperties implements Initializing Bean {private static final Logger Logger = Logger.getlogger (Initializingproperties.class);/* * (NON-JAVADOC) * * @see * o Rg.springframework.beans.factory.initializingbean#afterpropertiesset () */@Overridepublic void Afterpropertiesset ( ) throws Exception {Properties props = new Properties ();p rops.load (InitializingProperties.class.getClassLoader (). getResourceAsStream ("Test1properties"));p Rops.load (InitializingProperties.class.getClassLoader (). getResourceAsStream ("test2.properties")); for (Entry<object, object> e:props.entryset ()) {System.setProperty ( E.getkey (). ToString (), E.getvalue (). ToString ()), Logger.info (E.getkey (). ToString () + "---"+ E.getvalue (). toString ());}}} Second, Propertiesfactorybean
Propertiesfactorybean is a direct implementation class of Propertiesloadersupport
<bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertiesFactoryBean" >< Property name= "Location" ><value>classpath:portal_dev.properties</value></property></ Bean>
Package Com.phil.common.prop;import Java.util.map.entry;import Java.util.properties;import Org.apache.log4j.Logger ; Import Org.springframework.beans.factory.initializingbean;import Org.springframework.beans.factory.annotation.autowired;import org.springframework.stereotype.component;/** * Load Properties File * @author Phil * @date August 13, 2017 * */@Componentpublic class Initializingproperties implements Initializing Bean {private static final Logger Logger = Logger.getlogger (initializingproperties.class); @Autowiredprivate Properties propertyconfigurer;/* * (non-javadoc) * * @see * org.springframework.beans.factory.initializingbean# Afterpropertiesset () */@Overridepublic void Afterpropertiesset () throws Exception {for (Entry<object, object> e:p Ropertyconfigurer.entryset ()) {System.setproperty (E.getkey (). ToString (), E.getvalue (). ToString ()); Logger.info ( E.getkey (). ToString () + "---" + e.getvalue (). ToString ());}}}
Java reads the properties file (non-flooding)