spring boot - Exiting a SpringBoot app - how do I get a reference to SpringApplication? -


i realize programmatically exit springboot 4 application want call exit() method of springapplication, how can reference it?

of course have access in main() method, ask because if i'm in class loading resource , fails, want terminate app, class can't figure out how access springapplication.

thanks...

a cleaner approach use case use events & listeners wherein have add listener springapplication class listens event in case resource load failure , subsequently act accordingly i.e. exit application. can application context handle implementing applicationcontextaware interface. details on event & listener can found here.

myevent class :-

public class myevent extends contextrefreshedevent {      private static final long serialversionuid = 1l;      @autowired     public myevent(applicationcontext source) {         super(source);     }  } 

myevent listener class :-

@component public class mylistener implements applicationlistener<contextrefreshedevent> {      @override     public void onapplicationevent(contextrefreshedevent event) {                if(event instanceof myevent){             springapplication.exit(event.getapplicationcontext(), new exitcodegenerator() {                 @override                 public int getexitcode() {                     return 2;                 }             });         }     }  } 

resource loader class:-

@component public class myresourceloader implements applicationcontextaware, commandlinerunner {      private applicationcontext ctx ;          @autowired     private applicationeventpublisher publisher;      @override     public void run(string... args) throws exception {         //inside run resource load failure         publisher.publishevent(new myevent(ctx));            }      @override     public void setapplicationcontext(applicationcontext applicationcontext)             throws beansexception {         ctx = applicationcontext;     }  } 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -