Springboot Episode 4: springboot modular development and project construction process

Spring Boot is a rapid development framework based on the Spring framework that can be used to build stand-alone, production-level applications. When doing modular development, the application is split into multiple small, reusable modules, each responsible for its own functionality. The following is the process of Spring Boot modular development and project construction:

  1. Determine the functional requirements of the application: first of all, it is necessary to clarify which functions the application needs to achieve, and determine the responsibilities of each module.

  2. Create a Maven project: Use the Maven build tool to create an empty project and add Spring Boot dependencies to the pom.xml file.

  3. Create a parent-child module structure: According to the functional requirements of the application, create multiple sub-modules and use them as sub-modules of the parent module.

  4. Configure the parent module: In the pom.xml file of the parent module, add the dependencies of the sub-module and unified configuration information, such as logs and databases.

  5. Configure submodules: In the pom.xml file of the submodule, add the dependencies and configuration information of the current module, such as controllers, services, repositories, tool classes, exception handling, etc.

  6. Write code: Write corresponding code in each module to realize corresponding business logic.

  7. Test modules: unit test each module to make sure it is functional and bug-free.

  8. Package deployment: Use Maven to package and deploy to the server, start the application.

  9. Configuration file: Add application.yml or application.properties files in each submodule to configure the property information required by the current module, such as data source, port number, log level, etc.

  10. Common module: For tool classes, configuration information, etc. that need to be used by multiple sub-modules, it can be abstracted into a common module, so as to avoid duplication of code and improve the code reuse rate.

  11. Interceptor: Add interceptors in submodules to extend the functions of the application, such as recording request logs, permission verification, exception handling, etc.

  12. Web layer: Add controllers in submodules to implement HTTP requests and responses, and handle business logic related to the Web layer.

  13. Service layer: Add service classes in submodules, encapsulate specific business logic, and be called by the Web layer.

  14. Repository layer: Add repository interfaces in submodules for accessing databases or other data storage methods, such as Redis, Elasticsearch, etc.

  15. Exception handling: Add exception handling classes in submodules to handle exceptions that occur in the application, such as parameter verification failures, database access exceptions, etc.

Modular development with Spring Boot:

  1. Create a Maven project and add Spring Boot dependencies to the pom.xml file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>${spring.boot.version}</version>
</dependency>
  1. Create parent and child modules in the project root directory:

$ mkdir parent-module
$ cd parent-module
$ mvn archetype:generate -DgroupId=com.example -DartifactId=parent-module -DinteractiveMode=false

$ mkdir child-module-1
$ cd child-module-1
$ mvn archetype:generate -DgroupId=com.example -DartifactId=child-module-1 -DinteractiveMode=false

$ mkdir child-module-2
$ cd child-module-2
$ mvn archetype:generate -DgroupId=com.example -DartifactId=child-module-2 -DinteractiveMode=false
  1. Configure the pom.xml file of the parent module:

<modules>
    <module>child-module-1</module>
    <module>child-module-2</module>
</modules>
  1. Configure the pom.xml file of the submodule:

Submodule 1:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring.boot.version}</version>
    </dependency>
</dependencies>

Submodule 2:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>${spring.boot.version}</version>
    </dependency>
</dependencies>
  1. Write code:

Submodule 1: Add the HelloWorldController.java file under the src/main/java/com/example/child_module_1 directory to implement HTTP requests and responses:

@RestController
public class HelloWorldController {

    @GetMapping("/hello")
    public String helloWorld() {
        return "Hello, World!";
    }

}

Submodule 2: Add the UserRepository.java file in the src/main/java/com/example/child_module_2 directory to access and operate the user table:

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

    List<User> findByAge(int age);

}
  1. Test module: Write corresponding test cases in each submodule to ensure its normal function and no bugs.

  2. Package deployment: Use Maven to package and deploy to the server, start the application.

Spring Boot Starter Data JPA is a Spring Boot starter that facilitates developers using the Java Persistence API (JPA) and Hibernate ORM framework. JPA is a standardized ORM framework that provides a way to map Java objects into relational databases, allowing developers to manipulate data in an object-oriented manner.

Spring Boot Starter Data JPA provides automatic configuration, including:

  1. Data source configuration

  2. Configuration of the entity manager

  3. Configuration of transaction management

