Java Code Examples for org.springframework.beans.factory.support.RootBeanDefinition#setAutowireCandidate()

The following examples show how to use org.springframework.beans.factory.support.RootBeanDefinition#setAutowireCandidate() . 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: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBeanByTypeInstanceFiltersOutNonAutowireCandidates() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition(99);
	RootBeanDefinition bd2 = createConstructorDependencyBeanDefinition(43);
	RootBeanDefinition na1 = createConstructorDependencyBeanDefinition(21);
	na1.setAutowireCandidate(false);

	lbf.registerBeanDefinition("bd1", bd1);
	lbf.registerBeanDefinition("na1", na1);
	ConstructorDependency actual = lbf.getBean(ConstructorDependency.class, 42); // na1 was filtered
	assertThat(actual.beanName, equalTo("bd1"));

	lbf.registerBeanDefinition("bd2", bd2);
	try {
		lbf.getBean(TestBean.class, 67);
		fail("Should have thrown NoSuchBeanDefinitionException");
	}
	catch (NoSuchBeanDefinitionException ex) {
		// expected
	}
}
 
Example 2
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBeanByTypeFiltersOutNonAutowireCandidates() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	RootBeanDefinition na1 = new RootBeanDefinition(TestBean.class);
	na1.setAutowireCandidate(false);

	lbf.registerBeanDefinition("bd1", bd1);
	lbf.registerBeanDefinition("na1", na1);
	TestBean actual = lbf.getBean(TestBean.class); // na1 was filtered
	assertSame(lbf.getBean("bd1", TestBean.class), actual);

	lbf.registerBeanDefinition("bd2", bd2);
	try {
		lbf.getBean(TestBean.class);
		fail("Should have thrown NoSuchBeanDefinitionException");
	}
	catch (NoSuchBeanDefinitionException ex) {
		// expected
	}
}
 
Example 3
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testGetBeanByTypeFiltersOutNonAutowireCandidates() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	RootBeanDefinition na1 = new RootBeanDefinition(TestBean.class);
	na1.setAutowireCandidate(false);

	lbf.registerBeanDefinition("bd1", bd1);
	lbf.registerBeanDefinition("na1", na1);
	TestBean actual = lbf.getBean(TestBean.class);  // na1 was filtered
	assertSame(lbf.getBean("bd1", TestBean.class), actual);

	lbf.registerBeanDefinition("bd2", bd2);
	try {
		lbf.getBean(TestBean.class);
		fail("Should have thrown NoSuchBeanDefinitionException");
	}
	catch (NoSuchBeanDefinitionException ex) {
		// expected
	}
}
 
Example 4
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testGetBeanByTypeInstanceFiltersOutNonAutowireCandidates() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition(99);
	RootBeanDefinition bd2 = createConstructorDependencyBeanDefinition(43);
	RootBeanDefinition na1 = createConstructorDependencyBeanDefinition(21);
	na1.setAutowireCandidate(false);

	lbf.registerBeanDefinition("bd1", bd1);
	lbf.registerBeanDefinition("na1", na1);
	ConstructorDependency actual = lbf.getBean(ConstructorDependency.class, 42);  // na1 was filtered
	assertThat(actual.beanName, equalTo("bd1"));

	lbf.registerBeanDefinition("bd2", bd2);
	try {
		lbf.getBean(TestBean.class, 67);
		fail("Should have thrown NoSuchBeanDefinitionException");
	}
	catch (NoSuchBeanDefinitionException ex) {
		// expected
	}
}
 
Example 5
Source File: InjectAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testMethodInjectionWithMapAndMultipleMatchesButOnlyOneAutowireCandidate() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(MapMethodInjectionBean.class));
	bf.registerBeanDefinition("testBean1", new RootBeanDefinition(TestBean.class));
	RootBeanDefinition rbd2 = new RootBeanDefinition(TestBean.class);
	rbd2.setAutowireCandidate(false);
	bf.registerBeanDefinition("testBean2", rbd2);

	MapMethodInjectionBean bean = (MapMethodInjectionBean) bf.getBean("annotatedBean");
	TestBean tb = (TestBean) bf.getBean("testBean1");
	assertEquals(1, bean.getTestBeanMap().size());
	assertTrue(bean.getTestBeanMap().keySet().contains("testBean1"));
	assertTrue(bean.getTestBeanMap().values().contains(tb));
	assertSame(tb, bean.getTestBean());
	bf.destroySingletons();
}
 
Example 6
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testMethodInjectionWithMapAndMultipleMatchesButOnlyOneAutowireCandidate() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(MapMethodInjectionBean.class));
	bf.registerBeanDefinition("testBean1", new RootBeanDefinition(TestBean.class));
	RootBeanDefinition rbd2 = new RootBeanDefinition(TestBean.class);
	rbd2.setAutowireCandidate(false);
	bf.registerBeanDefinition("testBean2", rbd2);

	MapMethodInjectionBean bean = (MapMethodInjectionBean) bf.getBean("annotatedBean");
	TestBean tb = (TestBean) bf.getBean("testBean1");
	assertEquals(1, bean.getTestBeanMap().size());
	assertTrue(bean.getTestBeanMap().keySet().contains("testBean1"));
	assertTrue(bean.getTestBeanMap().values().contains(tb));
	assertSame(tb, bean.getTestBean());
	bf.destroySingletons();
}
 
