Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring configuration in multi-module project

I'm new to Spring and got a situation that single project with multiple modules including one web module. Web module uses Spring MVC, but I was wondering if I can have main Spring configuration at project level that takes care of whole project, so that I can take full advantages of Spring framework.

main
   -module1
   -module2
   -web
      +spring3.1, spring-security

What would be the best setting for this case?

like image 550
Daniel Kim Avatar asked Jan 19 '12 20:01

Daniel Kim


2 Answers

We have this kind of architecture where I work. We decided to use the ContextLoaderListener (Spring Event Listener) in the web module.

Then, in the serverApplicationContext.xml, we import all the modules context files :

<import resource="classpath*:module1ApplicationContext.xml" />
<import resource="classpath*:module2ApplicationContext.xml" />
...

Thus you leverage the spring context loading during the web application context initialization.

like image 134
webpat Avatar answered Sep 28 '22 01:09

webpat


The build layout and runtime CLASSPATH are two different things. Even if you define separate applicationContext.xml files or @Configuration classes in different modules, they might get merged into a single CLASSPATH.

That being said module1 and module2 might declare their own contexts, but since the CLASSPATH is merged at runtime, only a single main context will be created. Also if you choose to use CLASSPATH scanning in one module, it might pick-up classes (beans) annotated with @Service in other modules.

In web module, which should also have dependencies on Spring core libraries, will also depend on spring-web, MVC and spring-security. This module will create child web context, that has access to main context but not the other way around.

Obviously you should have only a single copy of each library in your uber-JAR (ear?)

like image 45
Tomasz Nurkiewicz Avatar answered Sep 28 '22 02:09

Tomasz Nurkiewicz