当前位置:   article > 正文

谈谈SpringBoot业务组件化开发思路

springboot组件开发

1. 背景

首先,谈一谈什么是“springBoot业务组件化开发”,最近一直在开发一直面临这一个问题,就是相同的业务场景场景在一个项目中使用了,又需要再另外一个项目中复用,一遍又一遍的复制代码,然后想将该业务的代码在不同的项目中维护起来真的很难。

最开始想用微服务的方式来解决这个问题,但是觉得一套完整的微服务太重,而且目前微服务还处于振荡期(去年的微服务解决方案,今年国内直接都换成了阿里的技术解决方案),此外很多时候我们接私活,就是个单体的springboot项目,用不上微服务这种级别的项目,所以想来想去这条路不是很满足我的需求;再后来,想到单体的聚合架构,但是聚合架构的项目,个人觉得有时候也不是很好,一般的聚合项目就是基于某个具体实例架构下才能使用,换一个架构自己写的业务model就不能用了(比如你在suoyi框架下开发的模块业务包,在guns下可能就直接不能使用了)。

最后,想了一下,能不能单独开发一个项目,这个项目可以自己独立运行(微服务架构下用),也可以在单体项目中直接通过pom引入的方式,然后简单的配置一下,然后直接使用多好;查了一下网上没有现成的技术解决方案,问了同事,他说我这种思想属于SOA的一种实现,同时有第三包和聚合项目的影子在里面。也许有什么更好的技术解决方案,也希望各位能够不吝赐教。

补充一句,之所以说“业务组件化”开发,来源于Vue的思想,希望Java后端开发的业务也可像vue的组件一样去使用,这样多好

2. demo

2-1项目准备

①建一个Java项目项目,结构如下图:

2497876d44a384d602f1756b74a1d3a0.png

②pom文件如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>
  6.  
  7.     <parent>
  8.         <groupId>org.springframework.boot</groupId>
  9.         <artifactId>spring-boot-starter-parent</artifactId>
  10.         <version>2.3.1.RELEASE</version>
  11.     </parent>
  12.  
  13.     <groupId>top.wp</groupId>
  14.     <artifactId>cx-flow</artifactId>
  15.     <version>1.0-SNAPSHOT</version>
  16.  
  17.  
  18.     <properties>
  19.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  20.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  21.         <mysql-connector-java.version>8.0.17</mysql-connector-java.version>
  22.         <druid.version>1.1.21</druid.version>
  23.         <mp.version>3.3.2</mp.version>
  24.         <fastjson.version>1.2.70</fastjson.version>
  25.         <jwt.version>0.9.1</jwt.version>
  26.         <hutool.version>5.3.7</hutool.version>
  27.         <lombok.versin>1.18.12</lombok.versin>
  28.         <swagger.version>2.9.2</swagger.version>
  29.         <swagger.bootstrap.ui.version>1.9.6</swagger.bootstrap.ui.version>
  30.         <easypoi.version>4.2.0</easypoi.version>
  31.         <jodconverter.version>4.2.0</jodconverter.version>
  32.         <libreoffice.version>6.4.3</libreoffice.version>
  33.         <justauth.version>1.15.6</justauth.version>
  34.         <aliyun.oss.version>3.8.0</aliyun.oss.version>
  35.         <qcloud.oss.version>5.6.23</qcloud.oss.version>
  36.         <aliyun.sms.sdk.version>4.4.6</aliyun.sms.sdk.version>
  37.         <aliyun.sms.esc.version>4.17.6</aliyun.sms.esc.version>
  38.         <qcloud.sms.sdk.version>3.1.57</qcloud.sms.sdk.version>
  39.     </properties>
  40.  
  41.  
  42.     <dependencies>
  43.         <!-- web -->
  44.         <dependency>
  45.             <groupId>org.springframework.boot</groupId>
  46.             <artifactId>spring-boot-starter-web</artifactId>
  47.         </dependency>
  48.  
  49.         <!--mybatis-plus-->
  50.         <dependency>
  51.             <groupId>com.baomidou</groupId>
  52.             <artifactId>mybatis-plus-boot-starter</artifactId>
  53.             <version>${mp.version}</version>
  54.         </dependency>
  55.  
  56.         <!--数据库驱动-->
  57.         <dependency>
  58.             <groupId>mysql</groupId>
  59.             <artifactId>mysql-connector-java</artifactId>
  60.             <version>${mysql-connector-java.version}</version>
  61.         </dependency>
  62.  
  63.         <!--数据库连接池-->
  64.         <dependency>
  65.             <groupId>com.alibaba</groupId>
  66.             <artifactId>druid</artifactId>
  67.             <version>${druid.version}</version>
  68.         </dependency>
  69.  
  70.         <!--hutool-->
  71.         <dependency>
  72.             <groupId>cn.hutool</groupId>
  73.             <artifactId>hutool-all</artifactId>
  74.             <version>${hutool.version}</version>
  75.         </dependency>
  76.  
  77.         <!--lombok-->
  78.         <dependency>
  79.             <groupId>org.projectlombok</groupId>
  80.             <artifactId>lombok</artifactId>
  81.             <version>${lombok.versin}</version>
  82.         </dependency>
  83.  
  84.     </dependencies>
  85.  
  86.  
  87.     <build>
  88.         <resources>
  89.             <resource>
  90.                 <directory>src/main/resources</directory>
  91.                 <!-- <excludes>
  92.                     <exclude>**/*.properties</exclude>
  93.                     <exclude>**/*.xml</exclude>
  94.                 </excludes> -->
  95.                 <includes>
  96.                     <include>**/*.properties</include>
  97.                     <include>**/*.xml</include>
  98.                     <include>**/*.yml</include>
  99.                 </includes>
  100.                 <filtering>false</filtering>
  101.             </resource>
  102.             <resource>
  103.                 <directory>src/main/java</directory>
  104.                 <includes>
  105.                     <include>**/*.xml</include>
  106.                 </includes>
  107.                 <filtering>false</filtering>
  108.             </resource>
  109.         </resources>
  110.     </build>
  111.  
  112.  
  113. </project>

