在创建springboot项目的时候,我们希望一个主题的多个子模块可以共享彼此的逻辑。springboot强调为服务,各自为战。但是我还是希望可以将同一个主题的多个springboot程序放在一起,它们可以共享通用的javabean、DAO和业务逻辑代码。
下面是helloWorld的项目结构:
helloWorld ├── helloWorld-commons //通用的javabean和工具类 │ ├── pom.xml │ ├── src │ └── target ├── helloWorld-dao // jdbc的 DAO │ ├── pom.xml │ ├── src │ └── target ├── helloWorld-service // 业务逻辑服务类 │ ├── pom.xml │ ├── src │ └── target ├── helloWorld-web // web程序 │ ├── pom.xml │ ├── src │ └── target └── pom.xml
下面是根pom.xml
<?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> <groupId>org.kanpiaoxue</groupId> <artifactId>helloWorld</artifactId> <version>0.0.1</version> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <modules> <module>helloWorld-commons</module> <module>helloWorld-dao</module> <module>helloWorld-service</module> <module>helloWorld-web</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.source.version>1.8</java.source.version> <java.target.version>1.8</java.target.version> </properties> <dependencyManagement> <dependencies> <!-- 这里配置各个模块需要的通用依赖,但是不配置 springboot 的各个依赖。 springboot的各个依赖配置在各个module中 --> ... </dependencies> </dependencyManagement> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>${java.source.version}</source> <target>${java.target.version}</target> <encoding>utf8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4.3</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.1.0</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> <configuration> <skipTests>false</skipTests> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
helloWorld-commons 模块的pom.xml: 注意它使用到了log日志,注意它是如何在pom里面引用log的。
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.kanpiaoxue</groupId> <artifactId>helloWorld</artifactId> <version>0.0.1</version> </parent> <artifactId>helloWorld-commons</artifactId> <name>helloWorld-commons</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- log start --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> <!-- log end --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency> <!-- start apache --> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </dependency> <!-- end apache --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> </dependency> <!-- test start --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4-rule-agent</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock.tests</groupId> <artifactId>powermock-tests-utils</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-core</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-classloading-xstream</artifactId> <scope>test</scope> </dependency> <!-- test end --> </dependencies> </project>
其他模块的pom.xml 就不一一罗列了,这里最关键是helloWorld-web的pom.xml 因为只有它才是真正的springboot的可执行程序。它的pom.xml如下:
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.kanpiaoxue</groupId> <artifactId>hello</artifactId> <version>0.0.1</version> </parent> <artifactId>hello-web</artifactId> <name>hello-web</name> <packaging>jar</packaging> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <helloWorld.version>0.0.1</helloWorld.version> </properties> <dependencies> <dependency> <groupId>org.kanpiaoxue</groupId> <artifactId>helloWorld-commons</artifactId> <version>${helloWorld.version}</version> </dependency> <dependency> <groupId>org.kanpiaoxue</groupId> <artifactId>helloWorld-dao</artifactId> <version>${helloWorld.version}</version> </dependency> <dependency> <groupId>org.kanpiaoxue</groupId> <artifactId>helloWorld-service</artifactId> <version>${helloWorld.version}</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency> <!-- springboot start --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!-- springboot start --> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </dependency> <!-- end apache --> <!-- DBPool start --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> </dependency> <!-- DBPool end --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> </dependency> <!-- test start --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4-rule-agent</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock.tests</groupId> <artifactId>powermock-tests-utils</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-core</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-classloading-xstream</artifactId> <scope>test</scope> </dependency> <!-- test end --> </dependencies> <!-- spring-boot-maven-plugin start --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
【特别注意】
这里需要额外注意一个问题:带有 @SpringBootApplication 的 Application.java(内含publicstaticvoid main(String[] args))方法的类,一定要放在几个module公用的package的那一层级,否则会引起找不到各个@Servce等注解类的情况。
org.kanpiaoxue.helloworld_commons org.kanpiaoxue.helloworld_dao org.kanpiaoxue.helloworld_service org.kanpiaoxue.helloworld_web
针对上面的几个module,它们各自有自己的package。如上。
我在 org.kanpiaoxue.helloworld_web 下面创建了带有 @SpringBootApplication 的 Application.java(内含public static void main(String[] args))方法的类,启动程序的是否发现总是有服务类无法被扫描到。因为 @SpringBootApplication 是从当前 Application.java 所在的package:org.kanpiaoxue.helloworld_web 向下扫描的,它自然无法扫描到其他的几个package:
org.kanpiaoxue.helloworld_commons
org.kanpiaoxue.helloworld_dao
org.kanpiaoxue.helloworld_service
解决方案:将带有 @SpringBootApplication 的 Application.java(内含public static void main(String[] args))方法的类,挪到这几个module公用的package的路径:
org.kanpiaoxue
下面,就解决了该问题。
我发现也有人遇到类似的问题,可以参考:
关于SpringBoot bean无法注入的问题(与文件包位置有关)改变自动扫描的:https://blog.csdn.net/u014695188/article/details/52263903
还有一种解决Application.java找不到各种@Service的方法就是在@SpringBootApplication里面添加包的扫描路径。如下:
@SpringBootApplication(scanBasePackages = "org.kanpiaoxue") @EnableTransactionManagement @MapperScan("org.kanpiaoxue.csis.dao") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
相关推荐
springboot-multi-module-v2:docker部署maven构建多模块springboot项目
然后,创建子模块 boot_test_oneserver,并选择 Maven Module 项目类型。 依赖版本管理 在 pom 文件中,需要进行依赖版本管理,以便正确地依赖项目所需的组件。常用的依赖项包括 Springboot 启动器、热部署、...
maven管理的多模块聚合的springboot+duubo+zokeeper+Redis整合demo,一个父pom文件,分为api/service/client多个子module
springBoot 2.0开发构建多模块应用及使用Maven进行项目打包。 1. 定义用户模型:ID、名称 2. 客户端发送 POST 请求,创建用户(Web MVC形式) 3. 客户端发送 GET 请求,获取所有用户(Web Flux形式——Spring...
Spring Boot多模块项目模板主模块位于master之下。
NULL 博文链接:https://maosheng.iteye.com/blog/2383973
三、idea以maven项目导入该项目后,发现没有maven的依赖包时,需要对每个maven module进行clear和install,并且注意maven的依赖顺序 SpringBoot开发非常美观的java博客系统(包含后台管理功能) SpringBoot开发非常...
- `unicorn-starter` [启动类(Maven),项目入口,包含模块及组件配置(DB读写分离 + Cache读写分离)](https://github.com/lWoHvYe/unicorn-starter) - `valentine-starter` 启动配置示例(Gradle),尝试Kotlin/...
最近一直在弄 springboot项目,但是只能是 单个模块开发,一直想做成多模块开发,但是 使用maven打包,一直打包不成功,总是提示: 找不到主类 。下面总结一下 多module 打包,做一个maven工程,可实现对多模块的...
基于SpringBoot+mybatis搜索引擎优化的健康问答系统系统java源码+项目说明.7z 采用spring boot + mybatis 框架构建 前端模型采用 theleaf 模板 爬虫模块采用 webmagic 框架 搜索引擎模块采用 luence 框架 数据源采用...
java8 源码 ###搭建一个mybatis,mysql,druid,...master分支,主分支(multi-module:Maven多模块结构) 2.0.1.RELEASE Springboot版本 1.5.6.RELEASE Springboot版本 single-module:mybatis全部环境,提供接口 mvc-page分支:
springmvc-multi-module-project:源码主要为学习整合的SSM框架:SpringMVC + Spring + MyBatis构建maven多模块工程项目,根据不同功能划分模块,方便管理和解压缩。项目主要涉及到的内容有:基于SpringMVC框架(非...
一个基于Maven的SpringBoot多模块项目搭建 支持Https协议 支持Http自动转Https协议 加入模块,自动打印请求信息
│ Maven__com_fasterxml_jackson_module_jackson_module_parameter_names_2_10_3.xml │ Maven__com_jayway_jsonpath_json_path_2_4_0.xml │ Maven__...
- Maven Multi-Module - WebSocket - ElasticSearch # 功能们: ## 用户模块 - 获取图片验证码 - 登录:解决重复登录问题 - 注册 - 分页查询用户信息 - 修改用户信息 ## 站内信模块 - 一对一发送站内信 - ...
Spring启动:简单的多模块项目 通过此设置,可以在项目的根目录中mvn clean spring-boot:run通常的mvn clean spring-boot:run 。 某些教程中缺少此功能,例如 也可以看看
jun_spring 项目module组件功能列表【spring_helloworld】springboot 的一个 helloworld【spring_properties】springboot 读取配置文件中的内容TODO:1、整cs的login跟index的page,适配Nginx跟ssm_jwt;2、整ssm_...
该项目是一个maven project项目,module test1主要spring boot的简单搭建以及与mybatis的集成,module test2主要是多数据源的构建。
添加新module时使用 idea->File->new->Module, 直接建立空maven项目, 注意在idea中选择正确的parent(一般选car_trade_project) 本地zookeeper搭建方式 需要用到 8887 8080 8081 8082端口, 注意其它应用不能占用这几...
基于Spring家族的360度整合,基本覆盖开发用九成以上的场景,即开即用,主要是详尽的了解项目里面的每个stater跟lib组件及功能,为个人及项目实际开发提供助力,帮组项目快速开发,本项目是maven项目,每个组件均...