Ejemplo n.º 1
0
        void Init()
        {
            if (initialized)
            {
                return;
            }

            lock (componentProperties)
            {
                foreach (var t in componentProperties.Keys)
                {
                    var builder = ObjectDefinitionBuilder.RootObjectDefinition(factory, t)
                                  .SetAutowireMode(AutoWiringMode.AutoDetect)
                                  .SetSingleton(typeHandleLookup[t] == DependencyLifecycle.SingleInstance);

                    componentProperties[t].Configure(builder);

                    IObjectDefinition def = builder.ObjectDefinition;
                    context.RegisterObjectDefinition(t.FullName, def);
                }
            }

            initialized = true;
            context.Refresh();
        }
Ejemplo n.º 2
0
        private static void Register <TSerivice, TImplementation>(IConfigurableListableObjectFactory confObjFactory)
        {
            ObjectDefinitionBuilder odb = ObjectDefinitionBuilder.RootObjectDefinition(objectDefinitionFactory, typeof(TImplementation)).
                                          SetAutowireMode(AutoWiringMode.Constructor);

            confObjFactory.RegisterObjectDefinition(typeof(TSerivice).FullName, odb.ObjectDefinition);
        }
        /// <summary>
        /// Registers the specified implementation type.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <param name="implementationType">Type of the implementation.</param>
        /// <param name="name">The name.</param>
        public void Register <TService>(Type implementationType, string name)
            where TService : class
        {
            var b = ObjectDefinitionBuilder.RootObjectDefinition(_factory, implementationType);

            _container.RegisterObjectDefinition(name, b.ObjectDefinition);
        }
Ejemplo n.º 4
0
        private static void RegisterPrototype <TImplementation>(IConfigurableListableObjectFactory confObjFactory)
        {
            ObjectDefinitionBuilder odb = ObjectDefinitionBuilder.RootObjectDefinition(objectDefinitionFactory, typeof(TImplementation))
                                          .SetSingleton(false).SetAutowireMode(AutoWiringMode.AutoDetect);

            confObjFactory.RegisterObjectDefinition(typeof(TImplementation).FullName, odb.ObjectDefinition);
        }
Ejemplo n.º 5
0
        private static IApplicationContext CreateContextProgrammaticallyWithAutoWire()
        {
            InitializeCommonLogging();
            GenericApplicationContext ctx = new GenericApplicationContext();

            IObjectDefinitionFactory objectDefinitionFactory = new DefaultObjectDefinitionFactory();


            //Create MovieLister and dependency on
            ObjectDefinitionBuilder builder =
                ObjectDefinitionBuilder.RootObjectDefinition(objectDefinitionFactory, typeof(MovieLister));

            builder.AddPropertyReference("MovieFinder", "BogusNameOfDependency")
            .SetAutowireMode(AutoWiringMode.ByType);

            ctx.RegisterObjectDefinition("MyMovieLister", builder.ObjectDefinition);

            builder = ObjectDefinitionBuilder.RootObjectDefinition(objectDefinitionFactory, typeof(ColonDelimitedMovieFinder));
            builder.AddConstructorArg("movies.txt")
            .SetAutowireMode(AutoWiringMode.ByType);

            ctx.RegisterObjectDefinition("AnotherMovieFinder", builder.ObjectDefinition);

            ctx.Refresh();

            return(ctx);
        }
        /// <summary>
        /// Registers the specified implementation type.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <param name="implementationType">Type of the implementation.</param>
        public void Register <TService>(Type implementationType)
            where TService : class
        {
            var b = SetLifetime(ObjectDefinitionBuilder.RootObjectDefinition(_factory, implementationType));

            _container.RegisterObjectDefinition(SpringServiceLocator.GetName(implementationType), b.ObjectDefinition);
        }
        /// <summary>
        /// Registers the specified name.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
        /// <param name="name">The name.</param>
        public void Register <TService, TImplementation>(string name)
            where TService : class
            where TImplementation : class, TService
        {
            var b = SetLifetime(ObjectDefinitionBuilder.RootObjectDefinition(_factory, typeof(TImplementation)));

            _container.RegisterObjectDefinition(name, b.ObjectDefinition);
        }
