ReconditusNeumen
ReconditusNeumen

Reputation: 121

Spring Maven project structure pom.xml

I'd like to get help in setting up a multi-module Maven project using Spring Boot.

Correct me if I'm wrong but I've read that Spring-Boot reads the start main Application (Annotated with @SpringBootApplication and ran with SpringApplication.run) and finds the necessary classes through reflection. Which means that it first accesses the start class and then proceeds to find the controllers, models, repositories. If so, how do I set up the dependency in the pom.xml of each module if I had a project structure like this:

app
--src
--pom.xml

core
--pom.xml

--models
----/pom.xml

--controllers
----/pom.xml

--repositories
----/pom.xml

pom.xml

Upvotes: 4

Views: 1249

Answers (3)

CRISTIAN ROMERO MATESANZ
CRISTIAN ROMERO MATESANZ

Reputation: 1818

I have a GitHub project where I configured a multimodule maven project:

https://github.com/cristianprofile/spring-boot-mvc-complete-example

This is Example project maven module structure:

enter image description here

 Spring mvc rest maven module ---> service maven module  ---> repository maven module

The main module should be configured like this (Spring mvc rest layer):

@SpringBootConfiguration
@EnableAutoConfiguration
//spring mvc module auto scan only its package
@ComponentScan(basePackageClasses = HelloWorldController.class)
//It needs Service bean so it will import ConfigurationService.class from
// Service maven module
@Import({ConfigurationService.class})

Complete class:

https://github.com/cristianprofile/spring-boot-mvc-complete-example/blob/develop/spring-boot-mvc-rest/src/main/java/com/mylab/cromero/controller/Application.java

It will only scan its package :

HelloWorldController.class  -->  com.mylab.cromero.controller;

This Rest layer use a service maven module so it is necessary to add dependency:

    <dependency>
        <groupId>com.mylab.cromero.core</groupId>
        <artifactId>mylab-core-service-impl</artifactId>
        <version>0.0.2-SNAPSHOT</version>
    </dependency>

Complete pom file:

https://github.com/cristianprofile/spring-boot-mvc-complete-example/blob/develop/spring-boot-mvc-rest/pom.xml#L16

ConfigurationService.class from service maven module autoscan its packages and it will import ConfigurationRepository.class (Repository maven module)

@Configuration
//It needs repository's bean so it will import ConfigurationRepository.class from
// Repository maven module
@Import(ConfigurationRepository.class)
//service layer module auto scan only its package
@ComponentScan(basePackageClasses = ConfigurationService.class)
public class ConfigurationService {

}

Complete Service maven module code:

https://github.com/cristianprofile/spring-boot-mvc-complete-example/blob/develop/mylab-core/mylab-core-service-impl/src/main/java/com/mylab/cromero/service/ConfigurationService.java#L12

Service maven module layer has a dependency with maven repository module:

https://github.com/cristianprofile/spring-boot-mvc-complete-example/blob/develop/mylab-core/mylab-core-service-impl/pom.xml#L38

Repository module will auto configure jpa and domain classed:

@Configuration
@EnableJpaRepositories(basePackages = "com.mylab.cromero.repository")
@EntityScan(basePackageClasses=Base.class)
@ComponentScan(basePackageClasses = BaseRepository.class)
public class ConfigurationRepository {

}

Upvotes: 1

kj007
kj007

Reputation: 6254

Please have a look complete guide how to create multi module project in spring boot.

https://spring.io/guides/gs/multi-module/

Upvotes: 3

John Humphreys
John Humphreys

Reputation: 39254

Spring boot will component scan from the package of the class annotated with @SpringBootApplication. Component scannign means that it is looking through the classes under that package recursively, analyzing annotations, and wiring up anything it recognizes. This can include controllers, simple variables with @Value annotations, members with @Autowired, and a host of other things.

You can actually jump into the source for the @SpringBootApplication annotation and see that it expands to numerous other annotations, @ComponentScan being one of them.

If all of your modules are in a sub-hierarchy package wise from there, then they will be scanned properly anyway. Often though, sub-modules will be in a slightly different package hierarchy. In this case, you can explicitly specify @ComponentScan() in your code and inside the () you can list the base packages to component scan from.

Whether or not its a sub-module doesn't matter much at this point; its just like scanning classes in any other library you're including.

General Advice

Also, just FYI - Multi module projects can get a little hard to manage (speaking from numerous separate experiences). They can be very good if used properly though. If you're a beginner to Maven though, it may be wiser to keep separte, well-defined projects with a proper release cycle and just import them as normal dependencies.

So, I'm not for or against them, but just make sure you understand them well going in :).

Upvotes: 1

Related Questions