潜在的资源泄漏:“ <未分配的可关闭值>”可能无法通过SpringApplication.run(...)关闭

时间:2020-06-25 13:00:13

标签: eclipse spring-boot memory-leaks

我已将Eclipse配置为警告“潜在的资源泄漏”。

我的Spring Boot主要方法具有以下代码:

public static void main(String[] args) {
    SpringApplication.run(App.class, args);
}

Eclipse将以下行检测为:Potential resource leak: '<unassigned Closeable value>' may not be closed

如果我这样设置:

public static void main(String[] args) {
    try(ConfigurableApplicationContext context = SpringApplication.run(App.class, args)){
        
    }
}

Spring Boot开始并立即结束

2020-06-25 14:02:28.336  INFO 9108 --- [main] demo.App                      : Started App in 50.426 seconds (JVM running for 51.605)
2020-06-25 14:02:28.403  INFO 9108 --- [main] org.mongodb.driver.connection : Closed connection [connectionId{localValue:2, serverValue:207}] to localhost:27017 because the pool has been closed.

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我已经这样解决了:

@SpringBootApplication
public class App implements Closeable {

    private static ConfigurableApplicationContext run;

    public static void main(String[] args) {
        run = SpringApplication.run(App.class, args);
    }

    @Override
    public void close() throws IOException {
        run.close();
    }

}

但是我更喜欢一种优雅的方式。