向已设置的Spring ApplicationContext添加Xml配置

向已设置的Spring ApplicationContext添加Xml配置,spring,Spring,我想知道是否有办法做到以下几点: 使用servle-context.xml启动我的wep应用程序 当这个xml配置中的一个特定bean在某个时刻被实例化时,它会将自己的xml配置添加到应用程序上下文或子应用程序中?。 我这样问是因为我想在一个独立的库中打包一些功能,然后在不同的项目中重用它,这样初始化这个库的bean就会加载它的xml配置 我写的是: 公共级虹膜帮助器{ ApplicationContext context; ApplicationContext irisContext; @R

我想知道是否有办法做到以下几点:

使用servle-context.xml启动我的wep应用程序 当这个xml配置中的一个特定bean在某个时刻被实例化时,它会将自己的xml配置添加到应用程序上下文或子应用程序中?。 我这样问是因为我想在一个独立的库中打包一些功能,然后在不同的项目中重用它,这样初始化这个库的bean就会加载它的xml配置

我写的是:

公共级虹膜帮助器{

ApplicationContext context;
ApplicationContext irisContext;

@Required
@Autowired
public void setContext(ApplicationContext ctx){

    this.context = ctx;

    ClassPathXmlApplicationContext xap = new ClassPathXmlApplicationContext(ctx);
    xap.setConfigLocation("classpath:com/dariodario/irislib/xmldefs/irisconfig.xml");
    this.irisContext = xap;
}

public ApplicationContext getIrisContext() {
    return irisContext;
}

public void setIrisContext(ApplicationContext irisContext) {
    this.irisContext = irisContext;
}
}

而irisconfig.xml是:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


    <!-- <context:component-scan base-package="com.dariodario"></context:component-scan> -->
    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
        p:synchronizeOnSession="true" />

    <context:component-scan base-package="com.dariodario.iris.controllers"></context:component-scan>
</beans>
问题是它似乎没有扫描com.dariodario.iris.controllers包,事实上控制器没有被映射!我已登录调试,但没有看到任何内容。

为什么不使用该标记?您可以加载jar中的spring配置文件。在jar中,SpringXML配置始终位于根目录下。但如果没有,您可以使用这种表示法:其中可配置路径可由属性占位符或其他人访问


我认为这种方式比合并两个Spring上下文更简洁。

…非常感谢。。。但是,呃。。。我已经知道了。。。除了开玩笑之外,这种方法并不可取,因为jar的用户必须知道xml在jar中的位置。。。