SpringBoot多模块项目框架搭建过程解析

这篇文章主要介绍了SpringBoot多模块项目框架搭建过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

创建根项目,New Project 创建一个名为 sms-bomber 的 SpringBoot 新项目,打包为 JAR 的即可,这里只添加 Lombok 与 spring-boot-starter-web 依赖,这两个依赖会传递给所有子模块,删除创建完成的项目中的 .mvn\ src\ mvnw mvnw.cmd

创建启动模块,根目录项目名上右键 New -> Model 选择 Maven 类型,不要选择 create from archetype,Parent 属性选择刚才创建的根项目,先创建一个 ${PREFIXE}-start 的模块,用于启动项目,start 模块 resources 目录下创建

server:

servlet:

# 项目上下文路径

context-path: /sms-bomber

spring:

main:

# 关闭 banner

banner-mode: off

profiles:

# 测试环境

active: dev-mysql

# 生产环境

# active: prod-mysql

以及 logback.xml

<?xml version="1.0" encoding="UTF-8"?>

<configuration scan="true" scanPeriod="30 seconds">

<!-- FIXME 记得更改如下配置 -->

<!-- CONSOLE/FILE -->

<property name="output-ref" value="CONSOLE" />

<property name="logfile-dir" value="D:/Tmp/Log" />

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">

<encoder>

<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>

</encoder>

</appender>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">

<encoder>

<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>

<charset>utf-8</charset>

</encoder>

<file>${logfile-dir}/ticket.log</file>

<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">

<fileNamePattern>${logfile-dir}/ticket.%i.log</fileNamePattern>

<minIndex>1</minIndex>

<maxIndex>5</maxIndex>

</rollingPolicy>

<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">

<maxFileSize>1MB</maxFileSize>

</triggeringPolicy>

</appender>

<root level="INFO">

<appender-ref ref="${output-ref}" />

</root>

<!-- 指定包的日志等级 -->

<!-- 使用 additivity="true" 会同时使用 appender-ref -->

<logger name="com.seliote.smsbomber" level="DEBUG" additivity="false">

<appender-ref ref="${output-ref}" />

</logger>

</configuration>

创建项目启动类

package com.seliote.smsbomber;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

* 项目启动类

*

* @author LiYangDi

* @since 2020/1/26

*/

@SpringBootApplication(scanBasePackages = "com.seliote.smsbomber")

public class Application {

public static void main(String... args) {

SpringApplication.run(Application.class, args);

}

}

创建 Servlet 初始化类

package com.seliote.smsbomber;

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

/**

* 打 WAR 包 Servlet 初始化

*

* @author LiYangDi

* @since 2020/1/26

*/

public class ServletInitializer extends SpringBootServletInitializer {

@Override

protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {

return builder.sources(Application.class);

}

}

修改 IDEA 的启动类,尝试启动项目

修改启动模块打包方式为 war, 修改启动模块的 pom.xml,project 节点下添加 <packaging>war</packaging> 与

<build>

<plugins>

<!-- 打包配置 -->

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

<configuration>

<!-- war 包名 -->

<finalName>sms-bomber</finalName>

</configuration>

</plugin>

</plugins>

</build>

建一个 TestCtr,maven package 一下把 war 扔进 Tomcat 里体验一下

创建模块 persistence, service, web, common 层模块层模块,并配置相关依赖,根项目 pom.xml 里添加依赖 project 节点下添加,再依次

<!-- 用于多模块版本管理 -->

<dependencyManagement>

<dependencies>

<dependency>

<groupId>com.seliote</groupId>

<artifactId>sms-bomber-common</artifactId>

<version>${sms-bomber.version}</version>

</dependency>

<dependency>

<groupId>com.seliote</groupId>

<artifactId>sms-bomber-persistence</artifactId>

<version>${sms-bomber.version}</version>

</dependency>

<dependency>

<groupId>com.seliote</groupId>

<artifactId>sms-bomber-service</artifactId>

<version>${sms-bomber.version}</version>

</dependency>

<dependency>

<groupId>com.seliote</groupId>

<artifactId>sms-bomber-web</artifactId>

<version>${sms-bomber.version}</version>

</dependency>

<dependency>

<groupId>com.seliote</groupId>

<artifactId>sms-bomber-start</artifactId>

<version>${sms-bomber.version}</version>

</dependency>

<dependency>

<groupId>org.mariadb.jdbc</groupId>

<artifactId>mariadb-java-client</artifactId>

<version>${mariadb.version}</version>

</dependency>

</dependencies>

</dependencyManagement>

dependencies 节点下添加用于全局模块使用

<dependency>

<groupId>com.seliote</groupId>

<artifactId>sms-bomber-common</artifactId>

</dependency>

${sms-bomber.version} 等版本定义在 properties 标签下定义

persistence 模块的 pom.xml 下添加依赖

<dependencies>

<!-- MariaDB 驱动 -->

<dependency>

<groupId>org.mariadb.jdbc</groupId>

<artifactId>mariadb-java-client</artifactId>

<scope>runtime</scope>

</dependency>

<!-- 持久层框架 -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

</dependencies>

service 模块 pom.xml 下添加

<dependencies>

<dependency>

<groupId>com.seliote</groupId>

<artifactId>sms-bomber-persistence</artifactId>

</dependency>

</dependencies>

web 模块 pom.xml 下添加

<dependencies>

<dependency>

<groupId>com.seliote</groupId>

<artifactId>sms-bomber-service</artifactId>

</dependency>

</dependencies>

start 模块 pom.xml 下添加

<dependencies>

<dependency>

<groupId>com.seliote</groupId>

<artifactId>sms-bomber-web</artifactId>

</dependency>

</dependencies>

配置 SpringDataJpa

dev yml 下添加

spring:

datasource:

driver-class-name: org.mariadb.jdbc.Driver

url: jdbc:mariadb://127.0.0.1:3306/sms_bomber

username: sms_bomber

password: 123456!@#$%^qp

jpa:

database-platform: org.hibernate.dialect.MariaDB103Dialect

open-in-view: false

show-sql: true

以上是 SpringBoot多模块项目框架搭建过程解析 的全部内容, 来源链接: utcz.com/z/332397.html

回到顶部