談?wù)?SpringBoot 業(yè)務(wù)組件化開發(fā)思路
1、背景
首先,談一談什么是“springBoot業(yè)務(wù)組件化開發(fā)”,最近一直在開發(fā)一直面臨這一個(gè)問題,就是相同的業(yè)務(wù)場(chǎng)景場(chǎng)景在一個(gè)項(xiàng)目中使用了,又需要再另外一個(gè)項(xiàng)目中復(fù)用,一遍又一遍的復(fù)制代碼,然后想將該業(yè)務(wù)的代碼在不同的項(xiàng)目中維護(hù)起來真的很難。
最開始想用微服務(wù)的方式來解決這個(gè)問題,但是覺得一套完整的微服務(wù)太重,而且目前微服務(wù)還處于振蕩期(去年的微服務(wù)解決方案,今年國內(nèi)直接都換成了阿里的技術(shù)解決方案),此外很多時(shí)候我們接私活,就是個(gè)單體的springboot項(xiàng)目,用不上微服務(wù)這種級(jí)別的項(xiàng)目,所以想來想去這條路不是很滿足我的需求;再后來,想到單體的聚合架構(gòu),但是聚合架構(gòu)的項(xiàng)目,個(gè)人覺得有時(shí)候也不是很好,一般的聚合項(xiàng)目就是基于某個(gè)具體實(shí)例架構(gòu)下才能使用,換一個(gè)架構(gòu)自己寫的業(yè)務(wù)model就不能用了(比如你在suoyi框架下開發(fā)的模塊業(yè)務(wù)包,在guns下可能就直接不能使用了)。
最后,想了一下,能不能單獨(dú)開發(fā)一個(gè)項(xiàng)目,這個(gè)項(xiàng)目可以自己獨(dú)立運(yùn)行(微服務(wù)架構(gòu)下用),也可以在單體項(xiàng)目中直接通過pom引入的方式,然后簡單的配置一下,然后直接使用多好;查了一下網(wǎng)上沒有現(xiàn)成的技術(shù)解決方案,問了同事,他說我這種思想屬于SOA的一種實(shí)現(xiàn),同時(shí)有第三包和聚合項(xiàng)目的影子在里面。也許有什么更好的技術(shù)解決方案,也希望各位能夠不吝賜教。
補(bǔ)充一句,之所以說“業(yè)務(wù)組件化”開發(fā),來源于Vue的思想,希望Java后端開發(fā)的業(yè)務(wù)也可像vue的組件一樣去使用,這樣多好
2、demo
2-1項(xiàng)目準(zhǔn)備
①建一個(gè)Java項(xiàng)目項(xiàng)目,結(jié)構(gòu)如下圖:
②pom文件如下:
<?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 http://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.3.1.RELEASE</version>
</parent>
<groupId>top.wp</groupId>
<artifactId>cx-flow</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<mysql-connector-java.version>8.0.17</mysql-connector-java.version>
<druid.version>1.1.21</druid.version>
<mp.version>3.3.2</mp.version>
<fastjson.version>1.2.70</fastjson.version>
<jwt.version>0.9.1</jwt.version>
<hutool.version>5.3.7</hutool.version>
<lombok.versin>1.18.12</lombok.versin>
<swagger.version>2.9.2</swagger.version>
<swagger.bootstrap.ui.version>1.9.6</swagger.bootstrap.ui.version>
<easypoi.version>4.2.0</easypoi.version>
<jodconverter.version>4.2.0</jodconverter.version>
<libreoffice.version>6.4.3</libreoffice.version>
<justauth.version>1.15.6</justauth.version>
<aliyun.oss.version>3.8.0</aliyun.oss.version>
<qcloud.oss.version>5.6.23</qcloud.oss.version>
<aliyun.sms.sdk.version>4.4.6</aliyun.sms.sdk.version>
<aliyun.sms.esc.version>4.17.6</aliyun.sms.esc.version>
<qcloud.sms.sdk.version>3.1.57</qcloud.sms.sdk.version>
</properties>
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mp.version}</version>
</dependency>
<!--數(shù)據(jù)庫驅(qū)動(dòng)-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<!--數(shù)據(jù)庫連接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.versin}</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- <excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
</excludes> -->
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
③配置文件如下:主要是數(shù)據(jù)庫和mybaits-plus的配置(其實(shí)可以不用這個(gè)配置文件,在這只是為了項(xiàng)目能夠獨(dú)立運(yùn)行起來)
#服務(wù)配置
server:
port: 8080
#spring相關(guān)配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/cx-xn?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=true
username: 數(shù)據(jù)庫賬戶
password: 數(shù)據(jù)庫密碼
servlet:
multipart:
max-request-size: 100MB
max-file-size: 100MB
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss.SSS
locale: zh_CN
serialization:
# 格式化輸出
indent_output: false
#mybaits相關(guān)配置
mybatis-plus:
mapper-locations: classpath*:top/wp/cx/**/mapping/*.xml, classpath:/META-INF/modeler-mybatis-mappings/*.xml
configuration:
map-underscore-to-camel-case: true
cache-enabled: true
lazy-loading-enabled: true
multiple-result-sets-enabled: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
banner: false
db-config:
id-type: assign_id
table-underline: true
enable-sql-runner: true
configuration-properties:
prefix:
blobType: BLOB
boolValue: TRUE
④啟動(dòng)入口(可以不用寫,啟動(dòng)入口存在目的是讓項(xiàng)目可以自己跑起來)
package top.wp.cx;
import cn.hutool.log.StaticLog;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CXApplication {
public static void main(String[] args) {
SpringApplication.run(CXApplication.class, args);
StaticLog.info(">>> " + CXApplication.class.getSimpleName() + " 啟動(dòng)成功!");
}
}
⑤測(cè)試:entity、result
package top.wp.cx.modular.test.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("test")
public class Test {
/**
* 主鍵
*/
@TableId(type = IdType.ASSIGN_ID)
private Integer id;
/**
* 賬號(hào)
*/
private String name;
}
package top.wp.cx.modular.test.result;
import lombok.Data;
@Data
public class TestResult {
private Integer id;
private String name;
}
⑥測(cè)試mapper、xml、service和controller
package top.wp.cx.modular.test.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import top.wp.cx.modular.test.entity.Test;
/**
* 系統(tǒng)用戶數(shù)據(jù)范圍mapper接口
*
* @author xuyuxiang
* @date 2020/3/13 15:46
*/
//@Mapper
public interface TestMapper extends BaseMapper<Test> {
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.wp.cx.modular.test.mapper.TestMapper">
</mapper>
package top.wp.cx.modular.test.service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.wp.cx.modular.test.entity.Test;
import top.wp.cx.modular.test.mapper.TestMapper;
/**
* 一個(gè)service實(shí)現(xiàn)
*
* @author yubaoshan
* @date 2020/4/9 18:11
*/
@Service
public class TestService extends ServiceImpl<TestMapper, Test> {
}
package top.wp.cx.modular.test.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.wp.cx.modular.test.entity.Test;
import top.wp.cx.modular.test.service.TestService;
import javax.annotation.Resource;
import java.util.List;
/**
* 一個(gè)示例接口
*
* @author yubaoshan
* @date 2020/4/9 18:09
*/
@RestController
@RequestMapping("/test")
public class TestController {
@Resource
private TestService testService;
@GetMapping("")
public List<Test> testResult(){
return testService.list();
}
@GetMapping("/2")
public String testResult2(){
return "22";
}
}
至此項(xiàng)目準(zhǔn)備完成,其實(shí)就是簡單見了一個(gè)測(cè)試項(xiàng)目,此時(shí)如果你按照上面的步驟,寫了啟動(dòng)類和配置項(xiàng)信息,項(xiàng)目是可以獨(dú)立運(yùn)行的。
2-2項(xiàng)目打包、引入、運(yùn)行
①將2-1中的測(cè)試項(xiàng)目進(jìn)行打包:install右鍵第一個(gè)選項(xiàng)
②此時(shí)你的本地maven倉庫會(huì)出現(xiàn)剛才的項(xiàng)目(當(dāng)然前提是你的idea配置過本地的maven)
③新建另外一個(gè)項(xiàng)目cx-main
④pom文件如下:注意將你剛才的準(zhǔn)備測(cè)試的項(xiàng)目引入進(jìn)來
<?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 http://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.3.1.RELEASE</version>
</parent>
<groupId>top.wp.cx</groupId>
<artifactId>cx-main</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<mysql-connector-java.version>8.0.17</mysql-connector-java.version>
<druid.version>1.1.21</druid.version>
<mp.version>3.3.2</mp.version>
<fastjson.version>1.2.70</fastjson.version>
<jwt.version>0.9.1</jwt.version>
<hutool.version>5.3.7</hutool.version>
<lombok.versin>1.18.12</lombok.versin>
<swagger.version>2.9.2</swagger.version>
<swagger.bootstrap.ui.version>1.9.6</swagger.bootstrap.ui.version>
<easypoi.version>4.2.0</easypoi.version>
<jodconverter.version>4.2.0</jodconverter.version>
<libreoffice.version>6.4.3</libreoffice.version>
<justauth.version>1.15.6</justauth.version>
<aliyun.oss.version>3.8.0</aliyun.oss.version>
<qcloud.oss.version>5.6.23</qcloud.oss.version>
<aliyun.sms.sdk.version>4.4.6</aliyun.sms.sdk.version>
<aliyun.sms.esc.version>4.17.6</aliyun.sms.esc.version>
<qcloud.sms.sdk.version>3.1.57</qcloud.sms.sdk.version>
</properties>
<dependencies>
<dependency>
<groupId>top.wp</groupId>
<artifactId>cx-flow</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mp.version}</version>
</dependency>
<!--數(shù)據(jù)庫驅(qū)動(dòng)-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<!--數(shù)據(jù)庫連接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.versin}</version>
</dependency>
</dependencies>
<!--xml打包排除-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- <excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
</excludes> -->
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
⑤application.yml配置文件 注意xml的掃描
#服務(wù)配置
server:
port: 8081
#spring相關(guān)配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/cx-xn?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=true
username: root
password: root
servlet:
multipart:
max-request-size: 100MB
max-file-size: 100MB
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss.SSS
locale: zh_CN
serialization:
# 格式化輸出
indent_output: false
#mybaits相關(guān)配置
mybatis-plus:
#xml文件掃描
mapper-locations: classpath*:top/wp/cx/**/mapping/*.xml, classpath:/META-INF/modeler-mybatis-mappings/*.xml
configuration:
map-underscore-to-camel-case: true
cache-enabled: true
lazy-loading-enabled: true
multiple-result-sets-enabled: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
banner: false
db-config:
id-type: assign_id
table-underline: true
enable-sql-runner: true
configuration-properties:
prefix:
blobType: BLOB
boolValue: TRUE
⑥啟動(dòng)入口,注意spring和mapper掃描
package top.wp.cx.main;
import cn.hutool.log.StaticLog;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"top.wp.cx.modular.test"}) // spring掃描
@MapperScan(basePackages = {"top.wp.cx.modular.test.**.mapper"}) // mybatis掃描mapper
public class CXApplication {
public static void main(String[] args) {
SpringApplication.run(CXApplication.class, args);
StaticLog.info(">>> " + CXApplication.class.getSimpleName() + " 啟動(dòng)成功!");
}
}
⑦此時(shí)啟動(dòng)cx-main的項(xiàng)目,訪問2-1的測(cè)試controller能訪問成功證明配置正確
來源:blog.csdn.net/weixin_44213308/
article/details/111151350
程序汪資料鏈接
程序汪接的7個(gè)私活都在這里,經(jīng)驗(yàn)整理
Java項(xiàng)目分享 最新整理全集,找項(xiàng)目不累啦 07版
堪稱神級(jí)的Spring Boot手冊(cè),從基礎(chǔ)入門到實(shí)戰(zhàn)進(jìn)階
臥槽!字節(jié)跳動(dòng)《算法中文手冊(cè)》火了,完整版 PDF 開放下載!
臥槽!阿里大佬總結(jié)的《圖解Java》火了,完整版PDF開放下載!
字節(jié)跳動(dòng)總結(jié)的設(shè)計(jì)模式 PDF 火了,完整版開放下載!
歡迎添加程序汪個(gè)人微信 itwang007 進(jìn)粉絲群或圍觀朋友圈
