pagid
pagid

Reputation: 13877

Spring and multimodule setups

my (maven based) project is built out of a couple of modules. Basically there's a Core module and a couple of modules which make use of it to provide various services to the outside. The "glue" between these modules is the "parent" module. The "parent" module isn't supposed to hold any code. Something like this: enter image description here

What I'd like to do is to use Spring IoC to inject / autowire the Core parts into the Service parts. But I can't seem to find a way to configure that. Or at least I can't seem to find a way to avoid redundant IoC configuration within the Service parts.

To be more specific - using the example from the Spring documentation - assume that would be the configuration for one of the service modules - how could I move the example.SimpleMovieCatalog configuration parts into the Core module without loosing the ability to inject them within one of the "sibling" modules?

  <?xml version="1.0" encoding="UTF-8"?>
  <beans...>
    <context:annotation-config/>
    <bean class="example.SimpleMovieCatalog">
        <qualifier value="main"/>
    </bean>
    <bean class="example.SimpleMovieCatalog">
        <qualifier value="action"/>
    </bean>
    <bean id="movieRecommender" class="example.MovieRecommender"/>
  </beans>

Upvotes: 4

Views: 3848

Answers (1)

pagid
pagid

Reputation: 13877

I finally figured out a way which seems to work for now:

  1. Each modules holds his spring configuration in /META-INF/spring-<module>.xml
  2. Each module has to keep their code within their own package, otherwise the context:component-scan will not work properly
  3. All modules depending on other modules have to load the foreign module configuration through the "configLocations" of the application context - the foreign module configurations should be referenced with sth. like "classpath*:META-INF/spring-core.xml"

Some remarks

  • The "classpath*:" is what does the magic - because it enables to include other resources from within the embeded jar-files
  • This solution still has one drawback for me, my IDE (Intellij IDEA) is not able to resolve the cross referenced beans. This happens due to the "hack" to load the spring-core.xml through the context directly. Unfortunately I haven't found any other way to so far :(
  • Another thing which caused quite some pain for me was that using sth. like <import resource="classpath*:META-INF/spring-core.xml" /> is understood by my IDE, but doesn't give the desired results at all (e.g. breaks context:component-scan configurations)

Upvotes: 5

Related Questions