Home » Java » Spring Framework filter, bean not injected

Spring Framework filter, bean not injected

Posted by: admin December 28, 2021 Leave a comment


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

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

The only problem is that the entry in web.xml got picked up by JBoss and then used, so creditProcessor is 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?





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

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. 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: