SpringBoot学习二 Developer tools的使用

SpringBoot提供的Developer tool有很多的功能,其主要是自启动和热部署

1.增加devtools的依赖包

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

2.主要的特点

1.默认属性

为了提高效率,springboot的很多jar包支持缓存,虽然高速缓存很有用,但是对于开发来说,会阻止我们看到我们程序更改的结果,而spring-boot-devtools默认情况下是阻止其使用缓存的。

2.在IDEA下的自动重启

除了在pom中添加上面的tools jar包外,还要设置IDEA中的两个地方

a.设置IDEA自动编译

b.Ctrl + Shift + A查找registry命令

勾选compiler.automake.allow.when.app.running选项


3.排除不需要重启的目录

默认情况下,在/META-INF/maven,/META-INF/resources ,/resources ,/static ,/public or /templates下的资源被修改后,只会重新加载,而不会重启,如果想排除一些默认的设置,可以在application.properties中添加想要排除的项,例如:

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

现在在static和public 下的文件将不会导致重启

如果你想保持默认的基础增加排除,可以使用spring.devtools.restart.additional-exclude

如果想要非classpath下的文件,更改能够重启,那么使用spring.devtools.restart.additional-paths

4.更改资源后不想重启

如果不想被重启,在application.properties中spring.devtools.restart.enabled为false,这种方式下,重启的类加载仍然是初始化的,但是不会监控资源的变化

如果想彻底禁止重启,需要在SpringApplication.run(…​)之前加入System属性,如下所示:

public static void main(String[] args) {
    System.setProperty("spring.devtools.restart.enabled", "false");
    SpringApplication.run(MyApp.class, args);
}

5.类加载的设置

springboot在重启的时候,有两个类加载来工作,在IDEA中的项目中,打开的项目都使用restart类加载器和有规律的jar使用base类加载器.

spring-devtools.properties文件提供了restart.exclude 和restart.include属性,restart.exclude表示把restart加载器中推入到基类加载器中,restart.include表示拉入到重启类加载器中.支持正则匹配

在META-INF/spring-devtools.properties文件中

restart.exclude.companycommonlibs=/mycorp-common-[\\w-]+\.jar
restart.include.projectcommon=/mycorp-myproj-[\\w-]+\.jar

6.在线加载

springboot中的tools支持在线加载,当有资源改变时,会触发浏览器的刷新,如果想禁止此功能,设置spring.devtools.livereload.enabled
为false

7 全局设置

8远程引用

具体参考

https://docs.spring.io/spring-boot/docs/2.0.0.M3/reference/htmlsingle/#using-boot-devtools-globalsettings

猜你喜欢

转载自blog.csdn.net/strong_yu/article/details/77563446