【问题标题】:Spring boot automatically restarts when doing file IO from intelliJ从 intelliJ 进行文件 IO 时 Spring Boot 会自动重启
【发布时间】:2018-02-10 22:19:03
【问题描述】:

我有一个非常奇怪的问题,当我以编程方式将文件保存到资源文件夹时,我的 spring 应用程序将结束所有线程并重新启动。奇怪的是,如果我打包到战争并部署在 tomcat 服务器上,问题似乎消失了,但是当我从 IntelliJ 运行它时,问题就在那里。更重要的是,我希望在 Spring 启动后立即写入此文件,从而导致应用程序启动和重新启动的无限循环。我检查了 build 文件夹中的资源文件夹,发现每次 spring 启动时文件都在保存,但之后应用程序似乎立即崩溃。

这是我用来编写文件的代码:

FileOutputStream fos = new FileOutputStream(
        this.getClass().getClassLoader().getResource("processes/").getPath() + "/filename.xml"
);
fos.write(processXML);
fos.close();

【问题讨论】:

  • 您是否也尝试将文件保存到其他位置?在任何情况下,一旦您打包并执行 Spring Boot 应用程序,该目录将不可用(默认情况下,除非您重新创建它)。
  • 您会在重新部署时丢失这些文件 - 如果这些是临时文件,可以写入临时目录。持久性数据(应该在重新部署后仍然存在)通常位于“/var/...”下。
  • 很高兴知道,但在这种情况下临时文件会没问题

标签: java spring spring-mvc intellij-idea


【解决方案1】:

这并不奇怪。

您的类路径依赖项中有:

org.springframework.boot:spring-boot-devtools

您正在写入 Spring Boot 重新加载组件正在扫描更改的位置 - 如您所说,这会导致无限循环。

在你的 application.properties 中设置:

spring.devtools.restart.enabled = false

更多信息在这里:

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

【讨论】:

    【解决方案2】:

    只需在 pom.xml 中添加两件事,首先是 devtools 依赖项,然后在 yml 文件中添加属性。

    #for not restarting the server every time
    spring.devtools.restart.enabled:  false
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <optional>true</optional>
            </dependency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 2016-01-25
      • 2017-02-05
      • 2018-03-27
      • 2021-01-29
      • 2021-09-07
      • 2017-01-31
      相关资源
      最近更新 更多