Val*_*jon 5 eclipse memory-leaks spring-boot
我已将 Eclipse 配置为警告“潜在资源泄漏”。
我的 Spring Boot main 方法有以下代码:
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
Run Code Online (Sandbox Code Playgroud)
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)){
}
}
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
我是这样解决的:
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
但我更喜欢更优雅的方式。
| 归档时间: |
|
| 查看次数: |
954 次 |
| 最近记录: |