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

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

Introduction

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

Prototype

public void setTargetBeanName(String targetBeanName) 

Source Link

Document

Set the name of the target bean in the factory.

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 .  java  2s .  co  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;
    }
}