Example usage for org.springframework.aop.target AbstractPrototypeBasedTargetSource setBeanFactory

List of usage examples for org.springframework.aop.target AbstractPrototypeBasedTargetSource setBeanFactory

Introduction

In this page you can find the example usage for org.springframework.aop.target AbstractPrototypeBasedTargetSource setBeanFactory.

Prototype

@Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException 

Source Link

Usage

From source file:org.springframework.aop.framework.autoproxy.target.AbstractPrototypeBasedTargetSourceCreator.java

public final TargetSource getTargetSource(Object bean, String beanName, BeanFactory factory) {
    AbstractPrototypeBasedTargetSource prototypeTargetSource = createPrototypeTargetSource(bean, beanName,
            factory);// w  w w  .  j  a  v  a2 s.c o  m
    if (prototypeTargetSource == null) {
        return null;
    } else {
        if (!(factory instanceof BeanDefinitionRegistry)) {
            logger.warn(
                    "Cannot do autopooling with a BeanFactory that doesn't implement BeanDefinitionRegistry");
            return null;
        }
        BeanDefinitionRegistry definitionRegistry = (BeanDefinitionRegistry) factory;
        RootBeanDefinition definition = (RootBeanDefinition) definitionRegistry.getBeanDefinition(beanName);

        logger.info("Configuring AbstractPrototypeBasedTargetSource...");

        // Infinite cycle will result if we don't use a different factory,
        // because a getBean() call with this beanName will go through the autoproxy
        // infrastructure again.
        // We to override just this bean definition, as it may reference other beans
        // and we're happy to take the parent's definition for those.
        DefaultListableBeanFactory beanFactory2 = new DefaultListableBeanFactory(factory);

        // Override the prototype bean
        beanFactory2.registerBeanDefinition(beanName, definition);

        // Complete configuring the PrototypeTargetSource
        prototypeTargetSource.setTargetBeanName(beanName);
        prototypeTargetSource.setBeanFactory(beanFactory2);

        return prototypeTargetSource;
    }
}