如何从过滤器中的servlet上下文中获取bean

时间:2017-04-21 05:52:21

标签: java spring servlets

这是我的web.xml

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/spring/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

我的自定义过滤器就像这样。


protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
                throws ServletException, IOException {
    WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    //wac is root context, not dispatcherServlet's context   
}

我想获得dispatcherServlet的上下文(servlet-context.xml)。

请帮帮我

1 个答案:

答案 0 :(得分:0)

我没有测试,但你可以试试这个 签入春季文档

DispatcherServlet ds = new DispatcherServlet(request.getServletContext())
相关问题