Spring Framework过滤器,未注入Bean
问题内容:
Servlet过滤器有2个条目,一个在web.xml中,一个在Spring applicationContext.xml中。
我将过滤器添加到applicationContext.xml中,因为我想将creditProcessor bean注入其中。
唯一的问题是,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>
Spring-applicationContext.xml:
<bean id="creditFilter" class="credit.filter.CreditFilter" >
<property name="creditProcessor" ref="creditProcessor"/>
</bean>
问题答案:
您不能像这样管理Filter
Spring。使用您的设置,它会在每年spring实例化一次,并在servlet容器中实例化一次。而是使用DelegatingFilterProxy
:
<filter>
在web.xml 中将过滤器代理声明为- 设置
targetBeanName
过滤器定义的init-param以指定应实际处理过滤的Bean:<init-param> <param-name>targetBeanName</param-name> <param-value>creditFilter</param-value>