springboot-devtools idea或eclipse 热加载

大家好,我是烤鸭:

    今天分享一下springboot项目的热加载。

    第二种方式在eclipse和idea中都可以。虽然会有一些小坑。

方式有两种:

1.   springloaded(无效)

<!-- https://mvnrepository.com/artifact/org.springframework/springloaded -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>springloaded</artifactId>
    <version>1.2.8.RELEASE</version>
    <scope>provided</scope>
</dependency>

2.   springboot-devtools(推荐)

    首先看一下官网,简单通俗。

    https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html

    2.1    pom文件

<!-- 热加载 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<version>2.0.0.RELEASE</version>
			<optional>true</optional>
			<scope>true</scope>
		</dependency>
            <plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<!-- fork: 如果没有配置该项配置,devtools不会起作用的,即应用不会restear -->
					<fork>ture</fork>
				</configuration>
			</plugin>
            </plugins>

    2.2    异常情况说明

        2.2.1    类型转换异常:

        https://blog.csdn.net/m0_38043362/article/details/78064539

        官网的解决方式:

        

        按这个思路,首先在src/main/resources 目录下建立META-INF文件夹,创建spring-devtools.properties

        内容:

restart.include.mapper=/mapper-[\\w-\\.]+jar
restart.include.pagehelper=/pagehelper-[\\w-\\.]+jar

最开始给我报的是类型转换异常,因为用的是shiro,所以我在properties中加入了shiro的包:

restart.include.shiro=/shiro-[\\w-\\.]+jar

后来又报thymeleaf的异常,导致项目无法启动,加入了thymeleaf的包

restart.include.thymeleaf=/thymeleaf-[\\w-\\.]+jar

再后来...

总之是哪个包报错了,就把响应的pom文件中引的这个包,在这个properties中配置一下。

最后:

spring-devtools.properties :

restart.include.mapper=/mapper-[\\w-\\.]+jar
restart.include.pagehelper=/pagehelper-[\\w-\\.]+jar
# 因为我项目中引用了 org.crazycake:shiro-redis ,所以要引用下面这个配置
restart.include.shiro=/shiro-[\\w-\\.]+jar
restart.include.thymeleaf=/thymeleaf-[\\w-\\.]+jar
restart.include.spring=/spring-[\\w-\\.]+jar
restart.include.mybatis=/mybatis-[\\w-\\.]+jar
restart.include.springframework=/org.springframework-[\\w-\\.]+jar
restart.include.springfox=/springfox-[\\w-\\.]+jar

    2.2.2    小结

    官网的意思是任何jar包都可以被"基本的"类加载器加载。如果你的项目是多模块的,不是每个模块都可以导入到你的IDE中。

    所以需要自定义。重点是这两句。

    The include elements are items that should be pulled up into the “restart” classloader, and the exclude elements are items that should be pushed down into the “base” classloader.

        include的包含的jar包被放入了“重启”的类加载器,而exclude的包含的jar包被推入了“基本”类加载器。

All property keys must be unique.    定义的Key值必须唯一。

          我个人的理解就是需要被他的类加载器加载的就不定义,如果有自己的类需要自己的类加载器,比如shiro等等,就需要定义在这个propertiesdevtools负责的是对基本类加载器中的类的加载。

3.    IDE中使用

    3.1    eclipse

    

        打开自动编译以后,每次编译时间长短不一样。多修改保存几次就好了。如果还不行,就取消自动编译再打钩

    3.2    idea

File——>settings——>Compiler——>Build project automatically

       

快捷键:CTRl+ALT+SHIFT+/    ——>  Registry


如果这些还是无效的话,可以试试手动编译。右键如下图。快捷键Ctrl+Shift+F9


4.    特别说明

     之前出现过:

     1.    eclipse需要多次保存class文件才能生效。

     2.    在idea上出现必须手动编译才能生效。

     3.    静态资源(js.css)或者模板文件(ftl,thymeleaf)没有reload。

     原因猜想:

        之前安装过JRebel(热加载插件,需要收费),虽然卸载了,不知道有没有影响。

        电脑的原因,换一台性能好点的电脑,上述问题就没有了。

        有类似问题的,欢迎交流。

     

猜你喜欢

转载自blog.csdn.net/angry_mills/article/details/80492068