Java Code Examples for org.springframework.beans.factory.config.BeanDefinitionHolder#getBeanName()

The following examples show how to use org.springframework.beans.factory.config.BeanDefinitionHolder#getBeanName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestScopedProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testGetAnonymousInnerBeanFromScope() throws Exception {
	TestBean bean = (TestBean) this.beanFactory.getBean("outerBean");
	assertFalse(AopUtils.isAopProxy(bean));
	assertTrue(AopUtils.isCglibProxy(bean.getSpouse()));

	BeanDefinition beanDef = this.beanFactory.getBeanDefinition("outerBean");
	BeanDefinitionHolder innerBeanDef =
			(BeanDefinitionHolder) beanDef.getPropertyValues().getPropertyValue("spouse").getValue();
	String name = innerBeanDef.getBeanName();

	MockHttpServletRequest request = new MockHttpServletRequest();
	RequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);

	try {
		assertNull(request.getAttribute("scopedTarget." + name));
		assertEquals("scoped", bean.getSpouse().getName());
		assertNotNull(request.getAttribute("scopedTarget." + name));
		assertEquals(TestBean.class, request.getAttribute("scopedTarget." + name).getClass());
		assertEquals("scoped", ((TestBean) request.getAttribute("scopedTarget." + name)).getName());
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example 2
Source File: BeanDefinitionReaderUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Register the given bean definition with the given bean factory.
 * @param definitionHolder the bean definition including name and aliases
 * @param registry the bean factory to register with
 * @throws BeanDefinitionStoreException if registration failed
 */
public static void registerBeanDefinition(
		BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
		throws BeanDefinitionStoreException {

	// Register bean definition under primary name.
	// 注释 1.17 在 DefaultListableBeanFactory 的 beanDefinitionMap 中添加 bean 定义
	String beanName = definitionHolder.getBeanName();
	registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());

	// Register aliases for bean name, if any.
	String[] aliases = definitionHolder.getAliases();
	if (aliases != null) {
		for (String alias : aliases) {
			registry.registerAlias(beanName, alias);
		}
	}
}
 
Example 3
Source File: BeanDefinitionReaderUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Register the given bean definition with the given bean factory.
 * @param definitionHolder the bean definition including name and aliases
 * @param registry the bean factory to register with
 * @throws BeanDefinitionStoreException if registration failed
 */
public static void registerBeanDefinition(
		BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
		throws BeanDefinitionStoreException {

	// Register bean definition under primary name.
	String beanName = definitionHolder.getBeanName();
	registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());

	// Register aliases for bean name, if any.
	String[] aliases = definitionHolder.getAliases();
	if (aliases != null) {
		for (String alias : aliases) {
			registry.registerAlias(beanName, alias);
		}
	}
}
 
Example 4
Source File: RequestScopedProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testGetAnonymousInnerBeanFromScope() throws Exception {
	TestBean bean = (TestBean) this.beanFactory.getBean("outerBean");
	assertFalse(AopUtils.isAopProxy(bean));
	assertTrue(AopUtils.isCglibProxy(bean.getSpouse()));

	BeanDefinition beanDef = this.beanFactory.getBeanDefinition("outerBean");
	BeanDefinitionHolder innerBeanDef =
			(BeanDefinitionHolder) beanDef.getPropertyValues().getPropertyValue("spouse").getValue();
	String name = innerBeanDef.getBeanName();

	MockHttpServletRequest request = new MockHttpServletRequest();
	RequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);

	try {
		assertNull(request.getAttribute("scopedTarget." + name));
		assertEquals("scoped", bean.getSpouse().getName());
		assertNotNull(request.getAttribute("scopedTarget." + name));
		assertEquals(TestBean.class, request.getAttribute("scopedTarget." + name).getClass());
		assertEquals("scoped", ((TestBean) request.getAttribute("scopedTarget." + name)).getName());
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example 5
Source File: BeanDefinitionReaderUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Register the given bean definition with the given bean factory.
 * @param definitionHolder the bean definition including name and aliases
 * @param registry the bean factory to register with
 * @throws BeanDefinitionStoreException if registration failed
 */
public static void registerBeanDefinition(
		BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
		throws BeanDefinitionStoreException {

	// Register bean definition under primary name.
	String beanName = definitionHolder.getBeanName();
	registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());

	// Register aliases for bean name, if any.
	String[] aliases = definitionHolder.getAliases();
	if (aliases != null) {
		for (String alias : aliases) {
			registry.registerAlias(beanName, alias);
		}
	}
}
 
Example 6
Source File: BeanDefinitionReaderUtils.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
/**
 * Register the given bean definition with the given bean factory.
 * @param definitionHolder the bean definition including name and aliases
 * @param registry the bean factory to register with
 * @throws BeanDefinitionStoreException if registration failed
 */
public static void registerBeanDefinition(
		BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
		throws BeanDefinitionStoreException {

	// Register bean definition under primary name.
	String beanName = definitionHolder.getBeanName();
	registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());

	// Register aliases for bean name, if any.
	String[] aliases = definitionHolder.getAliases();
	if (aliases != null) {
		for (String aliase : aliases) {
			registry.registerAlias(beanName, aliase);
		}
	}
}
 
Example 7
Source File: RequestScopedProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetAnonymousInnerBeanFromScope() throws Exception {
	TestBean bean = (TestBean) this.beanFactory.getBean("outerBean");
	assertFalse(AopUtils.isAopProxy(bean));
	assertTrue(AopUtils.isCglibProxy(bean.getSpouse()));

	BeanDefinition beanDef = this.beanFactory.getBeanDefinition("outerBean");
	BeanDefinitionHolder innerBeanDef =
			(BeanDefinitionHolder) beanDef.getPropertyValues().getPropertyValue("spouse").getValue();
	String name = innerBeanDef.getBeanName();

	MockHttpServletRequest request = new MockHttpServletRequest();
	RequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);

	try {
		assertNull(request.getAttribute("scopedTarget." + name));
		assertEquals("scoped", bean.getSpouse().getName());
		assertNotNull(request.getAttribute("scopedTarget." + name));
		assertEquals(TestBean.class, request.getAttribute("scopedTarget." + name).getClass());
		assertEquals("scoped", ((TestBean) request.getAttribute("scopedTarget." + name)).getName());
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example 8
Source File: BeanDefinitionReaderUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Register the given bean definition with the given bean factory.
 * @param definitionHolder the bean definition including name and aliases
 * @param registry the bean factory to register with
 * @throws BeanDefinitionStoreException if registration failed
 */
public static void registerBeanDefinition(
		BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
		throws BeanDefinitionStoreException {

	// Register bean definition under primary name.
	String beanName = definitionHolder.getBeanName();
	registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());

	// Register aliases for bean name, if any.
	String[] aliases = definitionHolder.getAliases();
	if (aliases != null) {
		for (String alias : aliases) {
			registry.registerAlias(beanName, alias);
		}
	}
}
 
Example 9
Source File: ServiceAnnotationBeanPostProcessor.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
/**
     * Registers {@link ServiceBean} from new annotated {@link Service} {@link BeanDefinition} 从新的带注释的服务bean定义注册ServiceBean
     *
     * @param beanDefinitionHolder
     * @param registry
     * @param scanner
     * @see ServiceBean
     * @see BeanDefinition
     */
    private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, BeanDefinitionRegistry registry,
                                     DubboClassPathBeanDefinitionScanner scanner) {

        Class<?> beanClass = resolveClass(beanDefinitionHolder);

//        从beanClass上获取@service注解对象
        Service service = findAnnotation(beanClass, Service.class);

//        解析@service注解指定的interface的Class对象
        Class<?> interfaceClass = resolveServiceInterfaceClass(beanClass, service);

        String annotatedServiceBeanName = beanDefinitionHolder.getBeanName();

//        构建service bean定义=》
        AbstractBeanDefinition serviceBeanDefinition =
                buildServiceBeanDefinition(service, interfaceClass, annotatedServiceBeanName);

        // ServiceBean Bean name 默认是接口类的权限定名
        String beanName = generateServiceBeanName(service, interfaceClass, annotatedServiceBeanName);

        if (scanner.checkCandidate(beanName, serviceBeanDefinition)) { // check duplicated candidate bean
            registry.registerBeanDefinition(beanName, serviceBeanDefinition);

            if (logger.isInfoEnabled()) {
                logger.warn("The BeanDefinition[" + serviceBeanDefinition +
                        "] of ServiceBean has been registered with name : " + beanName);
            }

        } else {

            if (logger.isWarnEnabled()) {
                logger.warn("The Duplicated BeanDefinition[" + serviceBeanDefinition +
                        "] of ServiceBean[ bean name : " + beanName +
                        "] was be found , Did @DubboComponentScan scan to same package in many times?");
            }

        }

    }
 
Example 10
Source File: ClassPathHttpApiScanner.java    From http-api-invoker with MIT License 5 votes vote down vote up
/**
 * registry the bean with FactoryBean
 */
@Override
protected void registerBeanDefinition(BeanDefinitionHolder holder, BeanDefinitionRegistry registry) {
    GenericBeanDefinition definition = (GenericBeanDefinition) holder.getBeanDefinition();
    if (logger.isDebugEnabled()) {
        logger.debug(includeAnn.getSimpleName() + ": Bean with name '" + holder.getBeanName()
                + "' and '" + definition.getBeanClassName() + "' interface");
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Enabling autowire by type for " + factoryBean.getSimpleName() + " with name '" + holder.getBeanName() + "'.");
    }
    definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
    // 需要被代理的接口 the interface
    definition.getPropertyValues().add("interfaceClass", definition.getBeanClassName());
    // 配置项
    definition.getPropertyValues().add("propertyResolver", propertyResolver);
    if (requestor != null) {
        definition.getPropertyValues().add("requestor", requestor);
    }
    if (requestPreprocessor != null) {
        definition.getPropertyValues().add("requestPreprocessor", requestPreprocessor);
    }
    if (responseProcessor != null) {
        definition.getPropertyValues().add("responseProcessor", responseProcessor);
    }
    // 获取bean名,注意:获取 BeanName 要在setBeanClass之前,否则BeanName就会被覆盖
    // caution! we nned to getBeanName first before setBeanClass
    String beanName = holder.getBeanName();
    // 将BeanClass设置成Bean工厂
    definition.setBeanClass(factoryBean);
    // 注册Bean
    registry.registerBeanDefinition(beanName, definition);
}
 
Example 11
Source File: GroovyBeanDefinitionWrapper.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public void setBeanDefinitionHolder(BeanDefinitionHolder holder) {
	this.definition = (AbstractBeanDefinition) holder.getBeanDefinition();
	this.beanName = holder.getBeanName();
}
 
Example 12
Source File: ScopedProxyUtils.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Generate a scoped proxy for the supplied target bean, registering the target
 * bean with an internal name and setting 'targetBeanName' on the scoped proxy.
 * @param definition the original bean definition
 * @param registry the bean definition registry
 * @param proxyTargetClass whether to create a target class proxy
 * @return the scoped proxy definition
 */
public static BeanDefinitionHolder createScopedProxy(BeanDefinitionHolder definition,
		BeanDefinitionRegistry registry, boolean proxyTargetClass) {

	String originalBeanName = definition.getBeanName();
	BeanDefinition targetDefinition = definition.getBeanDefinition();
	String targetBeanName = getTargetBeanName(originalBeanName);

	// Create a scoped proxy definition for the original bean name,
	// "hiding" the target bean in an internal target definition.
	RootBeanDefinition proxyDefinition = new RootBeanDefinition(ScopedProxyFactoryBean.class);
	proxyDefinition.setDecoratedDefinition(new BeanDefinitionHolder(targetDefinition, targetBeanName));
	proxyDefinition.setOriginatingBeanDefinition(targetDefinition);
	proxyDefinition.setSource(definition.getSource());
	proxyDefinition.setRole(targetDefinition.getRole());

	proxyDefinition.getPropertyValues().add("targetBeanName", targetBeanName);
	if (proxyTargetClass) {
		targetDefinition.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE);
		// ScopedProxyFactoryBean's "proxyTargetClass" default is TRUE, so we don't need to set it explicitly here.
	}
	else {
		proxyDefinition.getPropertyValues().add("proxyTargetClass", Boolean.FALSE);
	}

	// Copy autowire settings from original bean definition.
	proxyDefinition.setAutowireCandidate(targetDefinition.isAutowireCandidate());
	proxyDefinition.setPrimary(targetDefinition.isPrimary());
	if (targetDefinition instanceof AbstractBeanDefinition) {
		proxyDefinition.copyQualifiersFrom((AbstractBeanDefinition) targetDefinition);
	}

	// The target bean should be ignored in favor of the scoped proxy.
	targetDefinition.setAutowireCandidate(false);
	targetDefinition.setPrimary(false);

	// Register the target bean as separate bean in the factory.
	registry.registerBeanDefinition(targetBeanName, targetDefinition);

	// Return the scoped proxy definition as primary bean definition
	// (potentially an inner bean).
	return new BeanDefinitionHolder(proxyDefinition, originalBeanName, definition.getAliases());
}
 
Example 13
Source File: GroovyBeanDefinitionWrapper.java    From blog_demos with Apache License 2.0 4 votes vote down vote up
public void setBeanDefinitionHolder(BeanDefinitionHolder holder) {
	this.definition = (AbstractBeanDefinition) holder.getBeanDefinition();
	this.beanName = holder.getBeanName();
}
 
Example 14
Source File: ScopedProxyUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Generate a scoped proxy for the supplied target bean, registering the target
 * bean with an internal name and setting 'targetBeanName' on the scoped proxy.
 * @param definition the original bean definition
 * @param registry the bean definition registry
 * @param proxyTargetClass whether to create a target class proxy
 * @return the scoped proxy definition
 */
public static BeanDefinitionHolder createScopedProxy(BeanDefinitionHolder definition,
		BeanDefinitionRegistry registry, boolean proxyTargetClass) {

	String originalBeanName = definition.getBeanName();
	BeanDefinition targetDefinition = definition.getBeanDefinition();
	String targetBeanName = getTargetBeanName(originalBeanName);

	// Create a scoped proxy definition for the original bean name,
	// "hiding" the target bean in an internal target definition.
	RootBeanDefinition proxyDefinition = new RootBeanDefinition(ScopedProxyFactoryBean.class);
	proxyDefinition.setDecoratedDefinition(new BeanDefinitionHolder(targetDefinition, targetBeanName));
	proxyDefinition.setOriginatingBeanDefinition(targetDefinition);
	proxyDefinition.setSource(definition.getSource());
	proxyDefinition.setRole(targetDefinition.getRole());

	proxyDefinition.getPropertyValues().add("targetBeanName", targetBeanName);
	if (proxyTargetClass) {
		targetDefinition.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE);
		// ScopedProxyFactoryBean's "proxyTargetClass" default is TRUE, so we don't need to set it explicitly here.
	}
	else {
		proxyDefinition.getPropertyValues().add("proxyTargetClass", Boolean.FALSE);
	}

	// Copy autowire settings from original bean definition.
	proxyDefinition.setAutowireCandidate(targetDefinition.isAutowireCandidate());
	proxyDefinition.setPrimary(targetDefinition.isPrimary());
	if (targetDefinition instanceof AbstractBeanDefinition) {
		proxyDefinition.copyQualifiersFrom((AbstractBeanDefinition) targetDefinition);
	}

	// The target bean should be ignored in favor of the scoped proxy.
	targetDefinition.setAutowireCandidate(false);
	targetDefinition.setPrimary(false);

	// Register the target bean as separate bean in the factory.
	registry.registerBeanDefinition(targetBeanName, targetDefinition);

	// Return the scoped proxy definition as primary bean definition
	// (potentially an inner bean).
	return new BeanDefinitionHolder(proxyDefinition, originalBeanName, definition.getAliases());
}
 
