Java Read Properties file (non-flooding)

Source: Internet
Author: User

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)

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.