码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot 多模块项目框架搭建

时间:2020-01-26 22:09:07      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:size   mon   fixed   jar   art   char   ajp   rop   rgs   

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

  2. 创建启动模块,根目录项目名上右键 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 的启动类,尝试启动项目

  1. 修改启动模块打包方式为 war, 修改启动模块的 pom.xmlproject 节点下添加 <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 里体验一下

  1. 创建模块 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>
  1. 配置 SpringDataJpa

SpringBoot 多模块项目框架搭建

标签:size   mon   fixed   jar   art   char   ajp   rop   rgs   

原文地址:https://www.cnblogs.com/seliote/p/12234903.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!