我们怎样才能在Spring中创建多个IOC容器?

时间:2015-03-28 07:37:31

标签: java spring spring-mvc

我想知道如何在Spring的单个项目中创建多个IOC容器?

2 个答案:

答案 0 :(得分:1)

这是

    ClassPathXmlApplicationContext ctx1 = new ClassPathXmlApplicationContext("context1.xml");
    ClassPathXmlApplicationContext ctx2 = new ClassPathXmlApplicationContext("context2.xml");

答案 1 :(得分:1)

您只需创建两个独立的IOC容器即可创建它们。

ApplicationContext contextA = new GenericXmlApplicationContext("classpath:contextA.xml");
ApplicationContext contextB = new GenericXmlApplicationContext("classpath:contextB.xml");

您还可以通过AbstractApplicationContext.setParent(ApplicationContext)

创建两个或多个依赖于父/子关系的IOC包含(如Spring Core Context和Spring Web Context(ContextLoaderListener or not?))
ApplicationContext parent =...

AbstractApplicationContext contextA = new GenericXmlApplicationContext("classpath:contextA.xml");
contextA.setParent(parent);
AbstractApplicationContext contextB = new GenericXmlApplicationContext("classpath:contextB.xml");
contextB.setParent(parent);