什么是springBoot业务组件化开发?谈谈SpringBoot业务组件化

电子说

1.2w人已加入

描述

1、背景

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

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

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

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

2、demo

2-1项目准备

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

SpringBoot

②pom文件如下:

 



    4.0.0
 
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.1.RELEASE
    
 
    top.wp
    cx-flow
    1.0-SNAPSHOT
 
 
    
        UTF-8
        UTF-8
        8.0.17
        1.1.21
        3.3.2
        1.2.70
        0.9.1
        5.3.7
        1.18.12
        2.9.2
        1.9.6
        4.2.0
        4.2.0
        6.4.3
        1.15.6
        3.8.0
        5.6.23
        4.4.6
        4.17.6
        3.1.57
    
 
 
    
         
        
            org.springframework.boot
            spring-boot-starter-web
        
 
         
        
            com.baomidou
            mybatis-plus-boot-starter
            ${mp.version}
        
 
         
        
            mysql
            mysql-connector-java
            ${mysql-connector-java.version}
        
 
         
        
            com.alibaba
            druid
            ${druid.version}
        
 
         
        
            cn.hutool
            hutool-all
            ${hutool.version}
        
 
         
        
            org.projectlombok
            lombok
            ${lombok.versin}
        
 
    
 
 
    
        
            
                src/main/resources
                 
                
                    **/*.properties
                    **/*.xml
                    **/*.yml
                
                false
            
            
                src/main/java
                
                    **/*.xml
                
                false
            
        
    
 
 

 

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

 

#服务配置
server:
  port: 8080
#spring相关配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc//localhost:3306/cx-xn?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&nullCatalogMeansCurrent=true
    username: 数据库账户
    password: 数据库密码
  servlet:
    multipart:
      max-request-size: 100MB
      max-file-size: 100MB
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HHss.SSS
    locale: zh_CN
    serialization:
      # 格式化输出
      indent_output: false
 
#mybaits相关配置
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

 

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

 

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() + " 启动成功!");
 
    }
}

 

⑤测试: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;
 
    /**
     * 账号
     */
    private String name;
 
}
package top.wp.cx.modular.test.result;
 
import lombok.Data;
@Data
public class TestResult {
 
    private Integer id;
 
    private String name;
 
}

 

⑥测试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;
 
/**
 * 系统用户数据范围mapper接口
 *
 * @author xuyuxiang
 * @date 2020/3/13 15:46
 */
//@Mapper
public interface TestMapper extends BaseMapper {
}



 

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;
 
/**
 * 一个service实现
 *
 * @author yubaoshan
 * @date 2020/4/9 18:11
 */
@Service
public class TestService extends ServiceImpl {
 
}
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;
 
/**
 * 一个示例接口
 *
 * @author yubaoshan
 * @date 2020/4/9 18:09
 */
@RestController
@RequestMapping("/test")
public class TestController {
 
    @Resource
    private TestService testService;
 
    @GetMapping("")
    public List testResult(){
        return testService.list();
    }
 
    @GetMapping("/2")
    public String testResult2(){
        return "22";
    }
}

 

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

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

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

SpringBoot

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

SpringBootSpringBoot

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

SpringBoot

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

SpringBoot

 



    4.0.0
 
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.1.RELEASE
    
 
    top.wp.cx
    cx-main
    1.0-SNAPSHOT
 
    
        UTF-8
        UTF-8
        8.0.17
        1.1.21
        3.3.2
        1.2.70
        0.9.1
        5.3.7
        1.18.12
        2.9.2
        1.9.6
        4.2.0
        4.2.0
        6.4.3
        1.15.6
        3.8.0
        5.6.23
        4.4.6
        4.17.6
        3.1.57
    
 
 
    
 
        
            top.wp
            cx-flow
            1.0-SNAPSHOT
        
 
         
        
            org.springframework.boot
            spring-boot-starter-web
        
 
         
        
            com.baomidou
            mybatis-plus-boot-starter
            ${mp.version}
        
 
         
        
            mysql
            mysql-connector-java
            ${mysql-connector-java.version}
        
 
         
        
            com.alibaba
            druid
            ${druid.version}
        
 
         
        
            cn.hutool
            hutool-all
            ${hutool.version}
        
 
         
        
            org.projectlombok
            lombok
            ${lombok.versin}
        
 
    
 
 
     
    
        
            
                src/main/resources
                 
                
                    **/*.properties
                    **/*.xml
                    **/*.yml
                
                false
            
            
                src/main/java
                
                    **/*.xml
                
                false
            
        
    
 

 

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

SpringBoot

 

#服务配置
server:
  port: 8081
#spring相关配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc//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 HHss.SSS
    locale: zh_CN
    serialization:
      # 格式化输出
      indent_output: false
 
#mybaits相关配置
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

 

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

SpringBoot

 

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() + " 启动成功!");
    }
}

 

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





审核编辑:刘清

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分