动态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.
分享到:
相关推荐
(1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个...
在使用Spring框架进行开发的过程中,需要配置很多Spring框架包的依赖,如spring-core、spring-bean、spring-context等,而这些配置通常都是重复添加的,而且需要做很多框架使用及环境参数的重复配置,如开启注解、...
Spring Web MVC开发 xml配置文件格式,无bean之类 Spring Web MVC开发配置文件 applicationContext
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. ...
1.13 Spring Boot配置ContextPath 1.14 Spring Boot改变JDK编译版本 1.15 处理静态资源(默认资源映射) 1.16 处理静态资源(自定义资源映射) 1.17 Spring Boot定时任务的使用 1.18 Spring Boot使用Druid和监控配置 ...
-- Spring MVC 的Servlet,它将加载WEB-INF/annomvc-servlet.xml 的 配置文件, 以启动Spring MVC模块--> <servlet-name>annomvc org.springframework.web.servlet.DispatcherServlet <load-on-startup>2 ...
如果它们在SQL映射文件中定义过,则将它们动态定义为一个Spring Bean, 这样,我们在Service中就可以直接注入映射接口的bean 意思就是可以直接ref="dao类名",给你自动注册好了 2.7 写mybatis的配置文件,一个...
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. 便利的切入...
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性能优化实践 尽量让spring来管理Java对象,尤其是业务服务,数据访问,...对于第三方工具,若Spring已集成,应使用Spring的访问方式,若未集成,则最好先将其集成到Spring中再使用;
ClassPathXmlApplicationContext:使用这个工厂创建对象,他会根据scope智能判断是否懒加载,如果是单例则创建容器时就会创建里面bean的实例,如果是多例在获取使用时才会创建bean实例 ...
下面我们使用ContextLoaderListener作为载入器作一个详细的分析,这个Servlet的监听器是根上下文被载入的地方,也是整个 Spring web应用加载上下文的第一个地方;从加载过程我们可以看到,首先从Servlet事件中得到...
本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,现在这一篇补上。下面开始贴代码。 文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下。 先说...
spring开发基础部分, ...a> IOC:spring-core.jar\spring-context.jar\spring-test.jar\spring-beans.jar\spring-expression.jar\commons-logging.jar b> 准备一个配置文件 c> 从容器中获取bean的方法
* IOC:spring容器控制对象的生命周期:前提条件:在spring容器中的bean必须是单例的 * 创建 * 方式 * 利用默认的构造函数,如果没有默认的构造函数,会报错 * 利用静态工厂方法 * 利用实例工厂方法 * 时机 *...
此单元测试基于spring的AbstractJUnit4SpringContextTests,你需要添加spring的关于单元测试的支持 在类上标注@ContextConfiguration(locations="classpath:applicationContext.xml")意思是去classpath路径下加载...
Spring4最新源码,包含Bean,Context,Web等等,每个类添加中文注释,让你轻松读懂Spring4的源码。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
spring-data-jpa-extra可以解决三个问题: 动态本地查询支持,例如mybatis 返回类型可以是任何东西没有代码,只有sql例子首先添加ComponentScan 通过java bean @ComponentScan ({ " com.slyak " , " your.base....
注意:如果让spring来创建对象可以将action中的class属性中的值为spring 中bean 中的id值,如果想让struts来创建可以直接指定类 附加: 整合原理 : Struts2与Spring的集成要用到Spring插件包struts2-spring-plugin...
自定义标签中@Autowired的属性为null 解决办法:两步 1.新建一个类SpringContext,实现接口...2.spring.xml中添加<bean id="" class="com............SpringContext"> 3.使用SpingContext.getBean("bean名");获取