【问题标题】:Potential resource leak: '<unassigned Closeable value>' may not be closed with SpringApplication.run(...)潜在的资源泄漏:'<unassigned Closeable value>' 可能无法使用 SpringApplication.run(...) 关闭
【发布时间】:2020-06-25 13:00:13
【问题描述】:

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

我的 Spring Boot main 方法有这样的代码:

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

Eclipse 将此行检测为:Potential resource leak: '&lt;unassigned Closeable value&gt;' 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.

我该如何解决这个问题?

【问题讨论】:

    标签: eclipse spring-boot memory-leaks


    【解决方案1】:

    我是这样解决的:

    @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();
        }
    
    }
    

    但我更喜欢更优雅的方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      相关资源
      最近更新 更多