Ela*_*bak 5 java spring tomcat
有时在开发过程中,某些东西被破坏,导致 spring 上下文加载失败。问题是有时错误只是在某些 bean 中,但 webapp 的其余部分正在部分加载,然后你会得到一些奇怪的行为。
有没有一种已知的方法可以让 Spring 停止服务器进程,以防发生不好的事情?就像一些 bean 注入失败,或者一些 NPE 发生在一些 PostConstruct 或其他东西。
类似于 web.xml 中的 stopOnError=true。
So eventually the solution I found is: Create a class that implements ServletContextListener, call it ApplicationLoaderListener.
Set this class in web.xml:
<listener>
<listener-class>com.my.package.ApplicationLoaderListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
Add a private member to it:
private final ServletContextListener loader = new ContextLoaderListener();
Run Code Online (Sandbox Code Playgroud)
This class must implement the two interface methods, which the relevant one is contextInizialized:
@Override
public void contextInitialized(ServletContextEvent sce) {
try {
loader.contextInitialized(sce);
} catch (BeanCreationException e) {
handle(e);
}
}
Run Code Online (Sandbox Code Playgroud)
And the implementation of handle():
private void handle(BeanCreationException e) {
log.error("=============== FATAL =============== - failed to create bean: ", e);
System.exit(1);
}
Run Code Online (Sandbox Code Playgroud)
And to make the code complete, the second method:
@Override
public void contextDestroyed(ServletContextEvent sce) {
loader.contextDestroyed(sce);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3808 次 |
| 最近记录: |