SpringBoot 多模块项目框架搭建
Posted seliote
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 目录下创建application.yml
application-${PROFILES}.yml
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
以上是关于SpringBoot 多模块项目框架搭建的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot + React 前后端分离多模块项目框架搭建流程
SpringBoot+Mybatis多模块(module)项目搭建教程
2019最新 Java商城秒杀系统的设计与实战视频教程(SpringBoot版)_2-1微服务项目的搭建-SpringBoot搭建多模块项目一