【问题标题】:Spring and @Autowired on a DelegatingFilterProxyDelegatingFilterProxy 上的 Spring 和 @Autowired
【发布时间】:2012-03-30 06:00:53
【问题描述】:

我正在尝试将 spring bean 注入过滤器,但无法使其工作。

注入的 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”bean 中的注释

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

我错过了什么?谢谢!

【问题讨论】:

  • 你不应该在'filter-class'中使用你的确切类名吗?你确定你的过滤器是由 Spring 管理的吗?你不应该在这里使用@Configurable 吗?
  • 过滤器名称为CheckSession,与“filter-name”中的相同。如果我在“filter-name”中更改该名称,我会从 Spring 中得到一个“NoSuchBeanDefinitionException”。所以我想是的,过滤器由 Spring 管理(通过 DelegatingFilterProxy),但自动装配机制仍然无法正常工作...... :(

标签: spring autowired


【解决方案1】:

尝试为您的 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”(不大写第一个字母)。

【讨论】:

  • 这根本不起作用。虽然过滤器被扫描,但自动连线仍然不起作用
  • @Rahul 仅当您使用 org.springframework.web.filter.DelegatingFilterProxy 时才有效
【解决方案2】:

适用于 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

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

第二:连接Spring 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

【讨论】:

  • 我面临同样的问题。我正在使用弹簧靴。由于我没有 web.xml 来配置这样的过滤器,如何在 spring-boot 中配置过滤器?
猜你喜欢
  • 2012-06-15
  • 2014-12-23
  • 2012-06-23
  • 1970-01-01
  • 1970-01-01
  • 2020-05-22
  • 2011-10-07
  • 1970-01-01
  • 2015-12-22
相关资源
最近更新 更多