private ObjectDefinitionBuilder SetLifetime(ObjectDefinitionBuilder b) { switch (LifetimeForRegisters) { case ServiceRegistrarLifetime.Transient: break; case ServiceRegistrarLifetime.Singleton: b.SetSingleton(true); break; default: throw new NotSupportedException(); } return(b); }
/// <summary> /// Central template method to actually parse the supplied XmlElement /// into one or more IObjectDefinitions. /// </summary> /// <param name="element">The element that is to be parsed into one or more <see cref="Spring.Objects.Factory.Config.IObjectDefinition"/>s</param> /// <param name="parserContext">The the object encapsulating the current state of the parsing process; /// provides access to a <see cref="IObjectDefinitionRegistry"/></param> /// <returns> /// The primary IObjectDefinition resulting from the parsing of the supplied XmlElement /// </returns> protected override AbstractObjectDefinition ParseInternal(XmlElement element, ParserContext parserContext) { ObjectDefinitionBuilder builder = ObjectDefinitionBuilder.GenericObjectDefinition(); string parentName = GetParentName(element); if (parentName != null) { builder.RawObjectDefinition.ParentName = parentName; } Type objectType = GetObjectType(element); if (objectType != null) { builder.RawObjectDefinition.ObjectType = objectType; } else { string objectTypeName = GetObjectTypeName(element); if (objectTypeName != null) { builder.RawObjectDefinition.ObjectTypeName = objectTypeName; } } // TODO (EE) // builder.getRawBeanDefinition().setSource(parserContext.extractSource(element)); if (parserContext.IsNested) { // Inner object definition must receive same singleton status as containing object. builder.SetSingleton(parserContext.ContainingObjectDefinition.IsSingleton); } if (parserContext.IsDefaultLazyInit) { // Default-lazy-init applies to custom object definitions as well. builder.SetLazyInit(true); } DoParse(element, parserContext, builder); return(builder.ObjectDefinition); }
/// <summary> /// 如果某个类型未在容器注册,则注册该类型 /// </summary> /// <param name="container"></param> /// <param name="alias"></param> /// <param name="registerType"></param> /// <param name="registerAsSingleton"></param> /// <param name="autoWiringMode"></param> public static void RegisterTypeIfMissing(this IApplicationContext container, string alias, Type registerType, bool registerAsSingleton, AutoWiringMode autoWiringMode, bool lazyInit = false) { Type typeToRegister = registerType; if (!container.ContainsObjectDefinition(alias)) { DefaultObjectDefinitionFactory definitionFactory = new DefaultObjectDefinitionFactory(); ObjectDefinitionBuilder builder = ObjectDefinitionBuilder.RootObjectDefinition(definitionFactory, typeToRegister); builder.SetSingleton(registerAsSingleton); builder.SetAutowireMode(autoWiringMode); builder.SetLazyInit(lazyInit); IConfigurableApplicationContext configurableContext = container as IConfigurableApplicationContext; IObjectDefinitionRegistry definitionRegistry = container as IObjectDefinitionRegistry; if (definitionRegistry != null) { definitionRegistry.RegisterObjectDefinition(alias, builder.ObjectDefinition); //if (configurableContext != null) //{ // configurableContext.Refresh(); //} } } }
private void RegisterTypeIfMissing(string alias, Type typeToRegister, bool registerAsSingleton) { if (!container.ContainsObjectDefinition(alias)) { DefaultObjectDefinitionFactory definitionFactory = new DefaultObjectDefinitionFactory(); ObjectDefinitionBuilder builder = ObjectDefinitionBuilder.RootObjectDefinition(definitionFactory, typeToRegister); builder.SetSingleton(registerAsSingleton); builder.SetAutowireMode(AutoWiringMode.Constructor); IConfigurableApplicationContext configurableContext = container as IConfigurableApplicationContext; IObjectDefinitionRegistry definitionRegistry = container as IObjectDefinitionRegistry; if (definitionRegistry != null) { definitionRegistry.RegisterObjectDefinition(alias, builder.ObjectDefinition); //刷新后会导致之间加载的实例对象丢失 //if (configurableContext != null) //{ // configurableContext.Refresh(); //} } } }