Пример #1
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);
            }
        }
Пример #2
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);
        }
Пример #3
0
        private static IApplicationContext CreateContextProgrammatically()
        {
            InitializeCommonLogging();
            GenericApplicationContext ctx = new GenericApplicationContext();

            IObjectDefinitionFactory objectDefinitionFactory = new DefaultObjectDefinitionFactory();


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

            builder.AddPropertyReference("MovieFinder", "AnotherMovieFinder");

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

            builder = ObjectDefinitionBuilder.RootObjectDefinition(objectDefinitionFactory, typeof(ColonDelimitedMovieFinder));
            builder.AddConstructorArg("movies.txt");
            ctx.RegisterObjectDefinition("AnotherMovieFinder", builder.ObjectDefinition);

            ctx.Refresh();

            return(ctx);
        }
        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>
        /// 如果某个类型未在容器注册,则注册该类型
        /// </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();
                    //}
                }
            }
        }
Пример #6
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();
                    //}
                }
            }
        }