【问题标题】:Spring Framework filter, bean not injectedSpring Framework过滤器,bean未注入
【发布时间】:2011-04-20 07:58:51
【问题描述】:

Servlet Filter 有 2 个条目,一个在 web.xml 中,一个在 Spring applicationContext.xml 中

我将过滤器添加到 applicationContext.xml 中,因为我想将 creditProcessor bean 注入其中。

唯一的问题是web.xml中的条目被JBoss拾取然后使用,所以creditProcessor为空。

我是否必须使用 Spring 的 delegatingFilterProxy 或类似的东西才能将东西注入 bean,或者我可以调整 web.xml?

web.xml:

<filter>
    <filter-name>CreditFilter</filter-name>
    <filter-class>credit.filter.CreditFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>CreditFilter</filter-name>
    <url-pattern>/coverage/*</url-pattern>        
</filter-mapping>

Spring-applicationContext.xml:

<bean id="creditFilter" class="credit.filter.CreditFilter" >
      <property name="creditProcessor" ref="creditProcessor"/>
</bean>

【问题讨论】:

    标签: java spring jboss spring-mvc servlet-filters


    【解决方案1】:

    您不能像这样管理过滤器弹簧。通过您的设置,它由 spring 实例化一次,一次由 servlet 容器实例化。相反,请使用DelegatingFilterProxy:

    1. 在 web.xml 中将过滤器代理声明为 &lt;filter&gt;
    2. 设置过滤器定义的targetBeanNameinit-param 来指定实际处理过滤的bean:

      <init-param>
          <param-name>targetBeanName</param-name>
          <param-value>creditFilter</param-value>
      </init-param>
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-23
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2018-02-17
      相关资源
      最近更新 更多