使用spring工廠讀取property配置文件示例代碼
本文將介紹兩種Spring讀取property配置文件的方法,接下來(lái)看看具體內(nèi)容。
一、通過(guò)Spring工廠讀取
示例:
public class PropertyConfig { private static AbstractBeanFactory beanFactory = null; private static final Map<String,String> cache = new oncurrentHashMap<>(); @Inject public PropertyConfig(AbstractBeanFactory beanFactory) { this.beanFactory = beanFactory; } /** * 根據(jù)key獲取配置文件的Value * @param key * @return */ public static String getProperty(String key) { String propValue = ""; if(cache.containsKey(key)){ propValue = cache.get(key); } else { try { propValue = beanFactory.resolveEmbeddedValue("${" + key.trim() + "}"); cache.put(key,propValue); } catch (IllegalArgumentException ex) { ex.printStackTrace(); } } return propValue; } }
Spring xml的配置
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/> <property name="ignoreResourceNotFound" value="true"/> <property name="locations"> <list> <value>classpath:props/${property-path}.properties</value> <value>classpath:important.properties</value> </list> </property> </bean>
在項(xiàng)目中使用
String maxTimeInSecondsProp = PropertyConfig.getProperty("maxTimeInSeconds");
二、直接使用spirng程序代碼讀取項(xiàng)目的配置文件方法
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.core.io.FileSystemResource; public class Test { /** * @param args */ public static void main( String[] args ) { String configFile = "D:/test/application.properties"; //如果配置文件在classpath目錄下可以使用ClassPathResource對(duì)象 //Resource resource = new ClassPathResource("/application.properties"); Resource resource = new FileSystemResource( configFile ); try { Properties property = PropertiesLoaderUtils.loadProperties(resource); String driver = property.getProperty("jdbc.driver"); String url = property.getProperty("jdbc.url"); String userName = property.getProperty("jdbc.username"); String password = property.getProperty("jdbc.password"); } catch (IOException e1) { //log.error("read config file failed", e1); } } }
如果配置文件在classpath目錄下可以使用ClassPathResource對(duì)象
Resource resource = new ClassPathResource("/application.properties");
總結(jié)
以上就是本文關(guān)于使用spring工廠讀取property配置文件示例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
java?for循環(huán)內(nèi)執(zhí)行多線程問(wèn)題
這篇文章主要介紹了java?for循環(huán)內(nèi)執(zhí)行多線程問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03理解Java注解及Spring的@Autowired是如何實(shí)現(xiàn)的
今天通過(guò)本文帶領(lǐng)大家學(xué)習(xí)注解的基礎(chǔ)知識(shí),學(xué)習(xí)Spring的@Autowired是怎么實(shí)現(xiàn)的,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-07-07Spring Boot Excel文件導(dǎo)出下載實(shí)現(xiàn)代碼
這篇文章帶領(lǐng)我們直接實(shí)現(xiàn)Excel文件的直接導(dǎo)出下載,后續(xù)開(kāi)發(fā)不需要開(kāi)發(fā)很多代碼,直接繼承已經(jīng)寫(xiě)好的代碼,增加一個(gè)Xml配置就可以直接導(dǎo)出。具體實(shí)現(xiàn)代碼大家跟隨小編一起通過(guò)本文學(xué)習(xí)吧2018-11-11Spring Gateway處理微服務(wù)的路由轉(zhuǎn)發(fā)機(jī)制
我們?cè)敿?xì)地介紹了Spring Gateway,這個(gè)基于Spring 5、Spring Boot 2和Project Reactor的API網(wǎng)關(guān),通過(guò)這篇文章,我們可以清晰地看到Spring Gateway的工作原理,以及它的強(qiáng)大之處,感興趣的朋友一起看看吧2024-08-08Java中比較器Comparator和Comparable的區(qū)別
這篇文章主要介紹了Java中比較器Comparator和Comparable的區(qū)別,我們?cè)谑褂?Collections.sort()對(duì)鏈表進(jìn)行排序時(shí),常常需要根據(jù)不同情況自定義排序規(guī)則,今天我們來(lái)看看比較器之間的區(qū)別,需要的朋友可以參考下2023-08-08springboot內(nèi)置tomcat之NIO處理流程一覽
這篇文章主要介紹了springboot內(nèi)置tomcat之NIO處理流程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12