Reputation: 121
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
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:
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:
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:
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:
Service maven module layer has a dependency with maven repository module:
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
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
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