Spring - 过滤器无法访问Bean

时间:2014-12-25 10:33:38

标签: java spring spring-mvc filter

我正在开发spring-mvc应用程序。

我无法访问过滤器中的bean。我得到了以下异常

 org.springframework.beans.factory.NoSuchBeanDefinitionException : No qualifying bean of type [com.abc.app.SessionValue] is defined

我去了https://stackoverflow.com/a/11709272/3898076,但无法找到问题。

我的web.xml中有以下条目

<servlet>
    <servlet-name>myapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring_xyz-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

spring_xyz-servlet.xml包含组件扫描条目。

<context:component-scan base-package="com.abc.app" />
<context:annotation-config />
<context:spring-configured />

过滤代码:

WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(fConfig.getServletContext());
springContext.getBean(SessionValue.class);

这有什么配置问题吗?

感谢。

1 个答案:

答案 0 :(得分:1)

如果没有其他约束,您应该尝试使用Spring MVC Handler Interceptor,因为您处于Spring上下文中,并且处理程序几乎与Filters类似。

http://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/HandlerInterceptor.html

示例:

http://www.journaldev.com/2676/spring-mvc-interceptors-example-handlerinterceptor-and-handlerinterceptoradapter