Spring Boot学习笔记-1

本文介绍了SpringBoot热部署原理及配置方法,通过使用spring-boot-devtools模块实现代码修改后的自动重启,提高开发效率。文章详细解释了双类加载器机制,并提供了如何排除特定资源文件以避免不必要的重启。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Boot 学习笔记

本篇文章有以下知识点

  • Developer tools

Developer tools

应用程序如果使用了spring-boot-devtools,当classpath下面的文件发生改变时就会重启。

Maven 添加

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

自动重启(Automatic restart)

Restart vs Reload

The restart technology provided by Spring Boot works by using two classloaders. Classes that don’t change (for example, those from third-party jars) are loaded into a base classloader. Classes that you’re 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.

意思是说Spring Boot 用了2个类加载器来加载程序中的class文件,那些固定不变的类(第三方类)用一个类加载器,程序本身的类用另一个类加载器(restart classloader)。当Spring Boot检测到改变时就用restart类加载器来启动,相比冷启动来说速度要快很多。

排除加载一些文件(Excluding resources)

像一些资源文件,比如,/META-INF/maven, /META-INF/resources ,/resources ,/static ,/public or /templates就不需要出发程序重启,所以需要排除。在application.properties文件中添加

spring.devtools.restart.exclude=static/**,public/**

在生产环境中这些文件默认会缓存起来,但是在开发环境中不需要缓存,在application.properties文件中添加

    spring.thymeleaf.cache=false

spring-boot-devtools 默认为false


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值