炫酷的企业网站,网站开发工具.晴天娃娃,网站建设礻金手指下拉十二,肇庆专业网站建设公司背景
使用Idea 创建一个模块化的SpringBoot项目#xff0c;但是发现Idea 创建父子项目的方式较Eclipse 较为不同#xff0c;且Idea 创建的过程较Eclipse创建父子项目的过程复杂。 Eclipse 创建SpringBoot父子项目传送门
网上虽然有Idea创建SpringBoot父子项目#xff0c;但…背景
使用Idea 创建一个模块化的SpringBoot项目但是发现Idea 创建父子项目的方式较Eclipse 较为不同且Idea 创建的过程较Eclipse创建父子项目的过程复杂。 Eclipse 创建SpringBoot父子项目传送门
网上虽然有Idea创建SpringBoot父子项目但是携带各种其他的功能导致无法简单的搞懂如何创建。下面就一个例子进行说明一下。
构建简单SpringBoot父子项目
环境说明jdk1.8 Idea 版本2019.3 1打开Idea如果是新安装的软件不会有历史项目 2点击 “Create New Project”进入一个新的项目的创建界面。父项目直接创建一个Maven项目即可 PS不要勾选 “Create from archetype” 点击下一步。 3进入父项目的创建页面填写项目名称和GoupId与坐标 PS可以在上图红圈标注的地方修改项目的地址 4点击Finish 按钮完成一个父项目的创建。 下图是点击Finish 创建好的项目在Idea 软件中打开的情况 5在现有父项目的基础上新建SpringBoot子项目 在父项目的名称的位置右键鼠标 6新建一个Module进入新建页面。 子项目直接选择Spring initializr创建一个SpringBoot项目 点击下一步进入子项目的具体构建内容。 填写坐标后点击下一步。 7进入新建项目选择插件由于我们测试需要所以直接简单的选择了web 点击下一步可以更改项目名称和地址 可以看出子项目的地址默认在父项目文件夹下。点击Finish完成子项目的创建。 8可以删除一些子项目不需要的文件不删也行 P 还有父项目的src文件夹这个已经不需要了所以可以直接删除
9经过 1-8 步的操作我们创建了父子项目结构但是这两个项目尚未实现父子关系。下面我们需要更改POM文件来实现父子关系。 这里我们先粘贴出还没有改变关系之前的父子结构项目原有的POM 文件
父项目
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.codingdemo/groupIdartifactIdspringboot-demo/artifactIdversion1.0-SNAPSHOT/version
/project子项目
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.2.2.RELEASE/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.codingdemo/groupIdartifactIdspringboot-demo-redis/artifactIdversion0.0.1-SNAPSHOT/versionnamespringboot-demo-redis/namedescriptionDemo project for Spring Boot/descriptionpropertiesjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scopeexclusionsexclusiongroupIdorg.junit.vintage/groupIdartifactIdjunit-vintage-engine/artifactId/exclusion/exclusions/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project10首先将你认为子项目中是公共的部分拿到父项目中,包括spring-boot-starter-parent 和一些 properties 以及build 等公共配置
!--1 --parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.2.2.RELEASE/versionrelativePath/ !-- lookup parent from repository --/parent
!--2 --
propertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingproject.reporting.outputEncodingUTF-8/project.reporting.outputEncodingjava.version1.8/java.version/properties
!--3--
dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scopeexclusionsexclusiongroupIdorg.junit.vintage/groupIdartifactIdjunit-vintage-engine/artifactId/exclusion/exclusions/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build上面子项目中的配置文件都可以拿到父项目的POM文件中 。 11将父项目的坐标复制到 子项目的父坐标中 parentgroupIdcom.codingdemo/groupIdartifactIdspringboot-demo/artifactIdversion1.0-SNAPSHOT/version/parent(12) 父项目 新增 pom P这个必须 13将子项目的moudle 关联到 父项目中 modulesmodulespringboot-demo-redis/module/modules(14)我们再来看下新的父子项目的 POM文件
父项目
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.2.2.RELEASE/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.codingdemo/groupIdartifactIdspringboot-demo/artifactIdversion1.0-SNAPSHOT/versionpackagingpom/packagingmodulesmodulespringboot-demo-redis/module/modulespropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingproject.reporting.outputEncodingUTF-8/project.reporting.outputEncodingjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scopeexclusionsexclusiongroupIdorg.junit.vintage/groupIdartifactIdjunit-vintage-engine/artifactId/exclusion/exclusions/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project子项目
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdcom.codingdemo/groupIdartifactIdspringboot-demo/artifactIdversion1.0-SNAPSHOT/version/parentgroupIdcom.codingdemo/groupIdartifactIdspringboot-demo-redis/artifactIdversion0.0.1-SNAPSHOT/versionnamespringboot-demo-redis/namedescriptionDemo project for Spring Boot/description/project读者可以对比一下没有改造之前的POM文件有什么差距 (15) 我们来验证一下父子项目是否成功创建 我们在子项目中新增一个Controller 进行一下简单Web访问查看是否成功
package com.codingdemo.springbootdemoredis.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;RestController
public class HelloController {GetMapping(/hello)public String hello() {return hello world!;}
}启动一下SpringBoot 启动类,输入网址 可以看到能够成功访问我们已经成功搭建好简单的父子SpringBoot项目 16 如果我修改 Controller的方法需要重启项目这样比较麻烦。下面是如何配置热部署SpringBoot项目
SpringBoot项目的热部署
1在上面现有的父子SpringBoot项目中在父项目POM文件增加热部署的devtools依赖。
!-- 热加载 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdoptionaltrue/optionalscopetrue/scope/dependencybuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId!--热加载必须配上以下内容--configurationforktrue/fork/configuration/plugin/plugins/build(2) 我们可以在配置文件中设置devtools
### 热部署配置
spring.devtools.restart.enabledtrue
spring.devtools.restart.additional-pathssrc/main/java
spring.devtools.restart.poll-interval1s
spring.freemarker.cachefalse3如果修改了Controller的函数但是还是没有热加载可能是由于你是新的项目需要设置开启自动编译 P勾上 重启项目后然后再次修改文件后便可以看到开始热部署了