The Spring MVC framework accesses the files configured in the Applicationcontext.xml file in Java code (which can be used to read the configuration file contents)

Source: Internet
Author: User

class= "Com.****.framework.core.springpropertiesutil"        lazy-init= "false" >        < Property Name= "Locations" >            <list>                <value>classpath:config/sys.properties</value>            </list>        </property>    </bean>

The path to the Sys.properties file is configured in the Applicationcontext.xml file ↑↑↑↑

Then the contents of the Springpropertiesutil file:

 Packagecom.****. Framework.core;Importorg.apache.commons.lang3.StringUtils;Importorg.springframework.beans.BeansException;Importorg.springframework.beans.factory.config.ConfigurableListableBeanFactory;ImportOrg.springframework.beans.factory.config.PropertyPlaceholderConfigurer;ImportJava.util.HashMap;ImportJava.util.Map;Importjava.util.Properties; Public classSpringpropertiesutilextendsPropertyplaceholderconfigurer {Private StaticMap<string, string>Propertiesmap; //Default as in Propertyplaceholderconfigurer    Private intSpringsystempropertiesmode =System_properties_mode_fallback; @Override Public voidSetsystempropertiesmode (intSystempropertiesmode) {        Super. Setsystempropertiesmode (Systempropertiesmode); Springsystempropertiesmode=Systempropertiesmode; } @Overrideprotected voidProcessproperties (configurablelistablebeanfactory beanfactory, Properties props)throwsbeansexception {Super. Processproperties (beanfactory, props); Propertiesmap=NewHashmap<string, string>();  for(Object key:props.keySet ()) {String keystr=key.tostring (); String Valuestr=Resolveplaceholder (keystr, props, Springsystempropertiesmode);        Propertiesmap.put (Keystr, VALUESTR); }    }     Public Staticstring GetProperty (string name) {returnpropertiesmap.get (name). toString (); }         Public Staticstring GetProperty (String name,string def) {string ret=propertiesmap.get (name); if(Stringutils.isblank (ret)) {returndef; }        returnret.tostring (); }}

The third step is the point: Call this configuration in Java code:

Springpropertiesutil Springpropertiesutil = (springpropertiesutil) applicationcontext.getbean ("PropertyConfigurer" );             return Springpropertiesutil.getproperty (key);

Spring MVC framework accesses the files configured in the Applicationcontext.xml file in Java code (can be used to read configuration file contents)

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.