1

这是一个长镜头...

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

我在 XML 和代码之间混合配置容器(直接使用 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)

编辑: 我刚得到所有的定义名称,不知何故那里有一个空条目,这似乎是问题所在,不知道为什么会发生......

4

1 回答 1

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));
于 2013-01-17T13:12:08.613 回答