2012-11-19 47 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