更新時間:2020-05-25 16:07:09 來源:動力節(jié)點 瀏覽2267次
Java代碼
String?host=(String)?CustomizedPropertyPlaceholderConfigurer.getContextProperty("mail.smtp.host");
Java代碼
?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;?
???
??/**
???*?自定義PropertyPlaceholderConfigurer返回properties內(nèi)容
??*?
???*?@author?LHY?2012-02-24
???*?
???*/?
??public?class?CustomizedPropertyPlaceholderConfigurer?extends?
??????????PropertyPlaceholderConfigurer?{?
???
??????private?static?Map<String,?Object>?ctxPropertiesMap;?
???
??????@Override?
??????protected?void?processProperties(?
??????????????ConfigurableListableBeanFactory?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);?
??????}?
??}
Xml代碼
<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>
以上就是動力節(jié)點java培訓(xùn)機構(gòu)的小編針對“如何獲取java配置properties文件”的內(nèi)容進(jìn)行的回答,希望對大家有所幫助,如有疑問,請在線咨詢,有專業(yè)老師隨時為你服務(wù)。
相關(guān)閱讀
初級 202925
初級 203221
初級 202629
初級 203743