Spring.Objects.Factory.ObjectCreationException:System.ArgumentNullException:Key不能为null

时间:2012-12-18 11:06:41

标签: spring.net argumentnullexception

这是一个长镜头......

我正在使用spring 1.3.2,并且在随机时间我得到ArgumentNullException异常(请参阅下面的堆栈跟踪)。

我在XML和Code之间混合配置容器(直接使用ObjectDefinitionBuilder,没有代码配置)。注册并行进行(5个线程加载定义)。

我的所有对象都使用通过构造函数自动装配,错误发生在构造函数中具有项目的两个组件中。

在所有内容都已在容器中注册后,我正在进行以下调用

context.GetObjectsOfType(typeof (IFoo)).OfType<DictionaryEntry>().Select(d => (IFoo) d.Value)

从堆栈跟踪中查看代码,我发现对IsAlias的调用似乎失败了,但我不确定我是否理解这是怎么回事。

有什么想法/想法吗?

堆栈追踪:

Spring.Objects.Factory.ObjectCreationException: Error creating object with name 'My.App.SomeFooImplementation' : Initialization of object failed : Key cannot be null.
Parameter name: key ---> System.ArgumentNullException: Key cannot be null.
Parameter name: key
   at System.Collections.Hashtable.ContainsKey(Object key)
   at System.Collections.Specialized.OrderedDictionary.Contains(Object key)
   at Spring.Objects.Factory.Support.AbstractObjectFactory.IsAlias(String name)
   at Spring.Objects.Factory.Support.DefaultListableObjectFactory.DoGetObjectNamesForType(Type type, Boolean includeNonSingletons, Boolean allowEagerInit)
   at Spring.Objects.Factory.Support.DefaultListableObjectFactory.GetObjectNamesForType(Type type, Boolean includePrototypes, Boolean includeFactoryObjects)
   at Spring.Objects.Factory.ObjectFactoryUtils.ObjectNamesForTypeIncludingAncestors(IListableObjectFactory factory, Type type, Boolean includePrototypes, Boolean includeFactoryObjects)
   at Spring.Objects.Factory.Support.DefaultListableObjectFactory.FindAutowireCandidates(String objectName, Type requiredType, DependencyDescriptor descriptor)
   at Spring.Objects.Factory.Support.DefaultListableObjectFactory.ResolveDependency(DependencyDescriptor descriptor, String objectName, IList autowiredObjectNames)
   at Spring.Objects.Factory.Support.ConstructorResolver.CreateArgumentArray(String objectName, RootObjectDefinition rod, ConstructorArgumentValues resolvedValues, ObjectWrapper wrapper, Type[] paramTypes, MethodBase methodOrCtorInfo, Boolean autowiring, UnsatisfiedDependencyExceptionData& unsatisfiedDependencyExceptionData)
   at Spring.Objects.Factory.Support.ConstructorResolver.AutowireConstructor(String objectName, RootObjectDefinition rod, ConstructorInfo[] chosenCtors, Object[] explicitArgs)
   at Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.CreateObjectInstance(String objectName, RootObjectDefinition objectDefinition, Object[] arguments)
   at Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.InstantiateObject(String name, RootObjectDefinition definition, Object[] arguments, Boolean allowEagerCaching, Boolean suppressConfigure)
   --- End of inner exception stack trace ---
   at Spring.Objects.Factory.Support.AbstractAutowireCapableObjectFactory.InstantiateObject(String name, RootObjectDefinition definition, Object[] arguments, Boolean allowEagerCaching, Boolean suppressConfigure)
   at Spring.Objects.Factory.Support.AbstractObjectFactory.CreateAndCacheSingletonInstance(String objectName, RootObjectDefinition objectDefinition, Object[] arguments)
   at Spring.Objects.Factory.Support.AbstractObjectFactory.GetObjectInternal(String name, Type requiredType, Object[] arguments, Boolean suppressConfigure)
   at Spring.Objects.Factory.Support.AbstractObjectFactory.GetObject(String name)

修改 我刚刚得到了所有的定义名称,并且在那里有一个空条目,这似乎是问题,不知道它为什么会发生......

1 个答案:

答案 0 :(得分:1)

我能想出的唯一解决方法是将内部objectDefinitionNames ArrayList更改为使用ArrayList.Synchronized进行反射。 一点都不好但似乎做了这件事。

        var field = factory.GetType().GetField("objectDefinitionNames", BindingFlags.NonPublic | BindingFlags.Instance);
        if (field == null)
            throw new InvalidOperationException("Could not get Definitions field from application context.");

        var value = (ArrayList) field.GetValue(factory);
        field.SetValue(factory, ArrayList.Synchronized(value));