Potential resource leak: '<unassigned Closeable value>' may not be closed with SpringApplication.run(...)

Viewed 635

I've configured Eclipse to warn "Potential resource leak".

My Spring Boot main method has this code:

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

Eclipse detects this line as: Potential resource leak: '<unassigned Closeable value>' may not be closed

If I setup this way:

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

Spring Boot starts and ends immediately

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.

How can I solve this issue?

1 Answers

I've solved it this way:

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

}

But I prefer a more elegant way.

Related