Read the properties file in spring

Source: Internet
Author: User

Build configs.properties in src directory

Backup.host = 192.168.1.6==pwd

To create a static class:

 PackageCom.ly.jxc.util;Importjava.io.IOException;ImportJava.util.HashMap;ImportJava.util.Map;Importjava.util.Properties;ImportOrg.springframework.beans.factory.config.PropertyPlaceholderConfigurer;ImportOrg.springframework.core.io.ClassPathResource;ImportOrg.springframework.core.io.Resource;Importorg.springframework.core.io.support.PropertiesLoaderUtils;/*** Configuration file Read class *@authorYQ * 2014-09-29*/ Public classConfigsextendsPropertyplaceholderconfigurer {Private StaticMap<string, object>Ctxpropertiesmap; protected Static voidload () {Resource Resource=NewClasspathresource ("/config.properties");    Properties props; Try{Props=propertiesloaderutils.loadproperties (Resource); Ctxpropertiesmap=NewHashmap<string, object>();  for(Object key:props.keySet ()) {String keystr=key.tostring (); String value=Props.getproperty (KEYSTR);      Ctxpropertiesmap.put (keystr, value); }      } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }}  /*** return int with default value *@paramname *@return */ Public Static intGetintvalue (String name,intDefaultValue) {     if(Ctxpropertiesmap = =NULL||ctxpropertiesmap.isempty ()) load (); if(Ctxpropertiesmap.get (name) = =NULL)    returnDefaultValue; returninteger.parseint (String) ctxpropertiesmap.get (name)); }  /*** Return int *@paramname *@return */ Public Static intGetintvalue (String name) {if(Ctxpropertiesmap = =NULL||ctxpropertiesmap.isempty ()) load (); if(Ctxpropertiesmap.get (name) = =NULL)    return0; returninteger.parseint (String) ctxpropertiesmap.get (name)); }  /*** Return String *@paramname *@return */ Public Staticstring GetValue (string name) {if(Ctxpropertiesmap = =NULL||ctxpropertiesmap.isempty ()) load (); return(String) ctxpropertiesmap.get (name); }  }

Then configure the static class (see my other article http://www.cnblogs.com/yqweber/p/3992513.html) to be able to use the page directly to take the value.

Read the properties file in spring

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.