【问题标题】:Servlet Web Application Context from filter来自过滤器的 Servlet Web 应用程序上下文
【发布时间】:2015-07-10 22:53:59
【问题描述】:

我尝试从过滤器中获取 Spring Web 应用程序上下文。

我之前已经通过了:

WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext())

没关系,因为我得到了所谓的我认为 ROOT Web 应用程序上下文。 现在,我想在 web.xml 中移动我所有的 spring 定义:

    <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext instead of default XmlWebApplicationContext. --> 
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <!-- Custom web configuration annotated class. -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>foo.bar.WebConfiguration</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

我从 web.xml 中删除了“旧”定义:

<!--    <context-param> -->
<!--        <param-name>contextConfigLocation</param-name> -->
<!--        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> -->
<!--    </context-param> -->
<!--    <listener> -->
<!--        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> -->
<!--    </listener> -->

现在,我没有更多的“ROOT”Web 应用程序上下文,过滤器无法获取另一个 Web 应用程序上下文。

我看到还有一个电话:

WebApplicationContextUtils.getWebApplicationContext(sc, attrName)

它允许将“属性”指定为第二个参数,它实际上是您想要的Web应用程序上下文。默认情况下,它是“org.springframework.web.context.WebApplicationContext.ROOT”。

我想我必须调用这个方法,但我不知道该放什么作为第二个参数。

PS:我也尝试使用org.springframework.web.filter.DelegatingFilterProxy,但是tomcat抛出了他没有找到经典(ROOT)web应用上下文的错误:

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?

但还有另一个上下文,它只是以另一种方式定义。不可能只有我一个人试图获得 ROOT 的另一个上下文。

感谢您到目前为止阅读。

【问题讨论】:

  • 如果你有多个 dispatcherservlet 怎么办?然后获取哪个上下文(DelegatingFilterProxy 仅在根上下文上运行是有原因的,因为每个上下文可能有 0..n 个DispatcherServlets)。反过来(只有一个 ROOT 上下文)比尝试获取另一个上下文更容易。但是,如果您真的想要名称是 org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher,但是正如提到的,仅使用根上下文更容易、更自然...
  • 谢谢,我会尽快尝试。我会很高兴我的上下文是根上下文。但我感觉通过DispatcherServlet 而不是旧的ContextLoaderListener 定义我的上下文使我没有ROOT 上下文。也许有一种方法可以让我的上下文DispatcherServlet 成为根上下文。你认为这可能吗? (我今晚会找它)谢谢。
  • 好的,我确认您的解决方案有效,非常感谢!但是我仍然对你所说的感到不安,我应该使用 ROOT 上下文。所以我觉得我的上下文是以错误的方式声明的,它应该是 ROOT 的,但它不是。我会尝试使它成为 ROOT 的。
  • 根上下文是ContextLoaderListener加载的,不是DispatcherServlets加载的。

标签: spring tomcat


【解决方案1】:

我刚刚阅读了您的答案,是的,我昨天晚上就知道了;-) 我添加了 ROOT 上下文,这样定义它:

    <context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>foo.bar.WebConfiguration</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

过滤器可以通过自动装配直接访问服务。

我仍然觉得我的架构很脏,因为 ROOT 和“调度程序”上下文是相同的,我认为这不是一个好习惯。 也许我稍后会尝试将“WebConfiguration”一分为二。

还是谢谢你!

【讨论】:

    猜你喜欢
    • 2020-09-25
    • 2012-10-10
    • 1970-01-01
    • 2023-03-20
    • 2018-04-14
    • 2016-11-17
    • 2018-10-28
    • 2013-11-06
    • 1970-01-01
    相关资源
    最近更新 更多