gotch4
gotch4

Reputation: 13289

Adding Xml configurations to a Spring ApplicationContext that is already setup

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

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 {

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;
}

}

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).

Upvotes: 0

Views: 4862

Answers (1)

Vincent Devillers
Vincent Devillers

Reputation: 1628

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.

Upvotes: 1

Related Questions