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)