By simply adding the Spring Boot Starter Data JPA dependency, you can quickly enable JPA functionality in your Spring Boot application and start using the Hibernate ORM framework.

Additionally, Spring Boot Starter Data JPA supports other features such as:

  1. Spring Data JPA: Provides a set of easy-to-use repository interfaces for accessing databases and provides features such as paging, sorting, query building, and more.

  2. Spring Data REST: Provides a RESTful API for Spring Data repositories.

  3. Spring Batch: Provides support for batch job processing.

  4. Flyway and Liquibase: Provides support for database migration, making it easy to manage database schema changes.

<repositories>
    <repository>
        <id>public</id>
        <name>aliyun nexus</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
    </repository>
</repositories>

This is a Maven configuration file that defines the location and related information of a Maven repository.

Specifically, this configuration file contains an element that defines a warehouse. Below this element, there is another element that defines the details of a warehouse.

The id of this warehouse is “public”, the name is “aliyun nexus”, and the URL is http://maven.aliyun.com/nexus/content/groups/public/, indicating that this warehouse is stored in Nexus of Alibaba Cloud server and can be accessed through this URL address.

Below the element, the element is set to true, indicating that this warehouse allows release version components. That is to say, when a Maven project needs to use a component in this warehouse, it will first check whether the component exists in the warehouse, and if it exists, it will download and use it; if it does not exist, it will search in other configured warehouses .

<pluginRepositories>
    <pluginRepository>
        <id>public</id>
        <name>aliyun nexus</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

This is a Maven configuration file, which is used to configure the repository information of the Maven plugin.

Specifically, this configuration file contains a element, indicating that a plugin repository is defined. Below this element, there is another element, which defines the details of a plugin repository.

The id of this plugin repository is “public”, the name is “aliyun nexus”, and the URL is http://maven.aliyun.com/nexus/content/groups/public/, indicating that this plugin repository is stored in Alibaba Cloud Nexus server and can be accessed through this URL address.

Under the element, the element is set to true, indicating that this plugin repository allows the release of the release version of the plugin; in element, the element is set to false, which means that the plugin repository does not allow publishing snapshot version plugins.

Maven plugin repositories are similar to component repositories, except that they store different types of files. The plug-in warehouse is mainly used to store relevant information and codes of Maven plug-ins, while the component warehouse is mainly used to store the compilation results of the project (such as jar, war and other files).

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-text</artifactId>
    <version>1.10.0</version>
</dependency>

This is a configuration used to declare dependencies in a Maven project, indicating that the project depends on the commons-text module under the org.apache.commons organization, and the version number is 1.10.0.

Specifically, the organization name and module name correspond to the directory structure in the Maven repository, so Maven can use this information to find and download the required dependency packages. In this case, the commons-text module provides an implementation of the Apache Commons Text library that can be used to handle text-related operations such as string processing, formatting, and more.

In Maven, the format of the dependency declaration is usually ::, indicating the dependent organization, module and its version information. Maven will automatically download the required dependency packages according to the dependency information, and add them to the classpath of the project, so that the corresponding classes and methods can be called in the code.

<!--mapStruct dependency-->
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>${mapstruct.version}</version>
</dependency>
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>${mapstruct.version}</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>

Here are some dependency configurations in the pom.xml file of a Maven project. Maven is a Java build tool that automates project building and dependency management.

There are three dependencies listed here:

  1. org.mapstruct:mapstruct is a dependency of the MapStruct library, a Java Bean mapping library that simplifies the mapping between two objects. ${mapstruct.version} is a variable whose value will be set in the Maven configuration file.

  2. org.mapstruct:mapstruct-processor is also a dependency of the MapStruct library. This library provides compile-time code generation for better type checking and correctness guarantees. The element is set to “provided”, indicating that this dependency is provided by the compiler or deployment environment, not by Maven.

  3. javax.inject:javax.inject is the implementation of JSR-330 (Dependency Injection Specification), which includes annotations and interfaces, which can easily implement dependency injection mode.

