`
elicer
  • 浏览: 130997 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

动态加载 bean 到Spring Context

阅读更多
动态Bean的实现类

import java.io.IOException;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.ResourceEntityResolver;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * 
 * This class used to dynamic loan bean to spring context during runtime
 * 
 *
 */
public class DynamicLoadBean implements ApplicationContextAware{

	private ConfigurableApplicationContext applicationContext = null;
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = (ConfigurableApplicationContext)applicationContext;
	}
	public ConfigurableApplicationContext getApplicationContext() {
		return applicationContext;
	}
	
	/**
	 * 向spring的beanFactory动态地装载bean
	 * @param configLocationString 要装载的bean所在的xml配置文件位置。
spring配置中的contextConfigLocation,同样支持诸如"/WEB-INF/ApplicationContext-*.xml"的写法。
	 */
	public void loadBean(String configLocationString){
		XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader((BeanDefinitionRegistry)getApplicationContext().getBeanFactory());
		beanDefinitionReader.setResourceLoader(getApplicationContext());
		beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(getApplicationContext()));
		try {
			String[] configLocations = new String[]{configLocationString};
			for(int i=0;i<configLocations.length;i++)
				beanDefinitionReader.loadBeanDefinitions(getApplicationContext().getResources(configLocations[i]));
		} catch (BeansException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}




测试类如下
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:data-access-config.xml");   
                DynamicLoadBean dynamicBeanLoad =(DynamicLoadBean)ctx.getBean("dynamicLoadBean");
        
        dynamicBeanLoad.loadBean("data-access-dynamicConfig.xml");
        MagazineService magezineService = (MagazineService)ctx.getBean("magazineService"); 



data-access-config.xml文件中包含原始的bean, data-access-dynamicConfig.xml文件中包含要动态加载的bean的配置文件。

不要忘了把DyanmicLoanBean 配置到原始的spring context 配置文件中也就时data-access-config.xml中


   <bean id="dynamicLoadBean" class="com.spring.dynamic.DynamicLoadBean">       
   </bean>


动态加载的bean的配置文件

   <bean id="magazineService" class="com.ibank.test.openjpa.dao.MagazineServiceImpl">  
      <property name="magazineDao" ref="magazineDao"/>  
   </bean> 


当然了动态加载的bean也可以reference 到原始的context中的bean.比如magazineDao就是在data-access-config.xml中配置的一个bean.
11
1
分享到:
评论
8 楼 OMG-SOCOOL 2016-04-14  
不用单独加载指定的文件
private static ConfigurableApplicationContext applicationContext;
public static Object getBeanByClass(final Class c) {
return applicationContext.getBean(c);
}

public static Object getBeanById(final String id) {
return applicationContext.getBean(id);}
7 楼 heilinshuguang 2013-12-12  

dear
    动态加载bean已经了解。我现在有这样一个需求:spring配置文件system-config.xml,里面配置了服务的配置信息,服务启动后,我又想修改system-config.xml,请问有没有办法在不重启服务的情况下再次加载system-config.xml
6 楼 Bactryki 2011-10-18  
学习了太谢谢了
5 楼 cofspring 2010-10-15  
我也在找这方面的,但是我想直接程序运行过程中,程序自己修改原始配置文件,比如增加一个bean,这样可否自动加载新增加的这个bean,而不是重新读取整个配置文件,有什么方法?
4 楼 ronry 2010-08-08  
需要在原定义文件中定义DynamicLoadBean,侵入性太强
3 楼 siberxu 2010-02-08  
是好文章正在找这方面的,
只是我的为什么会出现
No bean named 'dynamicLoadBean' is defined错误
2 楼 jelly 2010-01-09  
不错,正好需要!
1 楼 ldeng76 2009-09-08  
非常感谢,这个对我很好帮助 .
很奇怪,好文章竟然没人顶.

相关推荐

    spring jar 包详解

    (1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个...

    SpringBoot快速入门

    在使用Spring框架进行开发的过程中,需要配置很多Spring框架包的依赖,如spring-core、spring-bean、spring-context等,而这些配置通常都是重复添加的,而且需要做很多框架使用及环境参数的重复配置,如开启注解、...

    Spring MVC开发配置文件 applicationContext

    Spring Web MVC开发 xml配置文件格式,无bean之类 Spring Web MVC开发配置文件 applicationContext

    Spring-Reference_zh_CN(Spring中文参考手册)

    12.2.2. 在Spring的application context中创建 SessionFactory 12.2.3. HibernateTemplate 12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7. ...

    从零开始学Spring Boot

    1.13 Spring Boot配置ContextPath 1.14 Spring Boot改变JDK编译版本 1.15 处理静态资源(默认资源映射) 1.16 处理静态资源(自定义资源映射) 1.17 Spring Boot定时任务的使用 1.18 Spring Boot使用Druid和监控配置 ...

    springweb3.0MVC注解(附实例)

    -- Spring MVC 的Servlet,它将加载WEB-INF/annomvc-servlet.xml 的 配置文件, 以启动Spring MVC模块--&gt; &lt;servlet-name&gt;annomvc org.springframework.web.servlet.DispatcherServlet &lt;load-on-startup&gt;2 ...

    spring+springmvc+mybatis的整合

    如果它们在SQL映射文件中定义过,则将它们动态定义为一个Spring Bean, 这样,我们在Service中就可以直接注入映射接口的bean 意思就是可以直接ref="dao类名",给你自动注册好了 2.7 写mybatis的配置文件,一个...

    Spring中文帮助文档

    6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 7.2.3. AspectJ切入点表达式 7.2.4. 便利的切入...

    Spring API

    6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 7.2.3. AspectJ切入点表达式 7.2.4. 便利的切入...

    09_高性能应用设计与开发技术.pptx

    高性能应用设计与开发技术Spring性能优化实践 尽量让spring来管理Java对象,尤其是业务服务,数据访问,...对于第三方工具,若Spring已集成,应使用Spring的访问方式,若未集成,则最好先将其集成到Spring中再使用;

    Spring.html

    ClassPathXmlApplicationContext:使用这个工厂创建对象,他会根据scope智能判断是否懒加载,如果是单例则创建容器时就会创建里面bean的实例,如果是多例在获取使用时才会创建bean实例 ...

    spring源代码解析

    下面我们使用ContextLoaderListener作为载入器作一个详细的分析,这个Servlet的监听器是根上下文被载入的地方,也是整个 Spring web应用加载上下文的第一个地方;从加载过程我们可以看到,首先从Servlet事件中得到...

    spring_MVC源码

    本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,现在这一篇补上。下面开始贴代码。 文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下。 先说...

    Spring框架

    spring开发基础部分, ...a&gt; IOC:spring-core.jar\spring-context.jar\spring-test.jar\spring-beans.jar\spring-expression.jar\commons-logging.jar b&gt; 准备一个配置文件 c&gt; 从容器中获取bean的方法

    spring.xls

    * IOC:spring容器控制对象的生命周期:前提条件:在spring容器中的bean必须是单例的 * 创建 * 方式 * 利用默认的构造函数,如果没有默认的构造函数,会报错 * 利用静态工厂方法 * 利用实例工厂方法 * 时机 *...

    Spring AOP配置源码

    此单元测试基于spring的AbstractJUnit4SpringContextTests,你需要添加spring的关于单元测试的支持 在类上标注@ContextConfiguration(locations="classpath:applicationContext.xml")意思是去classpath路径下加载...

    最全中文注释版Spring4源码

    Spring4最新源码,包含Bean,Context,Web等等,每个类添加中文注释,让你轻松读懂Spring4的源码。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

    spring-data-jpa-extra:带有模板动态查询的spring数据jpa(例如

    spring-data-jpa-extra可以解决三个问题: 动态本地查询支持,例如mybatis 返回类型可以是任何东西没有代码,只有sql例子首先添加ComponentScan 通过java bean @ComponentScan ({ " com.slyak " , " your.base....

    整合struts2和spring源代码(可以直接在tomcat中运行)

    注意:如果让spring来创建对象可以将action中的class属性中的值为spring 中bean 中的id值,如果想让struts来创建可以直接指定类 附加: 整合原理 : Struts2与Spring的集成要用到Spring插件包struts2-spring-plugin...

    自定义标签中@Autowired的属性为null

    自定义标签中@Autowired的属性为null 解决办法:两步 1.新建一个类SpringContext,实现接口...2.spring.xml中添加&lt;bean id="" class="com............SpringContext"&gt; 3.使用SpingContext.getBean("bean名");获取

Global site tag (gtag.js) - Google Analytics