Reputation: 301
I have a question while developing my own components depending on autoconfiguration, and there are required critical parameters.
So I have a checking method in my component which annotated with @PostConstruct
If the configuration was wrong, I want this method to print the error log and shutdown application immediately just like if I set something wrong in springboot's basic configuration.
But the fact is, while using SpringApplication.exit(applicationContext)
to shut down the application ,the application will remain running and stuck in somewhere and will not shut down.
the exception strack trace is shown below:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-07-15 17:37:28.623 [app:application-center,traceId:,spanId:,parentId:] [main] ERROR | SpringApplication.java:837 | o.s.boot.SpringApplication | Application run failed
java.lang.IllegalStateException: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@320494b6 has been closed already
at org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:1093)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108)
at org.springframework.cloud.context.scope.refresh.RefreshScope.eagerlyInitialize(RefreshScope.java:127)
at org.springframework.cloud.context.scope.refresh.RefreshScope.start(RefreshScope.java:118)
at org.springframework.cloud.context.scope.refresh.RefreshScope.onApplicationEvent(RefreshScope.java:112)
at org.springframework.cloud.context.scope.refresh.RefreshScope.onApplicationEvent(RefreshScope.java:66)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:361)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:898)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at com.myapp.applicationcenter.Application.main(Application.java:24)
Upvotes: 0
Views: 639
Reputation: 301
Well,after injection
@Resource
private ApplicationContext applicationContext
using ((ConfigurableApplicationContext)applicationContext).close()
in a try catch block, shutting down the application after catching exceptions will solve the problem.
Are there more graceful ways to do so?
Upvotes: 1