Spring 来自筛选器的Servlet Web应用程序上下文

Spring 来自筛选器的Servlet Web应用程序上下文,spring,tomcat,Spring,Tomcat,我试图从过滤器中获取SpringWeb应用程序上下文 我之前是通过: WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()) 还好,因为我得到了所谓的根web应用程序上下文。 现在,我想把我所有的spring定义都移动到web.xml中: <servlet> <servlet-name>dispatcher</servlet-n

我试图从过滤器中获取SpringWeb应用程序上下文

我之前是通过:

WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext())
还好,因为我得到了所谓的根web应用程序上下文。 现在,我想把我所有的spring定义都移动到web.xml中:

    <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>
<!--    <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> -->
它允许指定“attribute”作为第二个参数,它实际上是您想要的web应用程序上下文。默认情况下,它是“org.springframework.web.context.WebApplicationContext.ROOT”

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

PS:我还尝试使用org.springframework.web.filter.DelegatingFilterProxy,但tomcat抛出了一个错误,他没有找到经典(根)web应用程序上下文:

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
但还有另一个上下文,它只是用另一种方式定义的。不可能我是唯一一个试图获取另一个根的上下文的人


谢谢你读了这么多。

我刚刚读了你的答案,是的,我昨天晚上就猜出来了;-) 我添加了根上下文,定义如下:

    <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>

上下文类
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
上下文配置位置
foo.bar.WebConfiguration
org.springframework.web.context.ContextLoaderListener
通过自动接线,过滤器可以直接接入服务

我仍然觉得我的架构很脏,因为根和“dispatcher”上下文是相同的,我认为这不是一个好的实践。 也许我以后会尝试将“网络配置”一分为二


无论如何谢谢你

如果您有多个DispatcherServlet呢?然后获取哪个上下文(有一个原因a
DelegatingFilterProxy
只在根上下文上运行,因为每个上下文可能有0..n
DispatcherServlet
s)。另一种方法(仅根上下文)比尝试获取其他上下文更容易。但是,如果您真的想要名称为
org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher
,但是正如前面提到的那样,使用根上下文更简单、更自然……谢谢,我会尽快尝试。我很高兴我的背景是最基本的。但我觉得通过
DispatcherServlet
而不是旧的
ContextLoaderListener
定义上下文会使我没有根上下文。也许有一种方法可以让我的context
DispatcherServlet
成为根目录。你认为这可能吗?(我今晚去找)谢谢。好的,我确认你的解决方案有效,非常感谢!但是我仍然对你说的话感到不安,我应该使用根上下文。所以我觉得我的上下文是以错误的方式声明的,它应该是根,但不是。。我将尝试将其设置为根上下文。根上下文是由
ContextLoaderListener
加载的上下文,而不是由
DispatcherServlet
s加载的上下文。
    <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>