Pergunta

There is a maven-project which makes .ear file which contains a .war and .jar's. All these modules use Spring configuration files which are loaded with ContextLoaderListener.

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:context.xml</param-value>
</context-param>

And actual problem is that our .war-module has SpringBeans which depend on some other SpringBeans which are located in .jar-modules. During the start of application Spring context cannot be initialized because of wrong order of finding those context.xml's.

The question is, if it is possible somehow to set the correct order of loading spring contexts in web.xml?

Thanks in advance.

Foi útil?

Solução

You can change configuration file names for WAR and JAR - my-war-beans.xml & my-jar-beans.xml. Then define ContextLoaderListener so that it uses my-war-beans.xml (idea is to provide only one file as configuration entry point):

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:my-war-beans.xml</param-value>
</context-param>

And then just import beans from my-jar-beans.xml in my-war-beans.xml:

<beans> 

    <!-- Include beans from JAR -->
    <import resource="classpath*:my-jar-beans.xml"/>

    <!-- List beans from WAR -->
    <bean id="myBean" class="myclass" />

</beans>

Make sure that your bean definition files have unique names. Classpath is flat so if names are same - this approach will be vulnerable to jar hell problems. Note that you may need to change URLs like "classpath*:my-jar-beans.xml" to something else, depends on where you store files inside a JAR/WAR.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top