Example usage for org.springframework.beans.factory.config ConfigurableBeanFactory addBeanPostProcessor

List of usage examples for org.springframework.beans.factory.config ConfigurableBeanFactory addBeanPostProcessor

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config ConfigurableBeanFactory addBeanPostProcessor.

Prototype

void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);

Source Link

Document

Add a new BeanPostProcessor that will get applied to beans created by this factory.

Usage

From source file:org.sakaiproject.entitybroker.util.spring.BeanCollectorAutoRegistrar.java

public void afterPropertiesSet() throws Exception {
    log.debug("setAC: " + applicationContext.getDisplayName());
    ConfigurableApplicationContext cac = (ConfigurableApplicationContext) applicationContext;
    ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) cac.getBeanFactory();

    cbf.addBeanPostProcessor(new BeanPostProcessor() {
        @SuppressWarnings("unchecked")
        public Object postProcessBeforeInitialization(Object bean, String beanName) {
            if (bean instanceof BeanCollector<?>) {
                BeanCollector<Object> bc = (BeanCollector<Object>) bean;
                Class<?> c = bc.getCollectedType();
                if (c == null) {
                    throw new IllegalArgumentException("collected type cannot be null");
                }/*from   w  w w. j  a  v  a2s  .  c om*/

                List<Object> l = getAutoRegisteredBeansOfType(c);
                logCollectedBeanInsertion(beanName, c.getName(), l);
                bc.setCollectedBeans(l);
            } else if (bean instanceof BeanMapCollector) {
                BeanMapCollector bc = (BeanMapCollector) bean;
                Class<?>[] cArray = bc.getCollectedTypes();
                if (cArray == null) {
                    throw new IllegalArgumentException("collected types cannot be null");
                }

                Map<Class<?>, List<?>> collectedBeans = new HashMap<Class<?>, List<?>>();
                for (int i = 0; i < cArray.length; i++) {
                    List<Object> l = getAutoRegisteredBeansOfType(cArray[i]);
                    logCollectedBeanInsertion(beanName, cArray[i].getName(), l);
                    collectedBeans.put(cArray[i], l);
                }
                bc.setCollectedBeansMap(collectedBeans);
            }
            return bean;
        }

        public Object postProcessAfterInitialization(Object bean, String beanName) {
            return bean;
        }
    });

}