Example 7
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testGetBeanByTypeInstanceFiltersOutNonAutowireCandidates() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition(99);
	RootBeanDefinition bd2 = createConstructorDependencyBeanDefinition(43);
	RootBeanDefinition na1 = createConstructorDependencyBeanDefinition(21);
	na1.setAutowireCandidate(false);

	lbf.registerBeanDefinition("bd1", bd1);
	lbf.registerBeanDefinition("na1", na1);
	ConstructorDependency actual = lbf.getBean(ConstructorDependency.class, 42);  // na1 was filtered
	assertThat(actual.beanName, equalTo("bd1"));

	lbf.registerBeanDefinition("bd2", bd2);
	try {
		lbf.getBean(TestBean.class, 67);
		fail("Should have thrown NoSuchBeanDefinitionException");
	}
	catch (NoSuchBeanDefinitionException ex) {
		// expected
	}
}
 
Example 8
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testGetBeanByTypeFiltersOutNonAutowireCandidates() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	RootBeanDefinition na1 = new RootBeanDefinition(TestBean.class);
	na1.setAutowireCandidate(false);

	lbf.registerBeanDefinition("bd1", bd1);
	lbf.registerBeanDefinition("na1", na1);
	TestBean actual = lbf.getBean(TestBean.class);  // na1 was filtered
	assertSame(lbf.getBean("bd1", TestBean.class), actual);

	lbf.registerBeanDefinition("bd2", bd2);
	try {
		lbf.getBean(TestBean.class);
		fail("Should have thrown NoSuchBeanDefinitionException");
	}
	catch (NoSuchBeanDefinitionException ex) {
		// expected
	}
}
 
Example 9
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testMethodInjectionWithMapAndMultipleMatchesButOnlyOneAutowireCandidate() {
	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(MapMethodInjectionBean.class));
	bf.registerBeanDefinition("testBean1", new RootBeanDefinition(TestBean.class));
	RootBeanDefinition rbd2 = new RootBeanDefinition(TestBean.class);
	rbd2.setAutowireCandidate(false);
	bf.registerBeanDefinition("testBean2", rbd2);

	MapMethodInjectionBean bean = (MapMethodInjectionBean) bf.getBean("annotatedBean");
	TestBean tb = (TestBean) bf.getBean("testBean1");
	assertEquals(1, bean.getTestBeanMap().size());
	assertTrue(bean.getTestBeanMap().keySet().contains("testBean1"));
	assertTrue(bean.getTestBeanMap().values().contains(tb));
	assertSame(tb, bean.getTestBean());
}
 
Example 10
Source File: InjectAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testMethodInjectionWithMapAndMultipleMatchesButOnlyOneAutowireCandidate() {
	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(MapMethodInjectionBean.class));
	bf.registerBeanDefinition("testBean1", new RootBeanDefinition(TestBean.class));
	RootBeanDefinition rbd2 = new RootBeanDefinition(TestBean.class);
	rbd2.setAutowireCandidate(false);
	bf.registerBeanDefinition("testBean2", rbd2);

	MapMethodInjectionBean bean = (MapMethodInjectionBean) bf.getBean("annotatedBean");
	TestBean tb = (TestBean) bf.getBean("testBean1");
	assertEquals(1, bean.getTestBeanMap().size());
	assertTrue(bean.getTestBeanMap().keySet().contains("testBean1"));
	assertTrue(bean.getTestBeanMap().values().contains(tb));
	assertSame(tb, bean.getTestBean());
}
 
Example 11
Source File: AutowiredAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testMethodInjectionWithMapAndMultipleMatchesButOnlyOneAutowireCandidate() {
	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(MapMethodInjectionBean.class));
	bf.registerBeanDefinition("testBean1", new RootBeanDefinition(TestBean.class));
	RootBeanDefinition rbd2 = new RootBeanDefinition(TestBean.class);
	rbd2.setAutowireCandidate(false);
	bf.registerBeanDefinition("testBean2", rbd2);

	MapMethodInjectionBean bean = (MapMethodInjectionBean) bf.getBean("annotatedBean");
	TestBean tb = (TestBean) bf.getBean("testBean1");
	assertEquals(1, bean.getTestBeanMap().size());
	assertTrue(bean.getTestBeanMap().keySet().contains("testBean1"));
	assertTrue(bean.getTestBeanMap().values().contains(tb));
	assertSame(tb, bean.getTestBean());
}
 
Example 12
Source File: InjectAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testMethodInjectionWithMapAndMultipleMatchesButOnlyOneAutowireCandidate() {
	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(MapMethodInjectionBean.class));
	bf.registerBeanDefinition("testBean1", new RootBeanDefinition(TestBean.class));
	RootBeanDefinition rbd2 = new RootBeanDefinition(TestBean.class);
	rbd2.setAutowireCandidate(false);
	bf.registerBeanDefinition("testBean2", rbd2);

	MapMethodInjectionBean bean = (MapMethodInjectionBean) bf.getBean("annotatedBean");
	TestBean tb = (TestBean) bf.getBean("testBean1");
	assertEquals(1, bean.getTestBeanMap().size());
	assertTrue(bean.getTestBeanMap().keySet().contains("testBean1"));
	assertTrue(bean.getTestBeanMap().values().contains(tb));
	assertSame(tb, bean.getTestBean());
}
 
Example 13
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 14
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 15
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 16
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());
}
 
Example 17
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());
}