Skip to content
tylertreat edited this page Dec 31, 2012 · 2 revisions

The BeanPostProcessor allows for beans to be modified after they have been initialized by the container. BeanPostProcessor exposes a single method, postProcessBean. This method is invoked on every registered bean for all registered BeanPostProcessors after the bean definitions have been initialized and immediately before any BeanFactoryPostProcessor implementations have been called. BeanPostProcessors are useful for any situations where a particular bean or group of beans need to be altered or replaced based on certain conditions.

Example Implementation

Infinitum uses a BeanPostProcessor to resolve autowired dependencies in beans. Below is a slightly simplified fragment of that implementation with code excluded for brevity. Since BeanPostProcessors are special types of beans, they are registered with an InfinitumContext as normal.

@Component
public class AutowiredBeanPostProcessor implements BeanPostProcessor {

    @Override
    public void postProcessBean(InfinitumContext context, AbstractBeanDefinition beanDefinition) {
        registerFieldInjections(context.getBeanFactory(), beanDefinition);
	registerSetterInjections(context.getBeanFactory(), beanDefinition);
    }

    // ...

}

If you are not using component scanning, you can register this post processor in the infinitum.cfg.xml:

<bean id="autowiredBeanPostProcessor"
    class="com.foo.bar.postprocessor.AutowiredBeanPostProcessor" />

Clone this wiki locally