全球最实用的IT互联网信息网站!

AI人工智能P2P分享&下载搜索网页发布信息网站地图

当前位置:诺佳网 > 电子/半导体 > 嵌入式技术 >

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

时间:2023-07-20 11:30

人气:

作者:admin

标签: gun  SOA  JAVA语言 

导读:首先,谈一谈什么是“springBoot业务组件化开发”,最近一直在开发一直面临这一个问题,就是相同的业务场景场景在一个项目中使用了,又需要再另外一个项目中复用,一遍又一遍的复...

1、背景

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

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

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

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

2、demo

2-1项目准备

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

79523374-269e-11ee-962d-dac502259ad0.png

②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-ddHHss.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

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

packagetop.wp.cx;

importcn.hutool.log.StaticLog;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
publicclassCXApplication{
publicstaticvoidmain(String[]args){
SpringApplication.run(CXApplication.class,args);
StaticLog.info(">>>"+CXApplication.class.getSimpleName()+"启动成功!");

}
}

⑤测试:entity、result

packagetop.wp.cx.modular.test.entity;

importcom.baomidou.mybatisplus.annotation.IdType;
importcom.baomidou.mybatisplus.annotation.TableId;
importcom.baomidou.mybatisplus.annotation.TableName;
importlombok.Data;


@Data
@TableName("test")
publicclassTest{

/**
*主键
*/
@TableId(type=IdType.ASSIGN_ID)
privateIntegerid;

/**
*账号
*/
privateStringname;

}
packagetop.wp.cx.modular.test.result;

importlombok.Data;
@Data
publicclassTestResult{

privateIntegerid;

privateStringname;

}

⑥测试mapper、xml、service和controller

packagetop.wp.cx.modular.test.mapper;

importcom.baomidou.mybatisplus.core.mapper.BaseMapper;
importtop.wp.cx.modular.test.entity.Test;

/**
*系统用户数据范围mapper接口
*
*@authorxuyuxiang
*@date2020/3/1315:46
*/
//@Mapper
publicinterfaceTestMapperextendsBaseMapper{
}





packagetop.wp.cx.modular.test.service;

importcom.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
importorg.springframework.stereotype.Service;
importtop.wp.cx.modular.test.entity.Test;
importtop.wp.cx.modular.test.mapper.TestMapper;

/**
*一个service实现
*
*@authoryubaoshan
*@date2020/4/918:11
*/
@Service
publicclassTestServiceextendsServiceImpl{

}
packagetop.wp.cx.modular.test.controller;

importorg.springframework.web.bind.annotation.GetMapping;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RestController;
importtop.wp.cx.modular.test.entity.Test;
importtop.wp.cx.modular.test.service.TestService;

importjavax.annotation.Resource;
importjava.util.List;

/**
*一个示例接口
*
*@authoryubaoshan
*@date2020/4/918:09
*/
@RestController
@RequestMapping("/test")
publicclassTestController{

@Resource
privateTestServicetestService;

@GetMapping("")
publicListtestResult(){
returntestService.list();
}

@GetMapping("/2")
publicStringtestResult2(){
return"22";
}
}

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

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

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

7998771c-269e-11ee-962d-dac502259ad0.png

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

7a082f62-269e-11ee-962d-dac502259ad0.png7a2e12ae-269e-11ee-962d-dac502259ad0.png

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

7b31d15e-269e-11ee-962d-dac502259ad0.png

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

7b5e56a2-269e-11ee-962d-dac502259ad0.png



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的扫描

7b82a796-269e-11ee-962d-dac502259ad0.png

#服务配置
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-ddHHss.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扫描

7ba9eed2-269e-11ee-962d-dac502259ad0.png

packagetop.wp.cx.main;

importcn.hutool.log.StaticLog;
importorg.mybatis.spring.annotation.MapperScan;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
importorg.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages={"top.wp.cx.modular.test"})//spring扫描
@MapperScan(basePackages={"top.wp.cx.modular.test.**.mapper"})//mybatis扫描mapper
publicclassCXApplication{
publicstaticvoidmain(String[]args){
SpringApplication.run(CXApplication.class,args);
StaticLog.info(">>>"+CXApplication.class.getSimpleName()+"启动成功!");
}
}

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





审核编辑:刘清

温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信