How to obtain the properties property file configured in spring in Java

Source: Internet
Author: User

1. Configure the properties file through spring

 

<bean id="propertyConfigurer"class="com.tjsoft.base.util.CustomizedPropertyPlaceholderConfigurer"><property name="ignoreResourceNotFound" value="true" /><property name="locations"><list><value>/WEB-INF/config/jdbc.properties</value><value>/WEB-INF/config/mail.properties</value><value>/WEB-INF/config/system.properties</value></list></property></bean>

Here, class is the defined class.

2. Custom class customizedpropertyplaceholderconfigurer

Import Java. util. hashmap; import Java. util. map; import Java. util. properties; import Org. springframework. beans. beansexception; import Org. springframework. beans. factory. config. configurablelistablebeanfactory; import Org. springframework. beans. factory. config. propertyplaceholderconfigurer;/*** custom propertyplaceholderconfigurer returns properties ** @ author LHY 2012-02-24 **/public class customizedpropertyplaceholderconfigurer extends {Private Static Map <string, Object> ctxpropertiesmap; @ overrideprotected void processproperties (abstrablelistablebeanfactory beanfactorytoprocess, properties props) throws beansexception {super. processproperties (beanfactorytoprocess, props); ctxpropertiesmap = new hashmap <string, Object> (); For (Object key: props. keyset () {string keystr = key. tostring (); string value = props. getproperty (keystr); ctxpropertiesmap. put (keystr, value) ;}} public static object getcontextproperty (string name) {return ctxpropertiesmap. get (name );}}

 

 

In this way, you can use the customizedpropertyplaceholderconfigurer class to obtain the content in the properties property file.

3. How to obtain the property File Content

String host = (string) customizedpropertyplaceholderconfigurer. getcontextproperty ("mail. SMTP. Host ");

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.