關(guān)于spring屬性占位符用法
在bean定義時(shí)設(shè)置bean屬性的值時(shí),除了設(shè)置固定的值,還可以通過(guò)EL表達(dá)式和占位符來(lái)設(shè)置,容器在解析bean定義時(shí)會(huì)對(duì)EL表達(dá)式和占位符進(jìn)行解析求值。
本篇來(lái)學(xué)習(xí)一下通過(guò)占位符定義屬性的用法。
占位符的取值范圍有三個(gè):
- 系統(tǒng)變量(System.getProperty)
- 壞境變量(System.getEnv)
- 自定義的Properties文件
Spring提供了三種方式來(lái)配置加載自定義的properties:
1、PropertyPlaceholderConfigurer
一個(gè)BFPP,通過(guò)location屬性把properties文件的路徑傳入,并且可以設(shè)置系統(tǒng)變量加載模式,有三種:
- 不檢查系統(tǒng)屬性;
- 優(yōu)先加載自定義屬性,加載不到時(shí)加載系統(tǒng)屬性;
- 優(yōu)先加載系統(tǒng)屬性,加載不到時(shí)再加載自定義屬性。通過(guò)systemPropertiesMode設(shè)置加載模式。占位符的前后綴默認(rèn)是${和},但是可以通過(guò)設(shè)置placeholderPrefix和placeholderSuffix來(lái)修改前后綴。
PropertyPlaceholderConfigurer的用法如下:
bean定義xml:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:spring/beans/placeholder/attr.properties</value> </property> <property name="placeholderPrefix" value="${"></property> <property name="placeholderSuffix" value="}"></property> </bean> <bean id="placeHolderBean" class="spring.beans.placeholder.PlaceHolderBean"> <property name="id" value="${placeholder.id}"></property> <property name="name" value="${placeholder.name}"></property> <property name="refBean" ref="${placeholder.ref}"></property> </bean> <bean id="refedBean" class="spring.beans.placeholder.RefedBean"></bean>
自定義屬性文件attr.properties:
placeholder.id = 123 placeholder.name = cyy placeholder.ref = refedBean
2、PropertySourcesPlaceholderConfigurer
和PropertyPlaceholderConfigurer類(lèi)似,但是它不能設(shè)置系統(tǒng)屬性加載模式,而是通過(guò)localOverride屬性來(lái)決定是否用自定義的屬性來(lái)覆蓋系統(tǒng)屬性。
3、context:property-placeholder標(biāo)簽
spring設(shè)計(jì)了context:property-placeholder標(biāo)簽來(lái)簡(jiǎn)化配置,它的原理跟上面兩種方式是一樣的,通過(guò)location屬性設(shè)置屬性文件路徑,如有多個(gè)用逗號(hào)分隔。
容器通過(guò)PropertyPlaceholderBeanDefinitionParser解析該標(biāo)簽,在解析時(shí)會(huì)把這個(gè)標(biāo)簽解析成PropertyPlaceholderConfigurer或PropertySourcesPlaceholderConfigurer,當(dāng)system-properties-mode設(shè)置為ENVIRONMENT系統(tǒng)變量?jī)?yōu)先時(shí)使用PropertySourcesPlaceholderConfigurer,否則使用PropertyPlaceholderConfigurer,
下面是這個(gè)標(biāo)簽的用法:
<context:property-placeholder location="classpath:spring/beans/placeholder/attr.properties" /> <bean id="placeHolderBean" class="spring.beans.placeholder.PlaceHolderBean"> <property name="id" value="${placeholder.id}"></property> <property name="name" value="${placeholder.name}"></property> <property name="refBean" ref="${placeholder.ref}"></property> </bean> <bean id="refedBean" class="spring.beans.placeholder.RefedBean"></bean>
屬性配置器在加載完自定義屬性之后會(huì)創(chuàng)建一個(gè)字符值處理器StringValueResolver,并且把加載到的屬性設(shè)置到這個(gè)處理器中,遍歷當(dāng)前容器中所有的bean定義,使用這個(gè)處理來(lái)處理屬性中未處理的占位符以及別名中的占位符,并且把該處理添加到容器中,為后面的bean解析提供占位符處理,見(jiàn)PlaceholderConfigurerSupport類(lèi)中doProcessProperties方法的代碼:
protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess, StringValueResolver valueResolver) { BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver); String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames(); for (String curName : beanNames) { // Check that we're not parsing our own bean definition, // to avoid failing on unresolvable placeholders in properties file locations. if (!(curName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) { BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(curName); try { visitor.visitBeanDefinition(bd); } catch (Exception ex) { throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, ex.getMessage(), ex); } } } // New in Spring 2.5: resolve placeholders in alias target names and aliases as well. beanFactoryToProcess.resolveAliases(valueResolver); // New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes. beanFactoryToProcess.addEmbeddedValueResolver(valueResolver); }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JAVA利用順序表實(shí)現(xiàn)“楊輝三角”的思路及代碼示例
楊輝三角形是中國(guó)古代數(shù)學(xué)的杰出研究成果之一,是我國(guó)北宋數(shù)學(xué)家賈憲于1050年首先發(fā)現(xiàn)并使用的,這篇文章主要介紹了JAVA利用順序表實(shí)現(xiàn)楊輝三角的思路及代碼示例,需要的朋友可以參考下2025-01-01Java servlet 使用 PrintWriter 時(shí)的編碼與亂碼的示例代碼
本篇文章主要介紹了Java servlet 使用 PrintWriter 時(shí)的編碼與亂碼的示例代碼,探討了 PrintWriter 的缺省編碼與普通字符流的缺省編碼的差異,具有一定的參考價(jià)值,有興趣的可以了解一下2017-11-11SpringBoot引入模板引擎實(shí)現(xiàn)視圖解析
這篇文章主要介紹了SpringBoot引入模板引擎實(shí)現(xiàn)視圖解析方法流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-10-10mybatis-plus實(shí)現(xiàn)邏輯刪除的示例代碼
本文主要介紹了mybatis-plus實(shí)現(xiàn)邏輯刪除的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05java實(shí)現(xiàn)同步回調(diào)的示例代碼
同步回調(diào)是一種在調(diào)用代碼中同步執(zhí)行回調(diào)函數(shù)的編程模式,在Java中,通過(guò)定義和實(shí)現(xiàn)接口來(lái)構(gòu)建同步回調(diào),本文就來(lái)介紹一下如何實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-09-09淺談Java虛擬機(jī)對(duì)內(nèi)部鎖的四種優(yōu)化方式
這篇文章主要介紹了淺談Java虛擬機(jī)對(duì)內(nèi)部鎖的四種優(yōu)化方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10springboot中的多個(gè)application文件講解
這篇文章主要介紹了springboot中的多個(gè)application文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09