Example 15
Source File: GroovyBeanDefinitionWrapper.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public void setBeanDefinitionHolder(BeanDefinitionHolder holder) {
	this.definition = (AbstractBeanDefinition) holder.getBeanDefinition();
	this.beanName = holder.getBeanName();
}
 
Example 16
Source File: SerializableProxyUtils.java    From jdal with Apache License 2.0 4 votes vote down vote up
public static  BeanDefinitionHolder createSerializableProxy(BeanDefinitionHolder definition,
		BeanDefinitionRegistry registry, boolean proxyTargetClass) {

	String originalBeanName = definition.getBeanName();
	BeanDefinition targetDefinition = definition.getBeanDefinition();

	// Create a scoped proxy definition for the original bean name,
	// "hiding" the target bean in an internal target definition.
	RootBeanDefinition proxyDefinition = new RootBeanDefinition(SerializableProxyFactoryBean.class);
	proxyDefinition.setOriginatingBeanDefinition(definition.getBeanDefinition());
	proxyDefinition.setSource(definition.getSource());
	proxyDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);

	String targetBeanName = getTargetBeanName(originalBeanName);
	proxyDefinition.getPropertyValues().add("targetBeanName", targetBeanName);

	if (proxyTargetClass) {
		targetDefinition.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE);
	}
	else {
		proxyDefinition.getPropertyValues().add("proxyTargetClass", Boolean.FALSE);
	}

	// Copy autowire settings from original bean definition.
	proxyDefinition.setAutowireCandidate(targetDefinition.isAutowireCandidate());
	proxyDefinition.setPrimary(targetDefinition.isPrimary());
	if (targetDefinition instanceof AbstractBeanDefinition) {
		proxyDefinition.copyQualifiersFrom((AbstractBeanDefinition) targetDefinition);
	}
	
	// Set singleton property of FactoryBean
	proxyDefinition.getPropertyValues().add("singleton", !targetDefinition.isPrototype());

	// The target bean should be ignored in favor of the scoped proxy.
	targetDefinition.setAutowireCandidate(false);
	targetDefinition.setPrimary(false);

	// Register the target bean as separate bean in the factory.
	registry.registerBeanDefinition(targetBeanName, targetDefinition);

	// Return the scoped proxy definition as primary bean definition
	// (potentially an inner bean).
	return new BeanDefinitionHolder(proxyDefinition, originalBeanName, definition.getAliases());
}
 
