1、背景
首先,談一談什么是“springBoot業(yè)務(wù)組件化開(kāi)發(fā)”,最近一直在開(kāi)發(fā)一直面臨這一個(gè)問(wèn)題,就是相同的業(yè)務(wù)場(chǎng)景場(chǎng)景在一個(gè)項(xiàng)目中使用了,又需要再另外一個(gè)項(xiàng)目中復(fù)用,一遍又一遍的復(fù)制代碼,然后想將該業(yè)務(wù)的代碼在不同的項(xiàng)目中維護(hù)起來(lái)真的很難。
最開(kāi)始想用微服務(wù)的方式來(lái)解決這個(gè)問(wèn)題,但是覺(jué)得一套完整的微服務(wù)太重,而且目前微服務(wù)還處于振蕩期(去年的微服務(wù)解決方案,今年國(guó)內(nèi)直接都換成了阿里的技術(shù)解決方案),此外很多時(shí)候我們接私活,就是個(gè)單體的springboot項(xiàng)目,用不上微服務(wù)這種級(jí)別的項(xiàng)目,所以想來(lái)想去這條路不是很滿足我的需求;再后來(lái),想到單體的聚合架構(gòu),但是聚合架構(gòu)的項(xiàng)目,個(gè)人覺(jué)得有時(shí)候也不是很好,一般的聚合項(xiàng)目就是基于某個(gè)具體實(shí)例架構(gòu)下才能使用,換一個(gè)架構(gòu)自己寫的業(yè)務(wù)model就不能用了(比如你在suoyi框架下開(kāi)發(fā)的模塊業(yè)務(wù)包,在guns下可能就直接不能使用了)。
最后,想了一下,能不能單獨(dú)開(kāi)發(fā)一個(gè)項(xiàng)目,這個(gè)項(xiàng)目可以自己獨(dú)立運(yùn)行(微服務(wù)架構(gòu)下用),也可以在單體項(xiàng)目中直接通過(guò)pom引入的方式,然后簡(jiǎn)單的配置一下,然后直接使用多好;查了一下網(wǎng)上沒(méi)有現(xiàn)成的技術(shù)解決方案,問(wèn)了同事,他說(shuō)我這種思想屬于SOA的一種實(shí)現(xiàn),同時(shí)有第三包和聚合項(xiàng)目的影子在里面。也許有什么更好的技術(shù)解決方案,也希望各位能夠不吝賜教。
補(bǔ)充一句,之所以說(shuō)“業(yè)務(wù)組件化”開(kāi)發(fā),來(lái)源于Vue的思想,希望Java后端開(kāi)發(fā)的業(yè)務(wù)也可像vue的組件一樣去使用,這樣多好
2、demo
2-1項(xiàng)目準(zhǔn)備
①建一個(gè)Java項(xiàng)目項(xiàng)目,結(jié)構(gòu)如下圖:
②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
③配置文件如下:主要是數(shù)據(jù)庫(kù)和mybaits-plus的配置(其實(shí)可以不用這個(gè)配置文件,在這只是為了項(xiàng)目能夠獨(dú)立運(yùn)行起來(lái))
#服務(wù)配置 server: port:8080 #spring相關(guān)配置 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:數(shù)據(jù)庫(kù)賬戶 password:數(shù)據(jù)庫(kù)密碼 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相關(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)目可以自己跑起來(lái))
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()+"啟動(dòng)成功!"); } }
⑤測(cè)試: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; /** *賬號(hào) */ privateStringname; } packagetop.wp.cx.modular.test.result; importlombok.Data; @Data publicclassTestResult{ privateIntegerid; privateStringname; }
⑥測(cè)試mapper、xml、service和controller
packagetop.wp.cx.modular.test.mapper; importcom.baomidou.mybatisplus.core.mapper.BaseMapper; importtop.wp.cx.modular.test.entity.Test; /** *系統(tǒng)用戶數(shù)據(jù)范圍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; /** *一個(gè)service實(shí)現(xiàn) * *@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; /** *一個(gè)示例接口 * *@authoryubaoshan *@date2020/4/918:09 */ @RestController @RequestMapping("/test") publicclassTestController{ @Resource privateTestServicetestService; @GetMapping("") publicList testResult(){ returntestService.list(); } @GetMapping("/2") publicStringtestResult2(){ return"22"; } }
至此項(xiàng)目準(zhǔn)備完成,其實(shí)就是簡(jiǎn)單見(jiàn)了一個(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倉(cāng)庫(kù)會(huì)出現(xiàn)剛才的項(xiàng)目(當(dāng)然前提是你的idea配置過(guò)本地的maven)
③新建另外一個(gè)項(xiàng)目cx-main
④pom文件如下:注意將你剛才的準(zhǔn)備測(cè)試的項(xiàng)目引入進(jìn)來(lái)
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的掃描
#服務(wù)配置 server: port:8081 #spring相關(guān)配置 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相關(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掃描
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()+"啟動(dòng)成功!"); } }
⑦此時(shí)啟動(dòng)cx-main的項(xiàng)目,訪問(wèn)2-1的測(cè)試controller能訪問(wèn)成功證明配置正確
審核編輯:劉清
-
SOA
+關(guān)注
關(guān)注
1文章
300瀏覽量
28034 -
JAVA語(yǔ)言
+關(guān)注
關(guān)注
0文章
138瀏覽量
20525 -
gun
+關(guān)注
關(guān)注
0文章
6瀏覽量
7666 -
SpringBoot
+關(guān)注
關(guān)注
0文章
175瀏覽量
322
原文標(biāo)題:談?wù)?SpringBoot 業(yè)務(wù)組件化開(kāi)發(fā)思路
文章出處:【微信號(hào):芋道源碼,微信公眾號(hào):芋道源碼】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
springboot集成mqtt
SpringBoot應(yīng)用啟動(dòng)運(yùn)行run方法
SpringBoot配置嵌入式Servlet
基于組件的業(yè)務(wù)模型的研究與設(shè)計(jì)
深入了解SpringBoot的自動(dòng)配置原理

什么是 SpringBoot?

SpringBoot常用注解及使用方法1
SpringBoot的核心注解1

使用springboot完成流程的業(yè)務(wù)功能

Springboot項(xiàng)目的集成以及具體使用及配置

評(píng)論