8

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.

LuckyLuke
  • 47,771
  • 85
  • 270
  • 434
  • You should probably use [JavaConfig](http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/) rather than the somewhat dated XML based configuration. For true abstraction you need to look at an OSGI type layout with one module for the API that is a compile time dependency and one module for the code that is runtime. You can annotate the classes in the code module and they will be automatically picked up by classpath scanning. – Boris the Spider Jan 19 '14 at 18:33
  • I cant change to JavaConfig now. What to do when using XML? – LuckyLuke Jan 19 '14 at 20:13
  • You can use XML based configuration with annotations. You define which packages to scan for annotations in the XML and Spring will pick up the annotations automagically. – Boris the Spider Jan 19 '14 at 20:15
  • @BoristheSpider I am using annotations like `@Component`, `@Repository` etc. but sometimes I use XML for activating Spring MVC by using the namespaces. I can't move that configuration to JavaConfig at the moment. So how should I organize those XML files? – LuckyLuke Jan 19 '14 at 20:40

2 Answers2

3

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:

Community
  • 1
  • 1
MariuszS
  • 30,646
  • 12
  • 114
  • 155
1

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.

Community
  • 1
  • 1
Peter Lamberg
  • 8,151
  • 3
  • 55
  • 69
  • I cant change to JavaConfig now. What to do when using XML? – LuckyLuke Jan 19 '14 at 20:13
  • In my experience transitioning to annotations/JavaConfig can be done smoothly and incrementally. After a couple of years of development you barely have any XML at all. You can include the xml files as resources in the modules and import them directly from the parent xml as shown in here http://stackoverflow.com/a/5115384/1148030 or from web.xml as in @MariuszS answer. – Peter Lamberg Jan 21 '14 at 15:15