【问题标题】:How can i get beans from servlet context in filter我如何从过滤器中的 servlet 上下文中获取 bean
【发布时间】:2017-04-21 14:20:29
【问题描述】:

这是我的 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)。

请帮帮我

【问题讨论】:

标签: java spring servlets


【解决方案1】:

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

DispatcherServlet ds = new DispatcherServlet(request.getServletContext())

【讨论】:

    猜你喜欢
    • 2011-12-14
    • 1970-01-01
    • 2013-09-05
    • 2011-12-10
    • 2019-12-19
    • 2015-05-16
    • 2012-05-24
    • 1970-01-01
    • 2016-11-09
    相关资源
    最近更新 更多