private void DoGetObjectsOfType(Type type, bool includeFactoryObjects, bool includePrototypes, IDictionary collector) { bool isFactoryType = (type != null && typeof(IFactoryObject).IsAssignableFrom(type)); foreach (string name in objects.Keys) { object instance = objects[name]; if (instance is IFactoryObject && includeFactoryObjects) { IFactoryObject factory = (IFactoryObject)instance; Type objectType = factory.ObjectType; if ((objectType == null && factory.IsSingleton) || ((factory.IsSingleton || includePrototypes) && objectType != null && type.IsAssignableFrom(objectType))) { object createdObject = GetObject(name); if (type.IsInstanceOfType(createdObject)) { collector[name] = createdObject; } } } else if (type.IsAssignableFrom(instance.GetType())) { if (isFactoryType) { collector[ObjectFactoryUtils.BuildFactoryObjectName(name)] = instance; } else { collector[name] = instance; } } } }
/// <summary> /// Return the object instances that match the given object /// <see cref="System.Type"/> (including subclasses). /// </summary> /// <param name="type"> /// The <see cref="System.Type"/> (class or interface) to match. /// </param> /// <param name="includeNonSingletons"> /// Whether to include prototype objects too or just singletons (also applies to /// <see cref="Spring.Objects.Factory.IFactoryObject"/>s). /// </param> /// <param name="allowEagerInit"> /// Whether to include <see cref="Spring.Objects.Factory.IFactoryObject"/>s too /// or just normal objects. /// </param> /// <returns> /// An <see cref="System.Collections.IDictionary"/> of the matching objects, /// containing the object names as keys and the corresponding object instances /// as values. /// </returns> /// <exception cref="Spring.Objects.ObjectsException"> /// If any of the objects could not be created. /// </exception> /// <seealso cref="Spring.Objects.Factory.IListableObjectFactory.GetObjectsOfType(Type, bool, bool)"/> protected IList DoGetObjectNamesForType(Type type, bool includeNonSingletons, bool allowEagerInit) { IList result = new ArrayList(); string[] objectNames = GetObjectDefinitionNames(); foreach (string s in objectNames) { string objectName = s; if (!IsAlias(objectName)) { try { RootObjectDefinition mod = GetMergedObjectDefinition(objectName, false); // Only check object definition if it is complete if (!mod.IsAbstract && (allowEagerInit || (mod.HasObjectType || !mod.IsLazyInit /*|| this.AllowEagerTypeLoading*/) && !RequiresEagerInitForType(mod.FactoryObjectName))) { bool isFactoryObject = IsFactoryObject(objectName, mod); bool matchFound = (allowEagerInit || !isFactoryObject || ContainsSingleton(objectName)) && (includeNonSingletons || IsSingleton(objectName)) && IsTypeMatch(objectName, type); if (!matchFound && isFactoryObject) { // in case of a FactoryObject, try to match FactoryObject instance itself next objectName = ObjectFactoryUtils.BuildFactoryObjectName(objectName); matchFound = (includeNonSingletons || mod.IsSingleton) && IsTypeMatch(objectName, type); } if (matchFound) { result.Add(objectName); } } } catch (CannotLoadObjectTypeException ex) { if (allowEagerInit) { throw; } // Probably contains a placeholder; lets ignore it for type matching purposes. if (log.IsDebugEnabled) { log.Debug("Ignoring object class loading failure for object '" + objectName + "'", ex); } } catch (ObjectDefinitionStoreException ex) { if (allowEagerInit) { throw; } // Probably contains a placeholder; lets ignore it for type matching purposes. if (log.IsDebugEnabled) { log.Debug("Ignoring unresolvable metadata in object definition '" + objectName + "'", ex); } } } } // check singletons too, to catch manually registered singletons... string[] singletonNames = GetSingletonNames(); foreach (string s in singletonNames) { string objectName = s; // only check if manually registered... if (!ContainsObjectDefinition(objectName)) { // in the case of an IFactoryObject, match the object created by the IFactoryObject... if (IsFactoryObject(objectName)) { if ((includeNonSingletons || IsSingleton(objectName)) && IsTypeMatch(objectName, type)) { result.Add(objectName); continue; } objectName = ObjectFactoryUtils.BuildFactoryObjectName(objectName); } if (IsTypeMatch(objectName, type)) { result.Add(objectName); } } } return(result); }
/// <summary> /// Ensure that all non-lazy-init singletons are instantiated, also /// considering <see cref="Spring.Objects.Factory.IFactoryObject"/>s. /// </summary> /// <exception cref="Spring.Objects.ObjectsException"> /// If one of the singleton objects could not be created. /// </exception> /// <seealso cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory.PreInstantiateSingletons"/> public void PreInstantiateSingletons() { #region Instrumentation if (log.IsDebugEnabled) { log.Debug("Pre-instantiating singletons in factory [" + this + "]"); } #endregion try { int definitionCount = objectDefinitionNames.Count; for (int i = 0; i < definitionCount; i++) { string name = (string)objectDefinitionNames[i]; if (!ContainsSingleton(name) && ContainsObjectDefinition(name)) { RootObjectDefinition definition = GetMergedObjectDefinition(name, false); if (!definition.IsAbstract && definition.IsSingleton && !definition.IsLazyInit) { Type objectType = ResolveObjectType(definition, name); if (objectType != null && typeof(IFactoryObject).IsAssignableFrom(definition.ObjectType)) { IFactoryObject factoryObject = (IFactoryObject)GetObject( ObjectFactoryUtils. BuildFactoryObjectName(name)); if (factoryObject.IsSingleton) { GetObject(name); } } else { GetObject(name); } } } } } catch (ObjectsException) { // destroy already created singletons to avoid dangling resources... try { Dispose(); } catch (Exception ex) { log.Error( "PreInstantiateSingletons failed but couldn't destroy any already-created singletons.", ex); } throw; } }