[Most detailed] SpringBoot creates maven-based multi-module projects
background
Why do projects need multiple modules? Isn’t SpringMVC enough for everyday use?
- Design pattern mantra: “High cohesion, low coupling”, SpringMVC projects typically break the project into multiple packages: Controller, Service, DAO, util, etc. However, with the increasing complexity of the project, if you want to reuse another module, because it is in the form of package, it will be difficult to peel it out and the coupling is a little strong. The commonly used method is to copy the code modification, but this will do a lot of useless work and increase the probability of error.
- In simple terms, springboot multi-module mode is to divide modules by package, with the help of Maven to upgrade jar mode, abstraction is strengthened, if jar is upgraded to WAR or multiple collection JAR, it becomes a micro service, in multi-module JAR mode, a JAR can be taken out for external sharing. Can greatly improve the code reuse rate and development efficiency.
Don’t talk too much
Springboot multi-module creation
Parent module creation
- Open IDEA: Select Create New Project
Or go to the official website to create (the same as the IDEA ship project, I use IDEA here)
- Then select Spring Initializr
- After clicking Next – > Basic Settings
- Click Next -> Add dependencies
- Click next -> select the project address
- After I click Finish
Submodule creation
- Parent project name -> right-click -> New ->moudle
- Click Spring Initializr(select the appropriate JDK version)-> Next
- Click Next -> Set up Maven Project instead of the same Maven Pom as the parent Project
- Click Next -> Add dependencies
- Click Next -> select Confirm project address
- Click Finish
And then I’m going to create a sub-module multi-core and the process is the same as multi-controller, so I’m going to skip it here. Let’s create two submodules for now.
- The overall structure chart of the project was created and completed
Note: due to program the main entrance is multi – controller all multi – core inside the application. The properties MultiCoreApplication. Java files are deleted
Example Modify poM files
1. Modify the parent project POM – modify as follows (with notes)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>Against 2.4.1</version>
<relativePath/> <! -- lookup parent from repository -->
</parent>
<groupId>com.tinygray</groupId>
<artifactId>multi-parent</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
<name>multi-parent</name>
<description>Demo project for Spring Boot</description>
<! Alter parent project package to POM -->
<packaging>pom</packaging>
<! Add submodule to parent project -->
<modules>
<module>multi-controller</module>
<module>multi-core</module>
</modules>
<! -- Dependencies on version management -->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<! Parent project dependencies can be used in the child project generic -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<! 1.4.2.RELEASE can be used -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.0.1. RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Copy the code
2. Modify the POM
- multi-controller
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>multi-controller</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
<name>multi-controller</name>
<description>Demo project for Spring Boot</description>
<! Replace springboot parent (spring-boot-starter-parent) -->
<parent>
<artifactId>multi-parent</artifactId>
<groupId>com.tinygray</groupId>
<version>0.0.1 - the SNAPSHOT</version>
</parent>
<! -- <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> The < version > against 2.4.1 < / version > < relativePath / > & lt; ! – lookup parent from repository – > </parent>-->
<dependencies>
<! -- Introducing multi-core module -->
<dependency>
<groupId>com.tinygray</groupId>
<artifactId>multi-core</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.tinygray.multicontroller.MultiControllerApplication</mainClass>
<layout>JAR</layout>
</configuration>
</plugin>
</plugins>
</build>
</project>
Copy the code
- multi-core
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>multi-core</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
<name>multi-core</name>
<description>Demo project for Spring Boot</description>
<! Replace springboot parent (spring-boot-starter-parent) -->
<parent>
<artifactId>multi-parent</artifactId>
<groupId>com.tinygray</groupId>
<version>0.0.1 - the SNAPSHOT</version>
</parent>
<! -- <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> The < version > against 2.4.1 < / version > < relativePath / > & lt; ! – lookup parent from repository – > </parent>-->
<dependencies>
<! -- Import the dependencies required by this module -->
</dependencies>
</project>
Copy the code
Springboot is verified to start after multiple modules are created
validation
How do you know when your multi-module project is built and launched successfully?
See below:
The result is that your multi-module project is complete and ready to start
Start the
- Find the startup class
- Execution startup class
- Execute successfully
- Browser open
http://localhost:8080
- The above results indicate that the multi-module project has been constructed
Write a test interface access
- Create two Java files (one entity class User and one UserController)
- User. Java, userController.java file contents – very simple a test interface
- The browser enter http://localhost:8080/user/getUserInfo
- These results indicate that the test was successful.