Spring Framework过滤器,未注入Bean [英] Spring Framework filter, bean not injected

查看:107
本文介绍了Spring Framework过滤器,未注入Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Servlet过滤器有2个条目,一个在web.xml中,一个在Spring applicationContext.xml中

There are 2 entries for a Servlet Filter, one in web.xml and one in Spring applicationContext.xml

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

I added the filter into applicationContext.xml because I wanted to inject creditProcessor bean into it.

唯一的问题是web.xml中的条目被JBoss选中然后使用,因此creditProcessor为空.

The only problem is that the entry in web.xml got picked up by JBoss and then used, so creditProcessor is null.

我是否必须使用Spring的delegatingFilterProxy或类似的东西,以便可以向Bean中注入东西,或者可以调整web.xml?

Do I have to use Spring's delegatingFilterProxy or similar so I can inject stuffs into the bean, or can I tweak the 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:

Spring-applicationContext.xml:

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

推荐答案

您不能像这样对Filter Spring进行管理.使用您的设置,它会在每年春天实例化一次,并在servlet容器中实例化一次.相反,请使用 :

You can't make a Filter spring managed like this. With your setup it is instantiated once by spring, and once by the servlet container. Instead, use DelegatingFilterProxy:

  1. 在web.xml中将过滤器代理声明为<filter>
  2. 设置过滤器定义的targetBeanName init-param以指定应实际处理过滤的Bean:

  1. declare the filter proxy as a <filter> in web.xml
  2. Set the targetBeanName init-param of the filter definition to specify the bean that should actually handle the filtering:

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

这篇关于Spring Framework过滤器,未注入Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