Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should each module in a Maven project have its own Spring application context?

I am creating an application where I have separated the project in different modules such as (domain, repository, service and web) as well as many general purpose maven projects for mail sending, text formatting etc. I am also using Spring.

Currently I have the Spring application context file only in the web project. However, since I am creating general pupose projects for text formatting etc that encapsulates libraries (e.g. freemarker) from the actual application I don't like that I have to specify library dependent configuration in the Spring application context file in the web project.

The question is whether or not it is correct to have a separate Spring applicaiton context file for each module and then import the context files in the projects where I use them. Is this the correct way to do it, or are there better ways?

I am interested in how to solve this when I use XML files, not JavaConfig.

like image 709
LuckyLuke Avatar asked Jan 19 '14 18:01

LuckyLuke


2 Answers

Create applicationContext.xml configuration for all modules and place it in modules, then from web module imports all configuration from all modules.

web.xml

<!-- Context Configuration locations for Spring XML files -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:/applicationContext-resources.xml
        classpath:/applicationContext-dao.xml
        classpath:/applicationContext-service.xml
        classpath*:/applicationContext.xml
        /WEB-INF/applicationContext*.xml
        /WEB-INF/cxf-servlet.xml
        /WEB-INF/security.xml
    </param-value>
</context-param>

In this example all applicationContext.xml are imported from modules.

This example code is copyied from AppFuse application, take look on how this application is configured. To build sample multimodule Spring application with AppFuse use:

mvn archetype:generate -B -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-modular-spring-archetype -DarchetypeVersion=3.0.0 -DgroupId=com.mycompany -DartifactId=myproject -DarchetypeRepository=http://oss.sonatype.org/content/repositories/appfuse

References:

  • Splitting applicationContext to multiple files
  • Appfuse Quickstart
like image 74
MariuszS Avatar answered Sep 30 '22 01:09

MariuszS


I second Boris the Spider's comment about annotation based config.

My solution to the problem is first to use annotation based configuration in modules (and everywhere).

The second step is to configure the parent project to component scan the packages of the modules.

Finally if something is not completely handled by the annotations, it is most likely something that needs careful configuration to work inside the parent anyway.

Here is a newer reference on the annotation based configuration. Most cases can be handled with one or two simple annotations. Check out @Value that can be used to set values from properties etc.

like image 30
Peter Lamberg Avatar answered Sep 30 '22 02:09

Peter Lamberg