Spring Framework in modular design by Apache Maven

2

I created a modular web project using Apache Maven with the following structure:

proj-build
|--- proj-utils
|--- proj-persistence
|--- proj-services
|--- proj-web
'--- proj-ear

Description

proj-build : This groups the modules only to perform the chain build on the modules, ie each project has its own configuration when the build is performed, without any relationship with this project parent (proj-build).

proj-utils : This module brings together utilitarian classes like: Exceptions Classes, Hibernate SessionFactory Classes, etc.

proj-persistence : This module brings together entity classes, DAO classes, DAO interfaces. It has dependencies like JPA, Hibernate, etc.

proj-services : This module brings together classes that implement business logic. It has dependency on the proj-persistence module.

proj-web : This module brings together the Controllers, HTML pages, CSS, Javascript and any other resources you require on the pages. It has dependency with the module proj-services, Spring MVC, Spring Security, JSTL, Java Servlet, etc.

proj-ear : This module has dependency on all other modules except proj-build. This module packages the other modules in an EAR package.

My question is: How do I apply the Spring Framework DI / IoC as a dependency in this modular design? Do I apply dependency on all modules or on a specific module?

    
asked by anonymous 29.06.2014 / 07:45

1 answer

2

The Spring configurations can be distributed by the projects, each one configuring their respective components. This guarantees the possibility of unit tests in each module.

Some modules will have dependencies. For example, proj-services will probably need proj-persistence , right? In this case there are two outputs:

% configuration_configuration

Configuring a project can include configuring another project simply by doing an import.

Imagine that you have a file named Import and this project depends on spring-config-service.xml and utils . So in the configuration of this project, do so:

<import resource="classpath:/spring-config-persistence.xml" />
<import resource="classpath:/spring-config-util.xml" />

Inclusion via annotation in unit tests

Another (even more flexible) alternative is to include the settings in unit tests only.

Example:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath:/spring-config-service.xml",
        "classpath:/spring-config-persistence.xml",
        "classpath:/spring-config-util.xml"})
@Configurable
public class UnitTests { ... }

In the example above, you can see that you can add as many settings as you need to initialize Spring in the current test.

This is great because you could create up specific test settings with mocks and everything else you need, although the same can be achieved using profiles .

Important note

In both cases of import, the project must properly declare its dependencies on Maven and the configuration files must be in their respective persistence directories to be available in the test classpath . >

Final application configuration

Finally, in the case of a web application that will be distributed in a WAR, you can define all the settings that should be loaded with a parameter, like this:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring-config-web.xml
        classpath:/spring-config-service.xml",
        classpath:/spring-config-persistence.xml",
        classpath:/spring-config-util.xml"})
    </param-value>
</context-param>

Note: If the settings are packaged in Jar files, and Spring complains that you do not think so, try adding an asterisk (% with%) after the term resource in the definition, as below.

classpath*:/spring-config-util.xml
    
30.06.2014 / 22:56