③配置文件如下:主要是数据库和mybaits-plus的配置(其实可以不用这个配置文件,在这只是为了项目能够独立运行起来)

  1. #服务配置
  2. server:
  3.   port: 8080
  4. #spring相关配置
  5. spring:
  6.   datasource:
  7.     driver-class-name: com.mysql.cj.jdbc.Driver
  8.     url: jdbc:mysql://localhost:3306/cx-xn?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=true
  9.     username: 数据库账户
  10.     password: 数据库密码
  11.   servlet:
  12.     multipart:
  13.       max-request-size: 100MB
  14.       max-file-size: 100MB
  15.   jackson:
  16.     time-zone: GMT+8
  17.     date-format: yyyy-MM-dd HH:mm:ss.SSS
  18.     locale: zh_CN
  19.     serialization:
  20.       # 格式化输出
  21.       indent_output: false
  22.  
  23. #mybaits相关配置
  24. mybatis-plus:
  25.   mapper-locations: classpath*:top/wp/cx/**/mapping/*.xml, classpath:/META-INF/modeler-mybatis-mappings/*.xml
  26.   configuration:
  27.     map-underscore-to-camel-case: true
  28.     cache-enabled: true
  29.     lazy-loading-enabled: true
  30.     multiple-result-sets-enabled: true
  31.     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  32.  
  33.   global-config:
  34.     banner: false
  35.     db-config:
  36.       id-type: assign_id
  37.       table-underline: true
  38.     enable-sql-runner: true
  39.   configuration-properties:
  40.     prefix:
  41.     blobType: BLOB
  42.     boolValue: TRUE

④启动入口(可以不用写,启动入口存在目的是让项目可以自己跑起来)

  1. package top.wp.cx;
  2.  
  3. import cn.hutool.log.StaticLog;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6.  
  7. @SpringBootApplication
  8. public class CXApplication {
  9.     public static void main(String[] args) {
  10.         SpringApplication.run(CXApplication.class, args);
  11.         StaticLog.info(">>> " + CXApplication.class.getSimpleName() + " 启动成功!");
  12.  
  13.     }
  14. }

⑤测试:entity、result

  1. package top.wp.cx.modular.test.entity;
  2.  
  3. import com.baomidou.mybatisplus.annotation.IdType;
  4. import com.baomidou.mybatisplus.annotation.TableId;
  5. import com.baomidou.mybatisplus.annotation.TableName;
  6. import lombok.Data;
  7.  
  8.  
  9. @Data
  10. @TableName("test")
  11. public class Test {
  12.  
  13.     /**
  14.      * 主键
  15.      */
  16.     @TableId(type = IdType.ASSIGN_ID)
  17.     private Integer id;
  18.  
  19.     /**
  20.      * 账号
  21.      */
  22.     private String name;
  23.  
  24. }
  1. package top.wp.cx.modular.test.result;
  2.  
  3. import lombok.Data;
  4. @Data
  5. public class TestResult {
  6.  
  7.     private Integer id;
  8.  
  9.     private String name;
  10.  
  11. }

