概述
几乎完全参考自SpringBoot+Maven多模块项目(创建、依赖、打包可执行jar包部署测试)完整流程
目前由下到上划分了几个模块:
common : 常用比如工具类等
core : 打算用来放一些系统级别相关的类,配置等
dao : 数据层
service : 服务层
web : 前后端分离的话主要就剩下Controller了
下面是项目和每个模块的pom
项目的pom
需要注意的是packaging为pom。
声明每个子模块。
打包使用的插件,暂时还没深入了解,理解应该有错误。
目前我知道的是要打成war包的话,需要SpringBoot帮忙生成WEB-INF, DispatcherServlet等,所以得用spring-boot-maven-plugin。
网上还看到说打包成war还需要排除springboot的内置tomcat,我测试了下没排除的话,没发现什么影响。
<?xml version="1.0" encoding="UTF-8"?>
<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>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.funwe</groupId>
<artifactId>spfun</artifactId>
<version>0.0.1</version>
<name>spfun</name>
<description>spfun</description>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<!-- 子模块 -->
<modules>
<module>common</module>
<module>core</module>
<module>dao</module>
<module>service</module>
<module>web</module>
</modules>
<!-- 依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Web打成JAR包运行时使用 -->
<!--
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
-->
<!-- Web打成WAR包运行时使用 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
common
模块打包为jar。
声明上级parent。
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.funwe</groupId>
<artifactId>common</artifactId>
<version>0.0.1</version>
<name>common</name>
<description>common</description>
<packaging>jar</packaging>
<parent>
<groupId>com.funwe</groupId>
<artifactId>spfun</artifactId>
<version>0.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<commons-codec.version>1.8</commons-codec.version>
<commons-lang.version>3.3.2</commons-lang.version>
<jwt.version>3.5.0</jwt.version>
<fastjson.version>1.2.15</fastjson.version>
</properties>
<dependencies>
<!--Apache codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<!--Apache Commons Lang -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<!--JWT -->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>${jwt.version}</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
</dependencies>
</project>
core
模块打包为jar。
声明上级parent。
依赖common模块。
因为动态数据源实现被我放在这个模块,所以先依赖jpa。
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.funwe</groupId>
<artifactId>core</artifactId>
<version>0.0.1</version>
<name>core</name>
<description>core</description>
<packaging>jar</packaging>
<parent>
<groupId>com.funwe</groupId>
<artifactId>spfun</artifactId>
<version>0.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.funwe</groupId>
<artifactId>common</artifactId>
<version>0.0.1</version>
</dependency>
<!-- jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
</project>
dao
模块打包为jar。
声明上级parent。
目前没有依赖项目模块,以后或许依赖common和core模块。
数据层面的大多操作被放在这个模块,依赖了mysql, redis ; jpa, druid。
国内更流行mybatis,自己感受了一下觉得还是spring jpa对开发者更友好和强大,执行效率上应该不如mybatis,但是开发效率上应该远胜。复杂的报表Sql打算借助DbUtils。
可以参考Spring Data Jpa的使用
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.funwe</groupId>
<artifactId>dao</artifactId>
<version>0.0.1</version>
<name>dao</name>
<description>dao</description>
<packaging>jar</packaging>
<parent>
<groupId>com.funwe</groupId>
<artifactId>spfun</artifactId>
<version>0.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<druid.version>1.1.10</druid.version>
</properties>
<dependencies>
<!-- JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
</dependencies>
</project>
service
模块打包为jar。
声明上级parent。
依赖common和dao模块。
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.funwe</groupId>
<artifactId>service</artifactId>
<version>0.0.1</version>
<name>service</name>
<description>service</description>
<packaging>jar</packaging>
<parent>
<groupId>com.funwe</groupId>
<artifactId>spfun</artifactId>
<version>0.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 系统模块 -->
<dependency>
<groupId>com.funwe</groupId>
<artifactId>common</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.funwe</groupId>
<artifactId>dao</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
</project>
web
模块打包为jar或war,目前打包成war,直接打成jar的话项目pom的构建插件应该要调整下(项目pom被注释掉的那段),打包成jar的话可以直接运行,无需部署到容器中, 因为内置了Tomcat据说,没去深究。
声明上级parent。
依赖于其他模块。
注意下Build指定了mainClass, 把依赖的包都打包到生成的Jar包中,测试了下,其他模块都会打包jar包在WEB-INF/lib里头。
这里依赖了actuator,还没配置,可以简单的监控服务运行时的环境,比如框架管理的bean,生效的配置等,多用来配合Spring Cloud微服务架构时使用。
可参考Spring Boot Actuator:健康检查、审计、统计和监控
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.funwe</groupId>
<artifactId>web</artifactId>
<version>0.0.1</version>
<name>web</name>
<description>web</description>
<packaging>war</packaging>
<parent>
<groupId>com.funwe</groupId>
<artifactId>spfun</artifactId>
<version>0.0.1</version>
<relativePath>../pom.xml</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 系统模块 -->
<dependency>
<groupId>com.funwe</groupId>
<artifactId>core</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.funwe</groupId>
<artifactId>common</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.funwe</groupId>
<artifactId>dao</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.funwe</groupId>
<artifactId>service</artifactId>
<version>0.0.1</version>
</dependency>
<!-- Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.funwe.web.WebApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
一个简单的壳就出来了,依赖等没有解耦得很好,影响不大,有功夫了再慢慢调。
最后
以上就是花痴小甜瓜为你收集整理的单体应用-SpringBoot-(1)划分模块的全部内容,希望文章能够帮你解决单体应用-SpringBoot-(1)划分模块所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复