Example 17
Source File: GroovyBeanDefinitionWrapper.java    From java-technology-stack with MIT License 4 votes vote down vote up
public void setBeanDefinitionHolder(BeanDefinitionHolder holder) {
	this.definition = (AbstractBeanDefinition) holder.getBeanDefinition();
	this.beanName = holder.getBeanName();
}
 
Example 18
Source File: ScopedProxyUtils.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Generate a scoped proxy for the supplied target bean, registering the target
 * bean with an internal name and setting 'targetBeanName' on the scoped proxy.
 * @param definition the original bean definition
 * @param registry the bean definition registry
 * @param proxyTargetClass whether to create a target class proxy
 * @return the scoped proxy definition
 */
public static BeanDefinitionHolder createScopedProxy(BeanDefinitionHolder definition,
		BeanDefinitionRegistry registry, boolean proxyTargetClass) {

	String originalBeanName = definition.getBeanName();
	BeanDefinition targetDefinition = definition.getBeanDefinition();
	String targetBeanName = getTargetBeanName(originalBeanName);

	// Create a scoped proxy definition for the original bean name,
	// "hiding" the target bean in an internal target definition.
	RootBeanDefinition proxyDefinition = new RootBeanDefinition(ScopedProxyFactoryBean.class);
	proxyDefinition.setDecoratedDefinition(new BeanDefinitionHolder(targetDefinition, targetBeanName));
	proxyDefinition.setOriginatingBeanDefinition(targetDefinition);
	proxyDefinition.setSource(definition.getSource());
	proxyDefinition.setRole(targetDefinition.getRole());

	proxyDefinition.getPropertyValues().add("targetBeanName", targetBeanName);
	if (proxyTargetClass) {
		targetDefinition.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE);
		// ScopedProxyFactoryBean's "proxyTargetClass" default is TRUE, so we don't need to set it explicitly here.
	}
	else {
		proxyDefinition.getPropertyValues().add("proxyTargetClass", Boolean.FALSE);
	}

	// Copy autowire settings from original bean definition.
	proxyDefinition.setAutowireCandidate(targetDefinition.isAutowireCandidate());
	proxyDefinition.setPrimary(targetDefinition.isPrimary());
	if (targetDefinition instanceof AbstractBeanDefinition) {
		proxyDefinition.copyQualifiersFrom((AbstractBeanDefinition) targetDefinition);
	}

	// The target bean should be ignored in favor of the scoped proxy.
	targetDefinition.setAutowireCandidate(false);
	targetDefinition.setPrimary(false);

	// Register the target bean as separate bean in the factory.
	registry.registerBeanDefinition(targetBeanName, targetDefinition);

	// Return the scoped proxy definition as primary bean definition
	// (potentially an inner bean).
	return new BeanDefinitionHolder(proxyDefinition, originalBeanName, definition.getAliases());
}
 
