Can spring boot be told to start an application only if a resource is available?

427 views Asked by At

I am part of a team developing a spring boot backend application, which provides mobile applications with data. Along with a new feature the need of loading a resource at the backend, which need to be provided to the mobile application arose.

Due to the fact that this resource is important and making the access to the resource effective by loading it once during startup, i wondered if i can tell spring boot to not start without this resource existing in the file system. I know about the annotation @PostConstruct, but loading the resource there seems to be too late.

Thanks in advance for suggtestions

2

There are 2 answers

1
minion On BEST ANSWER

@PostConstruct can be used to make sure application has the stuff that you need when it starts like DB pooling etc. For example, you can throw IllegalStateException() if the file is not there and it can stop the application from loading. I ran a quick test and it works. You can use them as below.

@PostConstruct
    public void setup() {
        // check the stuff that you need.
        if (condition) {                
            throw new IllegalStateException();
        }
    }
0
bhdrkn On

You can do that just before starting your application.

@SpringBootApplication
public class SpringBootTestApplication {

    public static void main(String[] args) {
        // TODO Do your checking here and exit if you like
        SpringApplication.run(SpringBootTestApplication, args);
    }
}