I'm trying to inject a spring bean into a filter, but can't make it work.
The bean injected is always "null". I succeed autowiring this same bean in Controllers and HandlerInterceptors so it's correctly annotated.
The filter class is under the same base-package of the rest of Controllers.
This is the relevant part of my web.xml
<filter>
<filter-name>CheckSession</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>CheckSession</filter-name>
<url-pattern>/panel/*</url-pattern>
</filter-mapping>
This is the code for the filter
@Component
public class CheckSession extends OncePerRequestFilter implements Filter {
@Autowired private Usuario usuario;
@Override
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response, FilterChain chain)
throws ServletException, IOException {
// always null
System.out.println("autowired " + usuario);
chain.doFilter(request, response);
}
}
The filter is triggering on every request.
These are the annotations in the "Usuario" bean
@Component
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class Usuario implements java.io.Serializable { ... }
What am i missing? Thanks!