在过滤器类中获取Autowired服务

时间:2015-05-31 06:52:34

标签: java spring-mvc filter autowired

出现错误:

java.lang.IllegalStateException: 
No WebApplicationContext found: no ContextLoaderListener registeredatorg.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:252)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)

我的web.xml配置声明为DelegatingFilterProxy。如果有不同的身份验证规则,我如何在我的过滤器中写入?通过那些差异名称?

public class SecurityFilter implements Filter{
@Autowired
private TokenService tokenService;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
    String token = request.getParameter("token");//:TODO}}

和spring config是:

<bean id="securityFilter" class="xx.filter.SecurityFilter"></bean>

1 个答案:

答案 0 :(得分:0)

filter类不是spring anootated类,因此不会加载任何依赖spring相关的类。理想情况下,如果您将UserFilter注释为弹簧组件,那么您的@autowired dependents也将加载。