2012-03-13 82 views
11

我试图注入一个spring bean到过滤器,但不能使它的工作。春天和@Autowired DelegatingFilterProxy

注入的bean始终为“null”。我成功地在Controllers和HandlerInterceptors中自动装配了这个相同的bean,所以它的注释是正确的。

该过滤器类与其余控制器的基本包相同。

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

的相关部分。这是过滤

@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); 
    } 
} 

的过滤器在每个请求触发的代码。

这些都是在 “Usuario” 豆

@Component 
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS) 
public class Usuario implements java.io.Serializable { ... } 

我失去了什么注解?谢谢!

+0

不该引用'你在'过滤级'中使用你的确切类名吗?你确定你的过滤器是由Spring管理的吗?你不应该在这里使用@Configurable吗? – skegg99 2012-03-13 21:07:17

+0

过滤器的名称是CheckSession,与“过滤器名称”中指示的相同。如果我在“filter-name”中更改该名称,则会从Spring中获取“NoSuchBeanDefinitionException”。所以我想是的,过滤器是由Spring管理的(通过DelegatingFilterProxy),但autowire机制仍然不工作...:( – metacortechs 2012-03-13 21:51:44

回答

9

尽量明确地为您CheckSession的bean定义名称,并看看是否有帮助......像这样:

@Component("CheckSession") 
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); 
    } 
} 

的关键部分是这样的:@Component( “CheckSession”)

而且为了让事情更漂亮,更容易处理,我将camelCase这个名字并将它重新命名为“checkSession”(大写第一个字母)。

+0

这对我有效,我有同样的问题 – codemonkeyww 2013-01-22 17:53:03

+1

这并不工作虽然过滤器被扫描,但自动装置仍然不起作用 – Rahul 2013-12-23 10:47:51

+0

+1在这里工作得很好 – 2014-01-27 20:56:09

7

工程于Spring 3.2.6:

第一:名称您的组件,2个选项 - 只做一

选项A:注释 - 假定组件扫描启用

@Component("checkSession") 
public class CheckSession extends OncePerRequestFilter { 

@Autowired 
Usuario usuario; 

@Override 
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws ServletException, IOException { 

    // not null 
    System.out.println("autowired " + usuario); 
    chain.doFilter(request, response); 
    } 
} 

选项B:通过appliationContext.xml线豆

<bean id="checkSession" class="com.example.filter.CheckSession"></bean> 

二:线簧的DelegatingFilterProxy

<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>/*</url-pattern> 
</filter-mapping> 

注:bean的名称为filter-name

+0

我面临同样的问题,我正在使用spring-boot。如何在spring-boot中配置过滤器,因为我没有web.xml来配置像这样过滤? – kaluva 2016-02-19 18:23:55