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

I am creating an application in which I shared a project in different modules, such as (domain, repository, service and network), as well as many common maven projects for sending mail, formatting text, etc. I also use Spring.

Currently, I only have a Spring application context file in a web project. However, since I am creating common projects for text formatting, etc. that encapsulate libraries (like freemarker) from a real application that I don't like, I have to specify the library dependent configuration in the Spring application context file in the web project.

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

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

+7
java spring java-ee spring-mvc maven
source share
2 answers

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

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 imported from modules.

This sample code is copied from an AppFuse application, see how this application is configured. To create a sample multimodal 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 

Literature:

+3
source share

I’m Boris Spider’s second comment about annotation-based configuration.

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

The second step is to configure the parent project for the component for scanning module packages.

Finally, if something isn’t completely processed by annotations, most likely something needs to be carefully configured to work inside the parent.

Below is a new link in annotation-based customization. In most cases, you can process one or two simple annotations. Check out @Value , which you can use to set values ​​from properties, etc.

+1
source share

All Articles