选择 Spring Initializer

父项目无需选择任何一个 Dependencies ,因为它不会作为一个具体项目去使用,而是供具体的子模块来继承。然后在父项目的 pom.xml 中添加:

1
<packaging>pom</packaging>

若不添加该节点,则打包子模块时会报错

子模块记得让 Maven 来 Enable Auto-Import

此时,需要修改子项目中的继承方式:

修改后,继承关系为:spring_initializer ——> springboot_parent ——> spring-boot-starter-parent

继承后的好处:

修改SpringBoot 版本号,只需更改 parent 的 pom.xml 即可。

1
2
3
4
5
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.6.RELEASE</version> <!--更改此处的版本号即可-->
</parent>

image-20210305212623163

1
2
3
4
5
6
7
8
9
10
11
12
13
package top.joydee.controllers;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/sayhello")
public String sayHello(){
return "Hello, SpringBoot!";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.3)

2021-03-05 16:39:25.956 INFO 15928 --- [ main] t.j.SpringInitializerDemoApplication : Starting SpringInitializerDemoApplication using Java 15 on DESKTOP-TUSDBAD with PID 15928 (E:\springboot_parent\spring_initializer_demo\target\classes started by luffy in E:\springboot_parent)
2021-03-05 16:39:25.959 INFO 15928 --- [ main] t.j.SpringInitializerDemoApplication : No active profile set, falling back to default profiles: default
2021-03-05 16:39:26.763 INFO 15928 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-03-05 16:39:26.781 INFO 15928 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-03-05 16:39:26.782 INFO 15928 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.43]
2021-03-05 16:39:26.829 INFO 15928 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-03-05 16:39:26.830 INFO 15928 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 825 ms
2021-03-05 16:39:26.931 INFO 15928 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-05 16:39:27.060 INFO 15928 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-05 16:39:27.068 INFO 15928 --- [ main] t.j.SpringInitializerDemoApplication : Started SpringInitializerDemoApplication in 1.561 seconds (JVM running for 2.483)
2021-03-05 16:40:47.192 INFO 15928 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-03-05 16:40:47.192 INFO 15928 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-03-05 16:40:47.193 INFO 15928 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms

image-20210305222504587

在相应的模块中加这个依赖

1
2
3
4
5
6
7
<!--生成META-INF元数据,用于提供IDEA自动提示配置文件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<!--依赖不会传播(继承关系上)-->
<optional>true</optional>
</dependency>

image-20210305222236595

image-20210305223956396

要先启动一次,让 classes 目录生成 META-INF ,IDEA才能自动提示

yml 写法演示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
user:
username: 张三
age: ${random.int[100,200]} #逗号别加空格
birthday: 2001/01/07
hobbies: [睡觉, 学习]
# - 睡觉
# - 学习
score: {Math : 94.0, Chinese : 100.0, English : 94.0}
address:
id: 1
desc: ${user.username}的家在广州
# 它不支持 SPEL,只支持属性占位符

# score:
# 高等数学: 94.0
# 线性代数: 100.0
# 概率论与数理统计: 94.0

热部署

1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

IDEA 配置:

image-20210305235115522

ctrl + shift + alt + / ,选择Registry,勾上 Compiler autoMake allow when app running

指定 webapp 作为编译的位置?