I have many cookie-cutter spring beans and don’t want to explicitly define each one in xml. So I went the component scanning route which lets me do this. This is nice, but I just realized that MyBeanPostProcessor isn’t being called for the beans loaded in using the component-scan technique. MyBeanPostProcessor simply attempts to do some setter injection on these beans. The below configuration just shows this approach I tried which doesn’t work. Any other ideas how to do setter injection on these beans?
I’m using Spring 2.5.5
Thanks,
Ben
<context:component-scan base-package="us.benanderson"
use-default-filters="false"
annotation-config="false"
scope-resolver="us.benanderson.MyScopeResolver"
name-generator="us.benanderson.MyBeanNameGenerator">
<context:include-filter type="custom" expression="us.benanderson.MyTypeFilter" />
</context:component-scan>
<bean class="us.benanderson.MyBeanPostProcessor">
<property name="order" value="500" />
</bean>
Here is my test case, that appears to work (Spring 2.5.6). I thought about excluding some files for brevity, but I decided against it.
Start.java (entry-point)
Context.xml
CustomBean.java (this is the I want to find – see MyTypeFilter)
MyBeanPostProcessor.java
MyScopeResolver.java
MyTypeFilter.java
This produces the following output:
So, as you can see, the
customBeanbean was found by my type filter, added as a bean, and whenapplicationContext.getBean("customBean")was called a new object was instantiated and then it was passed to my post bean processor.