日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring 读取配置文件的优先级

發布時間:2025/6/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring 读取配置文件的优先级 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

spring 讀取配置文件的優先級

1 結論

在spring加載properties配置文件的過程中,會根據key出現覆蓋現象,后加載的覆蓋前面的。

| 加載順序 | key | value | 最終輸出 | | :---: | :--: | :--: | :--: | | p1 | testConver | 沒有覆蓋,為p1 | 覆蓋,為p2屬性 | | p1 | test| 測試區 | 測試區 | | p2 | testConver | 覆蓋,為p2屬性 | 覆蓋,為p2屬性 | | p2 | test| 開發區 | 開發區 |

2 上干活

2.1 java 代碼

單元測試類為:

import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(value = "classpath:/spring-resources.xml") public class SpringPropertiesTest {@Value("${testConver}")private String testConver;@Value("${dev}")private String dev;@Value("${test}")private String test;@Testpublic void showPropValue() {System.out.println(testConver);System.out.println(dev);System.out.println(test);} }

2.2 spring配置

spring-resources.xml配置為

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="order" value="1"/><property name="ignoreUnresolvablePlaceholders" value="true"/><property name="ignoreResourceNotFound" value="false"></property><property name="locations"><list><value>classpath:p1.properties</value><value>classpath:p2.properties</value></list></property></bean> </beans>

2.3 配置文件

p1.properties:

testConver=沒有覆蓋,為p1 test=測試區

p2.properties:

testConver=覆蓋,為p2屬性 dev=開發區

2.4 控制臺輸出結果

控制臺輸出內容為:

十月 28, 2016 3:24:18 下午 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners 信息: @TestExecutionListeners is not present for class [class com.dxhy.spring.SpringPropertiesTest]: using defaults. 十月 28, 2016 3:24:18 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [spring-resources.xml] 十月 28, 2016 3:24:19 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.GenericApplicationContext@2723a510: startup date [Fri Oct 28 15:24:19 CST 2016]; root of context hierarchy 十月 28, 2016 3:24:19 下午 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties 信息: Loading properties file from class path resource [p1.properties] 十月 28, 2016 3:24:19 下午 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties 信息: Loading properties file from class path resource [p2.properties] 十月 28, 2016 3:24:19 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@690a614: defining beans [propertyConfigurer,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy 覆蓋,為p2屬性 開發區 測試區 十月 28, 2016 3:24:19 下午 org.springframework.context.support.AbstractApplicationContext doClose 信息: Closing org.springframework.context.support.GenericApplicationContext@2723a510: startup date [Fri Oct 28 15:24:19 CST 2016]; root of context hierarchy

轉載于:https://my.oschina.net/dylw/blog/777391

總結

以上是生活随笔為你收集整理的spring 读取配置文件的优先级的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。