Take the 2-minute tour ×

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" 
    <context:include-filter type="custom" expression="us.benanderson.MyTypeFilter" />
<bean class="us.benanderson.MyBeanPostProcessor">
    <property name="order" value="500" />
share|improve this question
What happens if you try explicitly adding a bean definition instead of using classpath scanning? Can you confirm that the post processor works in that case? –  skaffman Jul 13 '09 at 19:40
yes, I did confirm that the beanpostprocessor works in that case –  andersonbd1 Jul 13 '09 at 23:26
are you looking for alternatives to the BeanPostProcessor for injection? Or is your primary goal to get component-scan working with or without the BeanPostProcessor? –  Brad Cupit Aug 19 '09 at 19:35
alternatives would be fine, although I'm not sure there are any. My requirement is: - auto load beans w/out needing to explicitly define them - do some simple setter injection "MyBeanPostProcessor isn't being called for the beans loaded in using the component-scan technique." I don't really know how else to say this. The beans that are defined using the component-scan technique aren't being passed to MyBeanPostProcessor. MyBeanPostProcessor simply is attempting to do some setter injection. MyBeanPostProcessor works fine for beans that I explicitly define in the xml. –  andersonbd1 Aug 20 '09 at 12:15
I created a small test case and it works perfectly for me. Even when I have annotation-config turned off and am using a custom TypeFilter. I think your best option is to try and take a few steps back: try to isolate the problem. I see you have your own scope resolver - this could be the culprit (not that it SHOULD cause a problem - it's a possibility though). –  waxwing Aug 25 '09 at 13:12
show 3 more comments

2 Answers

up vote 5 down vote accepted

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)

package se.waxwing.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Start {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("se/waxwing/test/Context.xml");



	<context:component-scan base-package="se.waxwing.test" 
    	<context:include-filter type="custom" expression="se.waxwing.test.MyTypeFilter" />

	<bean id="beanProcessor" class="se.waxwing.test.MyBeanPostProcessor" />


CustomBean.java (this is the I want to find - see MyTypeFilter)

package se.waxwing.test;

public class CustomBean {

    public CustomBean() {
        System.err.println("instantiating component");


package se.waxwing.test;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {

    public MyBeanPostProcessor() {

        System.err.println("after " + beanName);
        return bean;

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.err.println("before " + beanName);
        return bean;


package se.waxwing.test;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ScopeMetadata;
import org.springframework.context.annotation.ScopeMetadataResolver;
import org.springframework.context.annotation.ScopedProxyMode;

public class MyScopeResolver implements ScopeMetadataResolver {

    public ScopeMetadata resolveScopeMetadata(BeanDefinition definition) {
        ScopeMetadata result = new ScopeMetadata();
        return result;



package se.waxwing.test;

import java.io.IOException;

import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

public class MyTypeFilter implements TypeFilter {

    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        return metadataReader.getClassMetadata().getClassName().equals(CustomBean.class.getCanonicalName());

This produces the following output:

2009-aug-26 15:44:02 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c1b531: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]; startup date [Wed Aug 26 15:44:02 CEST 2009]; root of context hierarchy
2009-aug-26 15:44:02 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [se/waxwing/test/Context.xml]
2009-aug-26 15:44:02 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]: org.springframework.beans.factory.support.DefaultListableBeanFactory@121f1d
2009-aug-26 15:44:02 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@121f1d: defining beans [customBean,beanProcessor]; root of factory hierarchy
instantiating component
before customBean
after customBean

So, as you can see, the customBean bean was found by my type filter, added as a bean, and when applicationContext.getBean("customBean") was called a new object was instantiated and then it was passed to my post bean processor.

share|improve this answer
I'm an idiot! After I downloaded spring 2.5.6 and tried it out (and it was still broken), I noticed the fact that I wasn't calling applicationContext.getBean("customBean")! So it was actually working for me all along - just my way of confirming it was broken! Thanks for submitting your code and confirming that it works. I want to give you the bounty, but I don't know how. There's no check box or anything - according to this: meta.stackexchange.com/questions/13421/… I guess you're out of luck? –  andersonbd1 Aug 27 '09 at 14:29
No problem, it got me to play around with custom scope-resolvers and type-filters, which is a good thing. Guess I am out of luck on that bounty - can't fight the system. :) –  waxwing Aug 27 '09 at 18:04
add comment

After the application context has loaded check that the beans that you've annotated are available.

I suspect since you've turned off "annotation-config" that it is ignoring your annotations.

share|improve this answer
yes, I have verified that the beans are available. I wrote my own Filter which is why I turned off annotation-config. –  andersonbd1 Jul 15 '09 at 0:47
add comment

Your Answer


