161216. load Tool class using Spring's Defaultresourceloader custom properties file

Source: Internet
Author: User

Import java.io.IOException;

Import java.io.InputStream;

Import java.util.NoSuchElementException;

Import java.util.Properties;

Import org.apache.commons.io.IOUtils;

Import org.slf4j.Logger;

Import org.slf4j.LoggerFactory;

Import org.springframework.core.io.DefaultResourceLoader;

Import org.springframework.core.io.Resource;

Import org.springframework.core.io.ResourceLoader;

/**

* The properties file is loaded into the tool class. Multiple properties files can be loaded, * The values of the same attributes in the last loaded file will overwrite the previous value, but the System's property takes Precedence.

*/

public class Propertiesloader {

private static Logger Logger = Loggerfactory.getlogger (propertiesloader.class);

private static Resourceloader Resourceloader = new Defaultresourceloader ();

Private Final Properties properties;

Public Propertiesloader (String ... resourcespaths) {

Properties = LoadProperties (resourcespaths);

}

Public Properties getProperties () {

Return properties;

}

/**

* The property is removed, but the property in system takes precedence and the return empty string is not available.

*/

private string GetValue (string Key) {

String Systemproperty = System.getproperty (key);

If (systemproperty! = Null) {

Return systemproperty;

}

If (properties.containskey (key)) {

return Properties.getproperty (key);

}

Return "";

}

/**

* Remove the property of type string, but the property of system takes precedence, if all is null throws an Exception.

*/

public string GetProperty (string Key) {

String value = GetValue (key);

if (value = = Null) {

throw new Nosuchelementexception ();

}

Return value;

}

/**

* Remove the property of type string, but the property of system takes PRECEDENCE. returns the default value if both are Null.

*/

public string GetProperty (string key, string Defaultvalue) {

String value = GetValue (key);

return value! = null? value:defaultvalue;

}

/**

* Take out the property of the integer type, but the system property takes Precedence. throws an exception if both are null or the content is Wrong.

*/

Public Integer Getinteger (String Key) {

String value = GetValue (key);

if (value = = Null) {

throw new Nosuchelementexception ();

}

return integer.valueof (value);

}

/**

* Take out the property of the integer type, but the system property takes Precedence. returns the default value if both are null and throws an exception if the content is wrong

*/

Public Integer getinteger (String key, integer Defaultvalue) {

String value = GetValue (key);

return value! = null? Integer.valueof (value): defaultvalue;

}

/**

* The property of the double type is removed, but the property of the system takes precedence. throws an exception if both are null or content Errors.

*/

Public Double getdouble (String Key) {

String value = GetValue (key);

if (value = = Null) {

throw new Nosuchelementexception ();

}

return double.valueof (value);

}

/**

* Remove the property of the double type, but the system property takes Precedence. returns the default value if both are null and throws an exception if the content is wrong

*/

Public Double getdouble (String key, Integer Defaultvalue) {

String value = GetValue (key);

return value! = null? Double.valueof (value): defaultvalue;

}

/**

* Remove the Boolean property, but the system property takes precedence. if both null throws an exception, false is returned if the content is not true/false.

*/

Public Boolean Getboolean (String Key) {

String value = GetValue (key);

if (value = = Null) {

throw new Nosuchelementexception ();

}

return boolean.valueof (value);

}

/**

* Remove the Boolean property, but the system property takes Precedence. returns the default value if it is null, or false if the content is not true/false.

*/

Public Boolean Getboolean (String key, boolean Defaultvalue) {

String value = GetValue (key);

return value! = null? Boolean.valueof (value): defaultvalue;

}

/**

* Load multiple files, file paths using Spring resource format.

*/

Private Properties loadProperties (String ... resourcespaths) {

Properties props = new properties ();

For (String Location:resourcespaths) {

Logger.debug ("Loading Properties File from:" + location);

InputStream is = null;

try {

Resource Resource = Resourceloader.getresource (location);

is = Resource.getinputstream ();

Props.load (is);

} catch (ioexception Ex) {

Logger.info ("Could not the load properties from path:" + location + "," + ex.getmessage ());

} finally {

ioutils.closequietly (is);

}

}

Return props;

}

}

161216. load Tool class using Spring's Defaultresourceloader custom properties file

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.