Detailed code explanation of the POM file:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Define the version of the Maven POM model -->
<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
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <!-- Define the model version number of the POM -->
    <modelVersion>4.0.0</modelVersion>

    <!-- Define project coordinates, namely organization name, project name and version number -->
    <groupId></groupId>
    <artifactId></artifactId>
    <version></version>
    
    <!-- Define the project packaging method as pom, which means that the project does not generate jar or war packages -->
    <packaging>pom</packaging>

    <!-- Define the module name in a maven multi-module project, which can be multiple submodules -->
    <modules>
        <module>common</module>
        <module>logging</module>
    </modules>

    <!-- Define the project's name and URL -->
    <name></name>
    <url></url>

    <!-- Define the parent POM of this POM, define some public configuration -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version>
    </parent>

    <!-- Define some attribute values for later reference -->
    <properties>
        <log4j2.version>2.17.0</log4j2.version>
        <logback.version>1.2.9</logback.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <log4jdbc.version>1.16</log4jdbc.version>
        <swagger.version>2.9.2</swagger.version>
        <fastjson.version>1.2.83</fastjson.version>
        <druid.version>1.2.8</druid.version>
        <commons-pool2.version>2.11.1</commons-pool2.version>
        <mapstruct.version>1.4.2.Final</mapstruct.version>
    </properties>

    <!-- Dependent libraries required by the project -->
    <dependencies>
        <!--Spring boot core-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!--Spring boot Web container-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--Spring boot test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--Spring boot security framework-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <!-- spring boot cache -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

        <!--Spring boot Redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <!--spring boot integrated redis required common-pool2-->
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>${commons-pool2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
                        <artifactId>commons-lang3</artifactId>
            <version>3.13.0</version>
        </dependency>

        <!--hibernate-validator-->
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.2.0.Final</version>
        </dependency>

        <!--mysql database driver-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.27</version>
        </dependency>

        <!--Druid connection pool-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>

        <!--MyBatis Plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3</version>
        </dependency>


        <!--FastJson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>

        <!--Swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>

        <!--MapStruct-->
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>

        <!--Lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!--Log4j2-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>${log4j2.version}</version>
        </dependency>

        <!--Logback-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
            <version>6.6</version>
        </dependency>

    </dependencies>

    <!-- Define build-related configuration information -->
    <build>
        <!-- Skip the test phase when specifying packaging -->
        <skipTests>true</skipTests>
    </build>

    <!-- Define Maven repository -->
    <repositories>
        <repository>
            <id>aliyun</id>
            <url>https://maven.aliyun.com/repository/public</url>
        </repository>
    </repositories>

    <!-- Define Maven plugin repository -->
    <pluginRepositories>
        <pluginRepository>
            <id>aliyun-plug</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </pluginRepository>
    </pluginRepositories>

</project>

what is archetype

In Spring Boot, an Archetype is a project template that defines the project’s structure, dependencies, and other configurations. When you create a new Spring Boot project, you can use Archetype to quickly set up the basic structure and dependencies of the project.

Choosing the right Archetype depends on your needs and project type. For example, if you are developing a web application, you can choose Spring Boot Web Archetype; if you are developing a RESTful API, you can choose Spring Boot RESTful Service Archetype. In addition, there are some Archetypes for testing, security, etc.

You can find Spring Boot Archetype in Maven repository. To use them, generate a new project from Archetype using the Maven command line tool or the functionality available in your integrated development environment (IDE).

Summarize:

Spring Boot is a development framework based on the Spring framework, which can quickly build applications and support modular development. The following is the process of Spring Boot modular development and project construction:

  1. Determine project requirements: Determine the needs and functions of the project, including database design, user interface design, etc.

  2. Create a Maven project: Use Maven to create a Spring Boot project and add the required dependencies. Specify the version of Spring Boot along with other dependencies in the pom.xml file.

  3. Configure the database: configure the connection between Spring Boot and the database, and use Spring Data JPA for data access operations.

  4. Add modules: Add required modules according to requirements, such as Web modules, Security modules, etc. Each module needs to declare the required dependencies in the pom.xml file.

  5. Design the API interface: design the RESTful API interface according to the requirements, and write the corresponding Controller layer code.

  6. Implement business logic: implement business logic in the Service layer and inject it into the Controller.

  7. Write test code: write unit test and integration test code to ensure the correctness and security of the project.

  8. Package deployment: Use Maven to package the project and deploy it to the server.

The above is the general process of Spring Boot modular development and project construction. The specific implementation process will vary according to project requirements and scenarios.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Java skill treeHomepageOverview 108539 people are studying systematically