[英]Spring boot automatically restarts when doing file IO from intelliJ
I have a very strange issue where my spring application will end all threads and restart when I programmatically save a file to the resources folder. 我有一个非常奇怪的问题,当我以编程方式将文件保存到资源文件夹时,spring应用程序将结束所有线程并重新启动。 What's odd is that the problem seems to disappear if I package to war and deploy on a tomcat server, yet when I run it from IntelliJ the problem is there. 奇怪的是,如果我打包打包并在tomcat服务器上部署,问题似乎消失了,但是当我从IntelliJ运行它时,问题就在那里了。 What's more is that I am wanting this file write to happen right after Spring starts, leading to this infinite loop of the application starting and restarting. 更重要的是,我希望在Spring启动后立即进行此文件写入,从而导致应用程序启动和重启的无限循环。 I check in the resources folder within the build folder and see that the file is getting save each time spring starts, but the application seems to crash right afterwards. 我检查了build文件夹中的resources文件夹,发现每次spring启动时该文件都得到保存,但是此后该应用程序似乎崩溃了。
Here is the code I'm using to write the file: 这是我用来写文件的代码:
FileOutputStream fos = new FileOutputStream(
this.getClass().getClassLoader().getResource("processes/").getPath() + "/filename.xml"
);
fos.write(processXML);
fos.close();
That is not strange at all. 那一点也不奇怪。
You are having in your classpath dependency: 您在类路径依赖项中:
org.springframework.boot:spring-boot-devtools
You are writing to the location that is being scanned for changes by spring boot reload component - which results in infinite loop as you said. 您正在写入Spring Boot重新加载组件正在扫描的位置更改-如您所说,这将导致无限循环。
Set in your application.properties: 在您的application.properties中设置:
spring.devtools.restart.enabled = false
More on that here: 这里的更多信息:
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 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
Just add two things first devtools dependency in pom.xml and property in yml file. 只需在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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.