An IntelliJ Multi Module Spring Boot MVC Configuration

Version 0.1.2 of my Spring-Data app on GitHub adds a new MVC and Web Module.

Because I wanted to continue having the option of working with the Spring Data JPA processes in a console I separated the MVC and Web pieces into two IntelliJ Modules. This is really no different than Spring Web MVC's configuration of separating the data and services context from the web context, but in this case they are in two different IntelliJ Modules.

The Spring Boot MVC Module Initializer

As you know there are many ways to initialize a Spring Boot Web Application, with one option extending from AbstractAnnotationConfigDispatcherServletInitializer. For the initial MVC release I'm using SpringBootServletInitializer. Here is the complete WebInitializer.class file.

@SpringBootApplication
@Import(WebConfig.class)
public class WebInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WebInitializer.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(WebInitializer.class, args);
    }
}

Rather than create the Web Context in a getServletConfigClasses() override method I'm using the @SpringBootApplication and @Import(WebConfig.class).

The Web Application Context

The key to bringing the JPA data module into the web application is in the Web Context Bean Wiring. Both packages share the same root name (com.nixmash.springdata) which is the basis for our component scan for wiring the beans we need. But notice our @EnableJpaRepositories annotation points us to our JPA module where our data beans are located. This is how we're separating the web application context from the data context, but doing it with two separate modules.

A Testing Note

A note on successfully testing processes in the MVC Web Module, be sure to wire up the beans from both modules by including their Configuration classes in @ContextConfiguration. In the example below, WebConfig.class is from the MVC module, ApplicationConfig.class from the JPA module.

Building With Gradle

Here are the dependencies for the MVC Web Module. You'll notice that our first dependency is the :JPA Module.

All source code discussed in this post is available in the v0.1.2 Branch of my Spring-Data GitHub app.

Posted June 09, 2015 10:31 AM EDT

More Like This Post