2010-10-02 69 views
5

有2项为一个Servlet过滤器,一个在web.xml中,一个在春天的applicationContext.xmlSpring框架过滤器,豆不注射

我添加过滤器进入applicationContext.xml的,因为我想注入creditProcessor豆进去。

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

我是否必须使用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> 

弹簧的applicationContext.xml:

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

回答

11

你不能让一个过滤器弹簧这样的管理。使用你的设置,它会在spring中实例化一次,然后由servlet容器实例化一次。相反,使用DelegatingFilterProxy

  1. 在web.xml
  2. 设置过滤器定义的targetBeanName INIT-参数去指定应实际处理过滤豆声明过滤器代理为<filter>

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