2012-11-19 55 views
2

我想使用兩個屬性佔位符配置器,其中一個用於檢索base64解碼值。我遇到的問題是隻有其中一個將屬性加載到名稱/值集合中。哪一個依賴於我將它們放置在XML中的順序(並且在切換它們時設置第一個忽略不可解析)。Spring.NET使用多個PropertyPlaceholderConfigurers不起作用

這裏的配置是什麼樣子:

<object id="propertyConfigurer" type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
     <value>file://~Database.config</value> 
     </list> 
    </property> 
    <property name="configSections"> 
     <list> 
     <value>database</value> 
     </list> 
    </property> 
    <property name="ignoreUnresolvablePlaceholders" value="true" /> 
    </object> 

    <object id="encodedPropertyConfigurer" type="MyProject.Config.EncodedPropertyConfigurer"> 
    <property name="locations"> 
     <list> 
     <value>file://~Database_auth.config</value> 
     </list> 
    </property> 
    <property name="configSections"> 
     <list> 
     <value>database_auth</value> 
     </list> 
    </property> 
    <property name="ignoreUnresolvablePlaceholders" value="false" /> 
    </object> 

我延長PropertyPlaceholderConfigurer,覆蓋像這樣的虛方法:

public class EncodedPropertyConfigurer : PropertyPlaceholderConfigurer 
{ 
    protected override string ResolvePlaceholder(string placeholder, System.Collections.Specialized.NameValueCollection props) 
    { 
     return System.Text.Encoding.ASCII.GetString(Convert.FromBase64String(base.ResolvePlaceholder(placeholder, props)));    
    } 
} 

再次,根據我把它們在Web.config的順序,只有一個文件被加載到Name/Value集合中。作爲粘貼,它將使用encodedPropertyConfigurer(例如,我會在集合中看到「用戶名」和「密碼」,但不是連接字符串。如果我翻轉該命令,我會看到「connectionString」,但不是用戶名或密碼。 我做錯了什麼?該文檔說支持多個PropertyPlaceholderConfigurer,並且只需要注意ignoreUnresolvable設置。 請注意,我測試使用這兩個實例作爲Spring PropertyPlaceholderConfigurer(而不是我的擴展類)和SAME行爲發生 - 只有一個被加載到列表中。

回答

1

與此相關的:https://jira.springsource.org/browse/SPR-6428

指定的其他工作生產力促進中心不同的佔位符前綴/後綴,雖然不是很理想。就像這樣:

<!-- DB Properties (non-encoded) file configurer --> 
    <object name="propertyConfigurer" type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer"> 
     <property name="configSections" value="database"/> 
     <property name="ignoreUnresolvablePlaceholders" value="true" /> 
    </object> 

    <!-- DB Properties (encoded) file configurer --> 
    <object name="encodedPropertyConfigurer" type="MyProject.Config.EncodedPropertyConfigurer"> 
    <property name="configSections" value="database_auth"/> 
    <property name="ignoreUnresolvablePlaceholders" value="true" /> 
    <property name="placeholderPrefix" value="$[" /> 
    <property name="placeholderSuffix" value="]" /> 
    </object> 

然後只要確保是從第二個檢索到的那些與$ [],而不是默認的$ {}使用。

+0

爲什麼不理想? – Najera