【发布时间】:2014-09-16 20:33:42
【问题描述】:
相关:Add entry to dictionary in child object
我正在尝试在我的 Spring.NET 配置中创建合并的父/子继承对象,如下所示:
<object name=".parent" abstract="true" type="Assembly.KeysAndValues, Assembly">
<property name="TheDictionary">
<dictionary key-type="Assembly.KeyType, Assembly" value-type="Assembly.ValueType, Assembly">
<entry key-ref="Object1" value-ref="Value1" />
<entry key-ref="Object2" value-ref="Value2" />
<entry key-ref="Object3" value-ref="Value3" />
</dictionary>
</property>
</object>
<object name="child" parent=".parent">
<property name="TheDictionary">
<dictionary merge="true">
<entry key-ref="Object2"><null/></entry>
</dictionary>
</property>
</object>
主要区别在于我在相关问题中使用“key-ref”而不是“key”。我仍然希望它能够工作,但事实并非如此。创建“子”对象时,我得到一个“已添加具有相同键的项目”。例外。这就是重点,我想使用合并将 Value2 替换为 null。
这是一个例外:
InnerException: System.ArgumentException
HResult=-2147024809
Message=An item with the same key has already been added.
Source=mscorlib
StackTrace:
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2.System.Collections.IDictionary.Add(Object key, Object value)
at Spring.Objects.Factory.Config.ManagedDictionary.Resolve(String objectName, IObjectDefinition definition, String propertyName, ManagedCollectionElementResolver resolver)
at Spring.Objects.Factory.Support.ObjectDefinitionValueResolver.ResolvePropertyValue(String name, IObjectDefinition definition, String argumentName, Object argumentValue)
at Spring.Objects.Factory.Support.ObjectDefinitionValueResolver.ResolveValueIfNecessary(String name, IObjectDefinition definition, String argumentName, Object argumentValue)
at Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.ApplyPropertyValues(String name, RootObjectDefinition definition, IObjectWrapper wrapper, IPropertyValues properties)
at Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.PopulateObject(String name, RootObjectDefinition definition, IObjectWrapper wrapper)
at Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.ConfigureObject(String name, RootObjectDefinition definition, IObjectWrapper wrapper)
at Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.InstantiateObject(String name, RootObjectDefinition definition, Object[] arguments, Boolean allowEagerCaching, Boolean suppressConfigure)
spring 集合合并不支持 key-ref 还是这里发生了其他事情?
【问题讨论】:
-
Object2 是否在 spring 容器中定义为单例?
-
我尝试将其标记为单例,但无论是否在 key-ref 对象上使用该属性,相同的错误都会持续存在。
-
我也尝试过在 key-ref 对象类型上实现相等成员,但无论如何都会继续看到错误。
标签: c# spring merge spring.net