Springboot 在 idea 中 devtools 热部署不生效

开发中,每次对类的修改,都需要重启服务,很浪费时间,影响效率。下面介绍一种springboot热部署的方法。

    1、在Maven的pom.xml文件中添加依赖

    <!-- 热部署 www.1b23.com -->

    <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-devtools</artifactId>

      <!-- optional=true,依赖不会往下传递,如果有项目依赖本项目,并且想要使用devtools,需要重新引入 -->

      <optional>true</optional>

      <scope>true</scope>

    </dependency>

2、继续在Maven的pom.xml文件中添加插件的配置

<build>

    <plugins>

      <plugin>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-maven-plugin</artifactId>

        <configuration>

          <fork>true</fork><!-- 如果没有该配置,热部署的devtools不生效 -->

        </configuration>

      </plugin>

      <!-- 自定义配置spring Boot使用的JDK版本 -->

      <plugin>

        <artifactId>maven-compiler-plugin</artifactId>

        <configuration>

          <source>1.8</source>

          <target>1.8</target>

        </configuration>

      </plugin>

    </plugins>

</build>

   如果是Eclipse,配置到这里,只要重启服务,热部署就会生效了。

    但是IDEA的话,热部署还不会生效,因为devTools只会在类路径上的文件发生更改时才会自动重启,而IDEA默认不会自动编译。

    解决方法有两种:

    1、手动:修改完代码,按快捷键Ctrl+F9,手动构建项目,或者只修改单个类文件的话,按Ctrl+Shift+F9,重新编译该类文件,即可触发重启服务。

    2、自动:

        1)File -> Settings -> Compiler,勾选 Build Project automatically

         2)按快捷键Ctrl+Shift+Alt+/,选择1.Registry...

         3)勾选 compiler.automake.allow.when.app.running 即可


猜你喜欢

转载自blog.51cto.com/14622073/2460910