/// <summary> /// Instantiate and invoke all registered /// <see cref="Spring.Objects.Factory.Config.IObjectFactoryPostProcessor"/> /// objects, respecting any explicit ordering. /// </summary> /// <remarks> /// <note type="caution"> /// <b>Must</b> be called before singleton instantiation. /// </note> /// </remarks> /// <exception cref="ObjectsException">In the case of errors.</exception> private void InvokeObjectFactoryPostProcessors(IConfigurableListableObjectFactory objectFactory) { // Invoke BeanDefinitionRegistryPostProcessors first, if any. ArrayList processedObjects = new ArrayList(); if (objectFactory is IObjectDefinitionRegistry) { IObjectDefinitionRegistry registry = (IObjectDefinitionRegistry)objectFactory; ArrayList regularPostProcessors = new ArrayList(); ArrayList registryPostProcessors = new ArrayList(); foreach (IObjectFactoryPostProcessor factoryProcessor in ObjectFactoryPostProcessors) { if (factoryProcessor is IObjectDefinitionRegistryPostProcessor) { ((IObjectDefinitionRegistryPostProcessor)factoryProcessor).PostProcessObjectDefinitionRegistry(registry); registryPostProcessors.Add(factoryProcessor); } else { regularPostProcessors.Add(factoryProcessor); } } IDictionary objectMap = objectFactory.GetObjectsOfType(typeof(IObjectDefinitionRegistryPostProcessor), true, false); ArrayList registryPostProcessorObjects = new ArrayList(objectMap.Values); registryPostProcessorObjects.Sort(new OrderComparator()); foreach (System.Object processor in registryPostProcessorObjects) { ((IObjectDefinitionRegistryPostProcessor)processor).PostProcessObjectDefinitionRegistry(registry); } InvokeObjectFactoryPostProcessors(registryPostProcessors, objectFactory); InvokeObjectFactoryPostProcessors(registryPostProcessorObjects, objectFactory); InvokeObjectFactoryPostProcessors(regularPostProcessors, objectFactory); // processedObjects.Add(objectMap.Keys); foreach (DictionaryEntry entry in objectMap) { processedObjects.Add(entry.Key); } } else { foreach (IObjectFactoryPostProcessor factoryProcessor in ObjectFactoryPostProcessors) { // Invoke factory processors registered with the context instance. factoryProcessor.PostProcessObjectFactory(objectFactory); } } // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let the bean factory post-processors apply to them! ArrayList factoryProcessorNames = new ArrayList(); string[] names = GetObjectNamesForType(typeof(IObjectFactoryPostProcessor), true, false); foreach (string name in names) { factoryProcessorNames.Add(name); } // Separate between ObjectFactoryPostProcessors that implement PriorityOrdered, // Ordered, and the rest. ArrayList priorityOrderedFactoryProcessors = new ArrayList(); ArrayList orderedFactoryProcessorsNames = new ArrayList(); ArrayList nonOrderedFactoryProcessorNames = new ArrayList(); for (int i = 0; i < factoryProcessorNames.Count; ++i) { string processorName = (string)factoryProcessorNames[i]; if (processedObjects.Contains(processorName)) { //skip -- already processed in first phase above Debug.WriteLine(""); } else if (IsTypeMatch(processorName, typeof(IPriorityOrdered))) { priorityOrderedFactoryProcessors.Add(ObjectFactory.GetObject(processorName, typeof(IObjectFactoryPostProcessor))); } else if (IsTypeMatch(processorName, typeof(IOrdered))) { orderedFactoryProcessorsNames.Add(processorName); } else { nonOrderedFactoryProcessorNames.Add(processorName); } } // First, invoke the IObjectFactoryPostProcessors that implement IPriorityOrdered. InvokePriorityOrderedObjectFactoryPostProcessors(factoryProcessorNames, priorityOrderedFactoryProcessors); // Second, invoke those IObjectFactoryPostProcessors that implement IOrdered... ArrayList orderedFactoryProcessors = new ArrayList(); foreach (string orderedFactoryProcessorsName in orderedFactoryProcessorsNames) { orderedFactoryProcessors.Add(ObjectFactory.GetObject(orderedFactoryProcessorsName, typeof(IObjectFactoryPostProcessor))); } orderedFactoryProcessors.Sort(new OrderComparator()); InvokeObjectFactoryPostProcessors(orderedFactoryProcessors, ObjectFactory); // and then the unordered ones... ArrayList nonOrderedPostProcessors = new ArrayList(); foreach (string nonOrderedFactoryProcessorName in nonOrderedFactoryProcessorNames) { nonOrderedPostProcessors.Add(ObjectFactory.GetObject(nonOrderedFactoryProcessorName, typeof(IObjectFactoryPostProcessor))); } InvokeObjectFactoryPostProcessors(nonOrderedPostProcessors, ObjectFactory); #region Instrumentation if (log.IsDebugEnabled) { log.Debug(string.Format( CultureInfo.InvariantCulture, "processed {0} IFactoryObjectPostProcessors defined in application context [{1}].", factoryProcessorNames.Count, Name)); } #endregion }