开发小技巧
开发小技巧
Lombok
引入依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
然后 IDEA 配置启用 Lombok 插件
具体用法,在《Lombok 常用注解.md》中已经学习过了
dev-tools
[官方文档]:Developing with Spring Boot
添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
热更新,不推荐使用
其实这个热更新说白了就是自动重启,如果你想要热加载,那推荐使用 JRebel
。
Restart vs Reload
The restart technology provided by Spring Boot works by using two classloaders. Classes that do not change (for example, those from third-party jars) are loaded into a base classloader. Classes that you are actively developing are loaded into a restart classloader. When the application is restarted, the restart classloader is thrown away and a new one is created. This approach means that application restarts are typically much faster than “cold starts”, since the base classloader is already available and populated.
If you find that restarts are not quick enough for your applications or you encounter classloading issues, you could consider reloading technologies such as JRebel from ZeroTurnaround. These work by rewriting classes as they are loaded to make them more amenable to reloading.
之前听说使用 dev-tools
可能会导致类加载的问题,因为使用的是不同的类加载器,所以一般我们不推荐使用 JRebel
。
JRebel 的使用指南,请看《IDEA+JRebel.docx》
其他功能
还有其他功能,用的不多。有需要再去看
Spring Initailizr(项目初始化向导)
实际上就是将我们之前在《SpringBoot 基础篇 -2- 自动配置.md》中总结的 使用SpringBoot的最佳实践
图形化流程化了。
结合 IDEA 来用 Spring Initailizr 搭建一个常规的用于 CRUD 的 Spring Boot 项目


非常简单。
生成的项目结构如下

从 SpringBoot 3.0 开始,最低要求 Java17,因此如果在这里选择了 Spring 3.0 以上的版本,创建了模块之后,再在 pom 中把 jdk 版本改回 1.8 是行不通的,会报错,如果仍然使用 jdk8,那么这里就选择 SpringBoot 2.0 的版本,希望注意。