开发者

Adding Xml configurations to a Spring ApplicationContext that is already setup

I was wondering if there is a way to do the following:

  • have my wep app startup with its servle-context.xml
  • When, at a certain point, one particular bean in this xml config is instantiated, it will add it's own xml configuration to the application context (or to a child perhaps?).

I'm asking this because I want to pack some functionality in a stand alone library and then reuse it in different projects, so that initializing a bean of this library will load its xml config.

What I wrote is:

public class IrisLibHelper {

Applic开发者_StackOverflow中文版ationContext 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;
}

}

and the irisconfig.xml is:

<?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>

The problem is that it doesn't seem to scan the com.dariodario.iris.controllers package, in fact the controllers don't get mapped! (I've logging debugging on and I don't seen anything).


Why not use the tag <import resource="classpath:applicationConfig.xml" /> ? You can load a spring configuration file which is in jar. In a jar, the Spring XML configuration is always at the root. But if not, you can use this notation: <import resource="${configurablePath}/applicationConfig.xml" /> where configurablePath can be reach by a property place holder or other.

I think this way is cleaner than merging two Spring context.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