如何在Spring中将两个ApplicationContext彼此合并? [英] How to merge two ApplicationContext into one another in Spring?

查看:382
本文介绍了如何在Spring中将两个ApplicationContext彼此合并?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的spring java模块中有两个上下文

I have two contexts in my spring java module

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "SpringBeans.xml");

ClassPathXmlApplicationContext helloContext = new ClassPathXmlApplicationContext("HelloBeans.xml");

具有两个不同的xml文件.现在,我必须从context获取HelloBeans.xml的bean,而从helloContext获取SpringBeans.xml的bean,而无需刷新上下文.

with two different xml files. Now I have to get beans of HelloBeans.xml from context and beans of SpringBeans.xml from helloContext without refreshing the contexts.

推荐答案

找不到我想要的东西,但这是我所能做的最好的事情:

Couldn't find what I was looking for, but this is the best I could do:

PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver(
            context.getClassLoader());
Resource resource = pathMatchingResourcePatternResolver
            .getResource("classpath:HelloBeans.xml");
AutowireCapableBeanFactory factory = context
            .getAutowireCapableBeanFactory();
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(
            registry);
xmlReader.loadBeanDefinitions(resource);

这篇关于如何在Spring中将两个ApplicationContext彼此合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