【问题标题】:How to prevent spring context loading on exception in ServletContextListener?如何防止在ServletContextListener中的异常上加载弹簧上下文?
【发布时间】:2017-03-23 05:34:37
【问题描述】:

我使用ContextLoaderListener 提升弹簧上下文。 在此之前 web.xml 我有我的自定义侦听器,它执行一些检查。如果这些检查失败,我不希望启动 Spring 上下文。

如果我在另一个侦听器中有异常,我如何防止 Spring 上下文加载?

我会在 ServletContext 中添加一些属性,但我不知道它会如何影响 spring ctx 的加载。

这是一个非常相似的问题:Stop deployment when Exception occurs in ServletContextListener

【问题讨论】:

    标签: spring spring-mvc servlet-listeners spring-ioc


    【解决方案1】:

    这是我为解决这个问题所做的:

    在我执行一些检查的侦听器中,如果发生错误,我将属性设置为 servlet 上下文

    @Override
    public void contextInitialized(ServletContextEvent sce) {
    sce.getServletContext().setAttribute("checkFailed", true);
    }
    

    然后我扩展了 Spring 的 ContextLoaderListener 并像这样覆盖了 contextInitialized

    @Override
    public void contextInitialized(ServletContextEvent event) {
        if (event.getServletContext().getAttribute("checkFailed") != null) {
            return;
        }
        super.contextInitialized(event);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-01-10
      • 2021-08-30
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      • 2015-10-22
      • 1970-01-01
      • 2016-09-02
      • 1970-01-01
      相关资源
      最近更新 更多