Spring 动态注入javaBean

在项目中有时会有这样的需求:要求在程序运行时动态注入javaBean,显然新加的Bean不能写在Spring的配置文件中。
因为配置文件修改后需要重启服务器。还好spring为我们提供了动态注入bean的接口:

             //由配置文件得到

                 applicationContext

ApplicationContext applicationContext =new ClassPathXmlApplicationContext("applicationContext.xml");
BeanDefinition bean = new GenericBeanDefinition();

//类的全路径
        bean.setBeanClassName("com.xh.spring.bean.Dog");
        DefaultListableBeanFactory fty = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
        //注册Bean
fty.registerBeanDefinition("dog101", bean);
        //以后便可以取出Bean了
Dog mydog = (Dog)applicationContext.getBean("dog101");
        mydog.setColor("red");
        mydog.setName("tom");
        System.out.println(mydog.ww());

时间: 2024-07-31 08:20:59

Spring 动态注入javaBean的相关文章

java-关于spring+hibernate 注入sessionFactory为null的问题

问题描述 关于spring+hibernate 注入sessionFactory为null的问题 最近一个项目需要用到struts2+spring+hibernate组合框架,我在一点一点的做配置,但做到注入SessionFactory时,一直不成功,在网上搜的一些解决方案也都不适用.故在此发布,希望高手们不吝赐教.web.xml的配置为: <?xml version=""1.0"" encoding=""UTF-8""

请问spring构造注入的问题

问题描述 <beans:bean id="securityMetadataSource" class="com.security.MyInvocationSecurityMetadataSource"> <beans:constructor-arg><beans:ref bean="resourcesService"/></beans:constructor-arg></beans:bean&

Spring 依赖注入方式详解

阅读目录 1.Set注入 2.构造器注入 3.静态工厂的方法注入 4.实例工厂的方法注入 5.总结    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程序员实例化,而是通过spring容器帮我们new指定实例并且将实例注入到需要该对象的类中.依赖注入的另一种说法是"控制反转",通俗的理解是:平常我们new一个实例,这个实例的控制权是我

图片-spring security 注入的时候一直报

问题描述 spring security 注入的时候一直报 这个是什么问题了?求知啊··· 解决方案 我好奇楼主用的哪个编辑器 解决方案二: springMVC+mybatis+spring security<三>:使用数据库管理资源

spring 注入 多线程-spring多线程注入报错,求大神帮忙解决一下。。谢谢!!

问题描述 spring多线程注入报错,求大神帮忙解决一下..谢谢!! Error creating bean with name 'transactionManager': Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implement

spring依赖注入测试时出错

问题描述 spring依赖注入测试时出错 错误如下: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'analysisService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExcepti

spring 依赖注入的困惑 关于子类和父类

问题描述 spring 依赖注入的困惑 关于子类和父类 有一个Son类 package cn.extend; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; public class Son extends Father { } 还有一个父类Father

cxf spring @Autowired注入null

问题描述 cxf spring @Autowired注入null 配置文件 <!-- 配置文件资源导入 --> <import resource=""classpath:META-INF/cxf/cxf.xml""/> <import resource=""classpath:META-INF/cxf/cxf-extension-soap.xml""/> <import resourc

[ASP.NET MVC] 利用动态注入HTML的方式来设计复杂页面

原文:[ASP.NET MVC] 利用动态注入HTML的方式来设计复杂页面 随着最终用户对用户体验需求的不断提高,实际上我们很多情况下已经在按照桌面应用的标准来设计Web应用,甚至很多Web页面本身就体现为一个单一的页面.对于这种复杂的页面,我们在设计的时候不可以真的将所有涉及的元素通通至于某个单独的View中,将复杂页面相对独立的内容"分而治之"才是设计之道.我们可以借鉴Smart Clent应用的设计方式:将一个Windows Form作为应用的容器(Smart Client Sh