Spring and multi-module settings

My (maven based) project is built from several modules. Basically, there is a Core module and several modules that use it to provide various services outside. The “glue” between these modules is the “parent” module. The "parent" module must not contain any code. Something like that: enter image description here

What I would like to do is use Spring IoC to input / prepare the main parts in the Service part. But I can not find a way to configure this. Or at least I cannot find a way to avoid redundant IoC configuration in parts of the Service.

More specifically — using the example from the Spring documentation — suppose it will be a configuration for one of the service modules — how could I move example.SimpleMovieCatalog configuration details to the Core module without losing the ability to embed them in 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> 
+4
source share
1 answer

I finally figured out how this works:

  • Each module contains a spring configuration in /META-INF/spring-<module>.xml
  • Each module must store its own code in its own package, otherwise context:component-scan will not work properly
  • All modules that depend on other modules must load the configuration of the external module through the "configLocations" of the application context - the configuration of the external module should be referred to sth. like "classpath *: META-INF / spring -core.xml"

Some notes

  • "classpath *:" is what magic does, because it allows you to include other resources from embedded jar files.
  • This solution still has one drawback for me, my IDE (Intellij IDEA) cannot allow cross reference beans. This is due to a “hack” for loading spring -core.xml through the context directly. Unfortunately, I have not yet found another way :(
  • Another thing that caused me some pain was that using sth. for example, <import resource="classpath*:META-INF/spring-core.xml" /> is understood by my IDE, but does not give the desired results at all (for example, context:component-scan configuration breaks)
+5
source

All Articles