Example 19
Source File: GroovyBeanDefinitionWrapper.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public void setBeanDefinitionHolder(BeanDefinitionHolder holder) {
	this.definition = (AbstractBeanDefinition) holder.getBeanDefinition();
	this.beanName = holder.getBeanName();
}
 
Example 20
Source File: ScopedProxyUtils.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Generate a scoped proxy for the supplied target bean, registering the target
 * bean with an internal name and setting 'targetBeanName' on the scoped proxy.
 * @param definition the original bean definition
 * @param registry the bean definition registry
 * @param proxyTargetClass whether to create a target class proxy
 * @return the scoped proxy definition
 */
public static BeanDefinitionHolder createScopedProxy(BeanDefinitionHolder definition,
		BeanDefinitionRegistry registry, boolean proxyTargetClass) {

	String originalBeanName = definition.getBeanName();
	BeanDefinition targetDefinition = definition.getBeanDefinition();
	String targetBeanName = getTargetBeanName(originalBeanName);

	// Create a scoped proxy definition for the original bean name,
	// "hiding" the target bean in an internal target definition.
	RootBeanDefinition proxyDefinition = new RootBeanDefinition(ScopedProxyFactoryBean.class);
	proxyDefinition.setDecoratedDefinition(new BeanDefinitionHolder(targetDefinition, targetBeanName));
	proxyDefinition.setOriginatingBeanDefinition(targetDefinition);
	proxyDefinition.setSource(definition.getSource());
	proxyDefinition.setRole(targetDefinition.getRole());

	proxyDefinition.getPropertyValues().add("targetBeanName", targetBeanName);
	if (proxyTargetClass) {
		targetDefinition.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE);
		// ScopedProxyFactoryBean's "proxyTargetClass" default is TRUE, so we don't need to set it explicitly here.
	}
	else {
		proxyDefinition.getPropertyValues().add("proxyTargetClass", Boolean.FALSE);
	}

	// Copy autowire settings from original bean definition.
	proxyDefinition.setAutowireCandidate(targetDefinition.isAutowireCandidate());
	proxyDefinition.setPrimary(targetDefinition.isPrimary());
	if (targetDefinition instanceof AbstractBeanDefinition) {
		proxyDefinition.copyQualifiersFrom((AbstractBeanDefinition) targetDefinition);
	}

	// The target bean should be ignored in favor of the scoped proxy.
	targetDefinition.setAutowireCandidate(false);
	targetDefinition.setPrimary(false);

	// Register the target bean as separate bean in the factory.
	registry.registerBeanDefinition(targetBeanName, targetDefinition);

	// Return the scoped proxy definition as primary bean definition
	// (potentially an inner bean).
	return new BeanDefinitionHolder(proxyDefinition, originalBeanName, definition.getAliases());
}