簡體   English   中英

找不到具有多個上下文的屬性:property-placeholder

[英]property not found with multiple context:property-placeholder

我正在使用帶有彈簧輪廓的spring 3.1來加載bean。 在我的應用上下文文件中,我加載如下屬性:

<context:property-placeholder order="1"  location="classpath*:META-INF/spring/*_${spring.profiles.active}.properties" ignore-unresolvable="true"/>

然后我使用屬性值來加載數據源bean,例如

<property name="driverClassName" value="${database.driverClassName}"/>

工作正常。 當我添加幾個其他屬性占位符以便可以從某些數據庫表加載屬性時,問題就開始了。

這使用由加載的屬性引用

<bean id="configFactoryBean"
class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
   <constructor-arg ref="globalSystemConfiguration"/>
</bean>

為了增加細節,此configFactoryBean使用datasource從數據庫加載屬性。

當我這樣做時,我有以下異常:

java.lang.ClassNotFoundException: ${database.driverClassName}

我的分析是,它試圖在從第一個上下文屬性占位符解析屬性之前加載datasource 我可能是錯的。 或彈簧輪廓變量未正確解析。

誰能幫我解決這個問題。

謝謝Akki

關於多個屬性占位符的此錯誤可能與您的問題有關: https : //jira.spring.io/browse/SPR-9989

當使用多個PropertyPlaceholderConfigurer以及@Value注釋和占位符語法的默認值(即${key:defaultValue} )時,僅使用第一個PropertyPlaceholderConfigurer 如果此配置程序不包含所需的值,則即使第二個PropertyPlaceholderConfigurer包含該值,它也會回@Value默認值@Value

影響版本:3.1.3

在我的應用程序中,我以以下方式使用property-placeholder configurer,它工作得很好。 你可以試試看。

<bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="locations">
            <list>
                <value>classpath*:META-INF/spring/*_${spring.profiles.active}.properties</value>
            </list>
          </property>
    </bean>

我認為這應該可以解決您的問題。 :)

每個<context:property-placeholder>都會創建一個PropertyPlaceholderConfigurer的新實例 -容易弄亂。 每個應用程序和應用程序級別都應該有一個這樣的東西,而不是庫的一個-這樣會使維護容易得多。

有關更多細節和建議,請參見: http : //rostislav-matl.blogspot.cz/2013/06/resolving-properties-with-spring.html

由於已經建議對配置文件的路徑進行硬編碼,因此,請嘗試使用標簽上的profiles屬性來有選擇地包括配置。

<beans profile="profileName">
    <context:property-placeholder  order="1"  location="classpath*:META-INF/spring/hardcoded.properties" ignore-unresolvable="true"/>
</beans>

<beans profile="profileName2">    
    <context:property-placeholder order="1"  location="classpath*:META-INF/spring/hardcoded.properties" ignore-unresolvable="true"/>
</beans>

請參閱這篇說明配置文件的文章: http : //java.dzone.com/articles/using-spring-profiles-xml

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM