Java – Using spring beans from other modules

classloaderjakarta-eejavaspring

I imagine this is simple – but I can't find the right combination of search terms to get an answer. If I have a multi-module application, how do I get the beans in module A available to the beans in module B.

The project setup looks a little like this:

project.ear/module-a.jar/resources/beans.xml
project.ear/module-a.jar/java/foo/bar.class
project.ear/module-b.war/java/foo/barFactory.class

The beans.xml isn't accessible by either classpath, or filesystem. Is there a way to do this? Or should I be doing everything differently?

Best Answer

I think there is an easier solution than these 3 skaffman mentioned above.

  1. Put in a module-b.war/WEB-INF/web.xml these lines:

    <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>

    classpath*:/resources/beans.xml

    </param-value>

    </context-param>

    This enables loading every /resources/beans.xml from any piece of classpath (i.e. any jar on classpath) which is enough for you.

  2. You should load a module-a.jar as a part of a module-b.war (module-a.jar resides in module-b.war/WEB-INF/lib location).

I have very similar granularity in my project: business-logic.jar with its beans' configuration and frontend.war which uses beans configured in previous one via ref="someBean". It works.