动态Bean的实现类
测试类如下
data-access-config.xml文件中包含原始的bean, data-access-dynamicConfig.xml文件中包含要动态加载的bean的配置文件。
不要忘了把DyanmicLoanBean 配置到原始的spring context 配置文件中也就时data-access-config.xml中
动态加载的bean的配置文件
当然了动态加载的bean也可以reference 到原始的context中的bean.比如magazineDao就是在data-access-config.xml中配置的一个bean.
http://elicer.iteye.com/blog/438026
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.
http://elicer.iteye.com/blog/438026