Ejemplo n.º 8
0
        public static void RegisterSingleton(this IConfigurableApplicationContext context, Type type)
        {
            ObjectDefinitionBuilder definitionBuilder = ObjectDefinitionBuilder.RootObjectDefinition(new DefaultObjectDefinitionFactory(), type)
                                                        .SetAutowireMode(AutoWiringMode.AutoDetect)
                                                        .SetSingleton(true);

            context.ObjectFactory.RegisterObjectDefinition(type.FullName, definitionBuilder.ObjectDefinition);
        }
Ejemplo n.º 9
0
        public static void RegisterPrototype <T>(this IConfigurableApplicationContext context)
        {
            ObjectDefinitionBuilder definitionBuilder = ObjectDefinitionBuilder.RootObjectDefinition(new DefaultObjectDefinitionFactory(), typeof(T))
                                                        .SetAutowireMode(AutoWiringMode.AutoDetect)
                                                        .SetSingleton(false);

            context.ObjectFactory.RegisterObjectDefinition(Guid.NewGuid().ToString(), definitionBuilder.ObjectDefinition);
        }
Ejemplo n.º 10
0
        public void Register(Type componentType, Type implementationType, Lifestyle lifeStyle)
        {
            var factory = new DefaultObjectDefinitionFactory();
            var builder = ObjectDefinitionBuilder.RootObjectDefinition(factory, implementationType);

            builder.SetAutowireMode(AutoWiringMode.AutoDetect);
            builder.SetSingleton(lifeStyle == Lifestyle.Singleton);
            context.RegisterObjectDefinition(componentType.FullName, builder.ObjectDefinition);
        }
Ejemplo n.º 11
0
        /// <summary>
        ///   Adds a proxy creator for Spring.NET AOP.
        /// </summary>
        /// <param name = "proxyCreatorName"></param>
        /// <param name = "objectNames">Supports the '*' wildcard.</param>
        /// <param name = "interceptorNames">The object name(s) of the AOP interceptors to apply to the matched objects in objectNames.</param>
        public static void AddProxyCreator(string proxyCreatorName, IList <string> objectNames,
                                           IList <string> interceptorNames)
        {
            var builder = ObjectDefinitionBuilder.RootObjectDefinition(new DefaultObjectDefinitionFactory(),
                                                                       typeof(ObjectNameAutoProxyCreator))
                          .AddPropertyValue("ObjectNames", objectNames)
                          .AddPropertyValue("InterceptorNames", interceptorNames);

            context.RegisterObjectDefinition(proxyCreatorName, builder.ObjectDefinition);
        }
Ejemplo n.º 12
0
        private void Register(Type implementationType, Type serviceType, bool singleton)
        {
            var builder = ObjectDefinitionBuilder.RootObjectDefinition(this.factory, implementationType)
                          .SetAutowireMode(AutoWiringMode.AutoDetect)
                          .SetSingleton(singleton);

            var key = string.Format("{0} ({1})", serviceType, implementationType);

            this.context.RegisterObjectDefinition(key, builder.ObjectDefinition);
        }
Ejemplo n.º 13
0
        public static void AddObjectDefinition(
            string objectName,
            Type implementationType,
            bool singleton)
        {
            var builder = ObjectDefinitionBuilder.RootObjectDefinition(new DefaultObjectDefinitionFactory(),
                                                                       implementationType)
                          .SetAutowireMode(AutoWiringMode.ByType)
                          .SetSingleton(singleton);

            context.RegisterObjectDefinition(objectName, builder.ObjectDefinition);
        }
Ejemplo n.º 14
0
        public void RegisterByKey(Type componentType, Type implementationType, String instanceKey)
        {
            if (this.context == null)
            {
                throw new InvalidOperationException("GenericApplicationContext is not initialized.");
            }

            var factory = new DefaultObjectDefinitionFactory();
            var builder = ObjectDefinitionBuilder.RootObjectDefinition(factory, implementationType);

            builder.SetAutowireMode(AutoWiringMode.AutoDetect);
            this.context.RegisterObjectDefinition(instanceKey, builder.ObjectDefinition);
        }
