[英]Spring Framework filter, bean not injected
There are 2 entries for a Servlet Filter, one in web.xml and one in Spring applicationContext.xml Servlet过滤器有2个条目,一个在web.xml中,另一个在Spring applicationContext.xml中
I added the filter into applicationContext.xml because I wanted to inject creditProcessor bean into it. 我将过滤器添加到applicationContext.xml中,因为我想将creditProcessor bean注入其中。
The only problem is that the entry in web.xml got picked up by JBoss and then used, so creditProcessor is null. 唯一的问题是web.xml中的条目被JBoss拾取然后使用,因此creditProcessor为null。
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? 我是否必须使用Spring的delegingFilterProxy或类似的东西,以便我可以将东西注入bean中,或者我可以调整web.xml吗?
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>
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.
使用您的设置,它将由spring实例化一次,并由servlet容器实例化一次。 Instead, use
DelegatingFilterProxy
: 而是使用
DelegatingFilterProxy
:
<filter>
in web.xml <filter>
Set the targetBeanName
init-param of the filter definition to specify the bean that should actually handle the filtering: 设置过滤器定义的
targetBeanName
init-param以指定应该实际处理过滤的bean:
<init-param> <param-name>targetBeanName</param-name> <param-value>creditFilter</param-value> </init-param>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.