0

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

pekayde
  • 118
  • 10
  • What kind of resource are we talking about? DB or files? – minion Jun 10 '15 at 13:30
  • Excuse me, i am talking about a text file – pekayde Jun 10 '15 at 13:45
  • Well, if `@postConstruct` seems to be late(not sure why). You can implement a ServletContextListener and hook into the callback functions to notify or do whatever you want. Curious as why postConsturct is late for you? – minion Jun 10 '15 at 13:56
  • @minion i want to stop the start-up process of the application if the file is missing. If i understood the documentation of postConstruct correctly, the application will keep running anyway and i have to handle the state of the bean which couldn't load the file somehow – pekayde Jun 10 '15 at 14:33
  • Look at my answer, I guess it may be of help. – minion Jun 10 '15 at 17:56

2 Answers2

3

@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();
        }
    }
minion
  • 4,313
  • 2
  • 20
  • 35
1

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);
    }
}
bhdrkn
  • 6,244
  • 5
  • 35
  • 42