Ejemplo n.º 15
0
        /// <summary>
        ///   Adds the lemming to the current context
        /// </summary>
        /// <param name = "lemming">The lemming to add</param>
        protected override void AddToContext(Lemming lemming)
        {
            var builder = ObjectDefinitionBuilder.RootObjectDefinition(new DefaultObjectDefinitionFactory(),
                                                                       lemming.ConcreteType);

            builder.SetAutowireMode(AutoWiringMode.ByName);
            builder.SetSingleton(lemming.Singleton);

            foreach (var injection in lemming.Injections)
            {
                injection.Accept(new SpringInjector(injection.Property.Name, builder));
            }

            objectDefinitionRegistry.RegisterObjectDefinition(lemming.UniqueName, builder.ObjectDefinition);
        }
Ejemplo n.º 16
0
        public static void RegisterSingleton(this IConfigurableApplicationContext context, Type type, string name, params object[] constructorArguments)
        {
            ObjectDefinitionBuilder definitionBuilder = ObjectDefinitionBuilder.RootObjectDefinition(new DefaultObjectDefinitionFactory(), type)
                                                        .SetAutowireMode(AutoWiringMode.AutoDetect)
                                                        .SetSingleton(true);

            if (constructorArguments != null && constructorArguments.Length > 0)
            {
                foreach (object argument in constructorArguments)
                {
                    definitionBuilder.AddConstructorArg(argument);
                }
            }

            context.ObjectFactory.RegisterObjectDefinition(name, definitionBuilder.ObjectDefinition);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Registers an object in the Spring Container
        /// </summary>
        /// <param name="name">the name of the new object</param>
        /// <param name="target">the Type of the new object</param>
        /// <param name="registerAsSingleton">if the new object will be singleron scoped</param>
        protected void RegisterTypeIfMissing(string name, Type target, bool registerAsSingleton)
        {
            if (springContainer.ContainsObject(name))
            {
                return;
            }
            else
            {
                IObjectDefinitionRegistry definitionRegistry = springContainer as IObjectDefinitionRegistry;
                var objectDefinitionFactory = new DefaultObjectDefinitionFactory();
                var builder = ObjectDefinitionBuilder.RootObjectDefinition(objectDefinitionFactory, target);

                builder.SetSingleton(registerAsSingleton);
                builder.SetAutowireMode(AutoWiringMode.Constructor);

                definitionRegistry.RegisterObjectDefinition(name, builder.ObjectDefinition);
            }
        }
Ejemplo n.º 18
0
 protected virtual void RegisterConsumersFrom(Assembly assembly)
 {
     assembly.GetTypes()
     .Where(t => typeof(IMessageConsumer).IsAssignableFrom(t) &&
            !typeof(IOccasionalMessageConsumer).IsAssignableFrom(t) &&
            IsTypeAcceptableForThisBootStrapper(t) &&
            !t.IsInterface &&
            !t.IsAbstract)
     .ToList()
     .ForEach(type =>
     {
         ObjectDefinitionBuilder definitionBuilder = ObjectDefinitionBuilder
                                                     .RootObjectDefinition(new DefaultObjectDefinitionFactory(), type)
                                                     .SetAutowireMode(AutoWiringMode.Constructor)
                                                     .SetSingleton(false);
         applicationContext.ObjectFactory.RegisterObjectDefinition(type.FullName, definitionBuilder.ObjectDefinition);
     }
              );
 }
Ejemplo n.º 19
0
        private static IApplicationContext CreateContextMixXmlAndProgrammatic()
        {
            GenericApplicationContext ctx = new GenericApplicationContext();

            IObjectDefinitionReader objectDefinitionReader = new XmlObjectDefinitionReader(ctx);

            objectDefinitionReader.LoadObjectDefinitions("assembly://Spring.IocQuickStart.MovieFinder/Spring.IocQuickStart.MovieFinder/AppContextContribution.xml");

            IObjectDefinitionFactory objectDefinitionFactory = new DefaultObjectDefinitionFactory();
            ObjectDefinitionBuilder  builder =
                ObjectDefinitionBuilder.RootObjectDefinition(objectDefinitionFactory, typeof(ColonDelimitedMovieFinder));

            builder.AddConstructorArg("movies.txt");
            ctx.RegisterObjectDefinition("AnotherMovieFinder", builder.ObjectDefinition);


            ctx.Refresh();

            return(ctx);
        }
Ejemplo n.º 20
0
        public static void RegisterDefaultConversationAop(this IConfigurableListableObjectFactory confObjFactory)
        {
            var metaInfoStore = new ReflectionConversationalMetaInfoSource();

            confObjFactory.RegisterInstance(metaInfoStore);
            // register advisor definition
            var pc =
                ObjectDefinitionBuilder.RootObjectDefinition(ObjectDefinitionFactory,
                                                             typeof(ConversationInterceptor))
                .SetAutowireMode(AutoWiringMode.AutoDetect)
                .SetSingleton(false);

            confObjFactory.RegisterObjectDefinition("PersistentConversationalInterceptor", pc.ObjectDefinition);
            var postProcessor = new ConversationalAttributeAutoProxyCreator(metaInfoStore)
            {
                ObjectFactory    = confObjFactory,
                InterceptorNames = new[] { "PersistentConversationalInterceptor" }
            };

            confObjFactory.AddObjectPostProcessor(postProcessor);
        }
Ejemplo n.º 21
0
        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();
                    //}
                }
            }
        }
        /// <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();
                    //}
                }
            }
        }
        public void Factory_WhenObtainedFromContainer_HasContainerConfiguration()
        {
            RootObjectDefinition productTemplate = new RootObjectDefinition();

            productTemplate.PropertyValues.Add(new Spring.Objects.PropertyValue("ChannelManager.ChannelFactory.Credentials.UserName.UserName", "un"));
            productTemplate.PropertyValues.Add(new Spring.Objects.PropertyValue("ChannelManager.ChannelFactory.Credentials.UserName.Password", "pw"));
            GenericApplicationContext context = new GenericApplicationContext();
            IObjectDefinitionFactory  objectDefinitionFactory = new DefaultObjectDefinitionFactory();
            ObjectDefinitionBuilder   builder = ObjectDefinitionBuilder.RootObjectDefinition(objectDefinitionFactory, typeof(ChannelManagerFactoryObject));

            builder.AddPropertyValue("EndpointConfigurationName", EndpointName);
            builder.AddPropertyValue("ChannelType", typeof(IService));
            builder.AddPropertyValue("ProductTemplate", productTemplate);
            context.RegisterObjectDefinition("myChannelFactoryObject", builder.ObjectDefinition);

            var obj = context.GetObject("myChannelFactoryObject") as ProxyChannel <IService>;

            Assert.IsNotNull(obj);
            ChannelManagerBase <IService> channelManager = obj.ChannelManager as ChannelManagerBase <IService>;

            Assert.IsNotNull(channelManager);
            Assert.AreEqual("un", ((ChannelFactory <IService>)channelManager.ChannelFactory).Credentials.UserName.UserName);
            Assert.AreEqual("pw", ((ChannelFactory <IService>)channelManager.ChannelFactory).Credentials.UserName.Password);
        }
 /// <summary>
 /// Convenience method to create a builder for a root object definition.
 /// </summary>
 /// <param name="objectType">Type of the object.</param>
 /// <returns>a builder for a root object definition</returns>
 public ObjectDefinitionBuilder CreateRootObjectDefinitionBuilder(Type objectType)
 {
     return(ObjectDefinitionBuilder.RootObjectDefinition(this.readerContext.ObjectDefinitionFactory, objectType));
 }
        /// <summary>
        /// Registers the specified service type.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <param name="implementationType">Type of the implementation.</param>
        /// <param name="name">The name.</param>
        public void Register(Type serviceType, Type implementationType, string name)
        {
            var b = SetLifetime(ObjectDefinitionBuilder.RootObjectDefinition(_factory, implementationType));

            _container.RegisterObjectDefinition(name, b.ObjectDefinition);
        }
        /// <summary>
        /// Registers the specified service type.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        public void Register(Type serviceType)
        {
            var b = SetLifetime(ObjectDefinitionBuilder.RootObjectDefinition(_factory, serviceType));

            _container.RegisterObjectDefinition(SpringServiceLocator.GetName(serviceType), b.ObjectDefinition);
        }