⑥测试mapper、xml、service和controller

  1. package top.wp.cx.modular.test.mapper;
  2.  
  3. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  4. import top.wp.cx.modular.test.entity.Test;
  5.  
  6. /**
  7.  * 系统用户数据范围mapper接口
  8.  *
  9.  * @author xuyuxiang
  10.  * @date 2020/3/13 15:46
  11.  */
  12. //@Mapper
  13. public interface TestMapper extends BaseMapper<Test> {
  14. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="top.wp.cx.modular.test.mapper.TestMapper">
  4.  
  5. </mapper>
  1. package top.wp.cx.modular.test.service;
  2.  
  3. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  4. import org.springframework.stereotype.Service;
  5. import top.wp.cx.modular.test.entity.Test;
  6. import top.wp.cx.modular.test.mapper.TestMapper;
  7.  
  8. /**
  9.  * 一个service实现
  10.  *
  11.  * @author yubaoshan
  12.  * @date 2020/4/9 18:11
  13.  */
  14. @Service
  15. public class TestService extends ServiceImpl<TestMapper, Test> {
  16.  
  17. }
  1. package top.wp.cx.modular.test.controller;
  2.  
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import top.wp.cx.modular.test.entity.Test;
  7. import top.wp.cx.modular.test.service.TestService;
  8.  
  9. import javax.annotation.Resource;
  10. import java.util.List;
  11.  
  12. /**
  13.  * 一个示例接口
  14.  *
  15.  * @author yubaoshan
  16.  * @date 2020/4/9 18:09
  17.  */
  18. @RestController
  19. @RequestMapping("/test")
  20. public class TestController {
  21.  
  22.     @Resource
  23.     private TestService testService;
  24.  
  25.     @GetMapping("")
  26.     public List<Test> testResult(){
  27.         return testService.list();
  28.     }
  29.  
  30.     @GetMapping("/2")
  31.     public String testResult2(){
  32.         return "22";
  33.     }
  34. }

至此项目准备完成,其实就是简单见了一个测试项目,此时如果你按照上面的步骤,写了启动类和配置项信息,项目是可以独立运行的。

2-2项目打包、引入、运行

①将2-1中的测试项目进行打包:install右键第一个选项

2edbb03bcb7c55ed791f8376bfc6add6.png

②此时你的本地maven仓库会出现刚才的项目(当然前提是你的idea配置过本地的maven)

1774b53674430938f06e59011b70706b.png

b4d9c8f525f89eb2a7967e4200602e74.png

③新建另外一个项目cx-main

68492623d613e9a1e84c91ebf46926fe.png

④pom文件如下:注意将你刚才的准备测试的项目引入进来

93508004f513df24a62753e52352d8f3.png

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>
  6.  
  7.     <parent>
  8.         <groupId>org.springframework.boot</groupId>
  9.         <artifactId>spring-boot-starter-parent</artifactId>
  10.         <version>2.3.1.RELEASE</version>
  11.     </parent>
  12.  
  13.     <groupId>top.wp.cx</groupId>
  14.     <artifactId>cx-main</artifactId>
  15.     <version>1.0-SNAPSHOT</version>
  16.  
  17.     <properties>
  18.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20.         <mysql-connector-java.version>8.0.17</mysql-connector-java.version>
  21.         <druid.version>1.1.21</druid.version>
  22.         <mp.version>3.3.2</mp.version>
  23.         <fastjson.version>1.2.70</fastjson.version>
  24.         <jwt.version>0.9.1</jwt.version>
  25.         <hutool.version>5.3.7</hutool.version>
  26.         <lombok.versin>1.18.12</lombok.versin>
  27.         <swagger.version>2.9.2</swagger.version>
  28.         <swagger.bootstrap.ui.version>1.9.6</swagger.bootstrap.ui.version>
  29.         <easypoi.version>4.2.0</easypoi.version>
  30.         <jodconverter.version>4.2.0</jodconverter.version>
  31.         <libreoffice.version>6.4.3</libreoffice.version>
  32.         <justauth.version>1.15.6</justauth.version>
  33.         <aliyun.oss.version>3.8.0</aliyun.oss.version>
  34.         <qcloud.oss.version>5.6.23</qcloud.oss.version>
  35.         <aliyun.sms.sdk.version>4.4.6</aliyun.sms.sdk.version>
  36.         <aliyun.sms.esc.version>4.17.6</aliyun.sms.esc.version>
  37.         <qcloud.sms.sdk.version>3.1.57</qcloud.sms.sdk.version>
  38.     </properties>
  39.  
  40.  
  41.     <dependencies>
  42.  
  43.         <dependency>
  44.             <groupId>top.wp</groupId>
  45.             <artifactId>cx-flow</artifactId>
  46.             <version>1.0-SNAPSHOT</version>
  47.         </dependency>
  48.  
  49.         <!-- web -->
  50.         <dependency>
  51.             <groupId>org.springframework.boot</groupId>
  52.             <artifactId>spring-boot-starter-web</artifactId>
  53.         </dependency>
  54.  
  55.         <!--mybatis-plus-->
  56.         <dependency>
  57.             <groupId>com.baomidou</groupId>
  58.             <artifactId>mybatis-plus-boot-starter</artifactId>
  59.             <version>${mp.version}</version>
  60.         </dependency>
  61.  
  62.         <!--数据库驱动-->
  63.         <dependency>
  64.             <groupId>mysql</groupId>
  65.             <artifactId>mysql-connector-java</artifactId>
  66.             <version>${mysql-connector-java.version}</version>
  67.         </dependency>
  68.  
  69.         <!--数据库连接池-->
  70.         <dependency>
  71.             <groupId>com.alibaba</groupId>
  72.             <artifactId>druid</artifactId>
  73.             <version>${druid.version}</version>
  74.         </dependency>
  75.  
  76.         <!--hutool-->
  77.         <dependency>
  78.             <groupId>cn.hutool</groupId>
  79.             <artifactId>hutool-all</artifactId>
  80.             <version>${hutool.version}</version>
  81.         </dependency>
  82.  
  83.         <!--lombok-->
  84.         <dependency>
  85.             <groupId>org.projectlombok</groupId>
  86.             <artifactId>lombok</artifactId>
  87.             <version>${lombok.versin}</version>
  88.         </dependency>
  89.  
  90.     </dependencies>
  91.  
  92.  
  93.     <!--xml打包排除-->
  94.     <build>
  95.         <resources>
  96.             <resource>
  97.                 <directory>src/main/resources</directory>
  98.                 <!-- <excludes>
  99.                     <exclude>**/*.properties</exclude>
  100.                     <exclude>**/*.xml</exclude>
  101.                 </excludes> -->
  102.                 <includes>
  103.                     <include>**/*.properties</include>
  104.                     <include>**/*.xml</include>
  105.                     <include>**/*.yml</include>
  106.                 </includes>
  107.                 <filtering>false</filtering>
  108.             </resource>
  109.             <resource>
  110.                 <directory>src/main/java</directory>
  111.                 <includes>
  112.                     <include>**/*.xml</include>
  113.                 </includes>
  114.                 <filtering>false</filtering>
  115.             </resource>
  116.         </resources>
  117.     </build>
  118.  
  119. </project>

⑤application.yml配置文件 注意xml的扫描

f11f025c808090f7233bb7fff89957cc.png

  1. #服务配置
  2. server:
  3.   port: 8081
  4. #spring相关配置
  5. spring:
  6.   datasource:
  7.     driver-class-name: com.mysql.cj.jdbc.Driver
  8.     url: jdbc:mysql://localhost:3306/cx-xn?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=true
  9.     username: root
  10.     password: root
  11.   servlet:
  12.     multipart:
  13.       max-request-size: 100MB
  14.       max-file-size: 100MB
  15.   jackson:
  16.     time-zone: GMT+8
  17.     date-format: yyyy-MM-dd HH:mm:ss.SSS
  18.     locale: zh_CN
  19.     serialization:
  20.       # 格式化输出
  21.       indent_output: false
  22.  
  23. #mybaits相关配置
  24. mybatis-plus:
  25.   #xml文件扫描
  26.   mapper-locations: classpath*:top/wp/cx/**/mapping/*.xml, classpath:/META-INF/modeler-mybatis-mappings/*.xml
  27.   configuration:
  28.     map-underscore-to-camel-case: true
  29.     cache-enabled: true
  30.     lazy-loading-enabled: true
  31.     multiple-result-sets-enabled: true
  32.     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  33.  
  34.   global-config:
  35.     banner: false
  36.     db-config:
  37.       id-type: assign_id
  38.       table-underline: true
  39.     enable-sql-runner: true
  40.   configuration-properties:
  41.     prefix:
  42.     blobType: BLOB
  43.     boolValue: TRUE

⑥启动入口,注意spring和mapper扫描

43a9e9b9fbc1e31e710810e59fe6f8f7.png

  1. package top.wp.cx.main;
  2.  
  3. import cn.hutool.log.StaticLog;
  4. import org.mybatis.spring.annotation.MapperScan;
  5. import org.springframework.boot.SpringApplication;
  6. import org.springframework.boot.autoconfigure.SpringBootApplication;
  7. import org.springframework.context.annotation.ComponentScan;
  8.  
  9. @SpringBootApplication
  10. @ComponentScan(basePackages = {"top.wp.cx.modular.test"}) // spring扫描
  11. @MapperScan(basePackages = {"top.wp.cx.modular.test.**.mapper"}) // mybatis扫描mapper
  12. public class CXApplication {
  13.     public static void main(String[] args) {
  14.         SpringApplication.run(CXApplication.class, args);
  15.         StaticLog.info(">>> " + CXApplication.class.getSimpleName() + " 启动成功!");
  16.     }
  17. }

⑦此时启动cx-main的项目,访问2-1的测试controller能访问成功证明配置正确。

  1. 作者:<每天一点>
  2. https://blog.csdn.net/weixin_44213308/article/details/111151350
  3. 公众号“Java精选”所发表内容注明来源的,版权归原出处所有(无法查证版权的或者未注明出处的均来自网络,系转载,转载的目的在于传递更多信息,版权属于原作者。如有侵权,请联系,笔者会第一时间删除处理!
  4. 最近有很多人问,有没有读者交流群!加入方式很简单,公众号Java精选,回复“加群”,即可入群!
  5. Java精选面试题(微信小程序):3000+道面试题,包含Java基础、并发、JVM、线程、MQ系列、Redis、Spring系列、Elasticsearch、Docker、K8s、Flink、Spark、架构设计等,在线随时刷题!
  6. ------ 特别推荐 ------
  7. 特别推荐:专注分享最前沿的技术与资讯,为弯道超车做好准备及各种开源项目与高效率软件的公众号,「大咖笔记」,专注挖掘好东西,非常值得大家关注。点击下方公众号卡片关注。
  8. 点击“阅读原文”,了解更多精彩内容!文章有帮助的话,点在看,转发吧!
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/283941?site
推荐阅读
相关标签
  

闽ICP备14008679号