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

The following examples show how to use org.springframework.beans.factory.support.RootBeanDefinition#setPrimary() . 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: AutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testObjectProviderInjectionWithUnresolvedOrderedStream() {
	bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectProviderInjectionBean.class));
	RootBeanDefinition tb1 = new RootBeanDefinition(TestBeanFactory.class);
	tb1.setFactoryMethodName("newTestBean1");
	tb1.setPrimary(true);
	bf.registerBeanDefinition("testBean1", tb1);
	RootBeanDefinition tb2 = new RootBeanDefinition(TestBeanFactory.class);
	tb2.setFactoryMethodName("newTestBean2");
	tb2.setLazyInit(true);
	bf.registerBeanDefinition("testBean2", tb2);

	ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean");
	List<?> testBeans = bean.sortedTestBeans();
	assertEquals(2, testBeans.size());
	assertSame(bf.getBean("testBean2"), testBeans.get(0));
	assertSame(bf.getBean("testBean1"), testBeans.get(1));
}
 
Example 2
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAutowireBeanByTypeWithTwoPrimaryCandidates() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPrimary(true);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setPrimary(true);
	lbf.registerBeanDefinition("test", bd);
	lbf.registerBeanDefinition("spouse", bd2);

	try {
		lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
		assertNotNull("Exception should have cause", ex.getCause());
		assertEquals("Wrong cause type", NoUniqueBeanDefinitionException.class, ex.getCause().getClass());
	}
}
 
Example 3
Source File: AutowireWithExclusionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void byTypeAutowireWithPrimaryInParentAndChild() throws Exception {
	CountingFactory.reset();
	DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
	parent.getBeanDefinition("props1").setPrimary(true);
	parent.preInstantiateSingletons();
	DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
	RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class);
	robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
	robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally"));
	child.registerBeanDefinition("rob2", robDef);
	RootBeanDefinition propsDef = new RootBeanDefinition(PropertiesFactoryBean.class);
	propsDef.getPropertyValues().add("properties", "name=props3");
	propsDef.setPrimary(true);
	child.registerBeanDefinition("props3", propsDef);
	TestBean rob = (TestBean) child.getBean("rob2");
	assertEquals("props3", rob.getSomeProperties().getProperty("name"));
	assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
 
Example 4
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutowireBeanWithFactoryBeanByTypeWithPrimary() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(LazyInitFactory.class);
	RootBeanDefinition bd2 = new RootBeanDefinition(LazyInitFactory.class);
	bd2.setPrimary(true);
	lbf.registerBeanDefinition("bd1", bd1);
	lbf.registerBeanDefinition("bd2", bd2);
	LazyInitFactory bd1FactoryBean = (LazyInitFactory) lbf.getBean("&bd1");
	LazyInitFactory bd2FactoryBean = (LazyInitFactory) lbf.getBean("&bd2");
	assertNotNull(bd1FactoryBean);
	assertNotNull(bd2FactoryBean);
	FactoryBeanDependentBean bean = (FactoryBeanDependentBean) lbf.autowire(FactoryBeanDependentBean.class,
			AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
	assertNotEquals(bd1FactoryBean, bean.getFactoryBean());
	assertEquals(bd2FactoryBean, bean.getFactoryBean());
}
 
Example 5
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testGetBeanByTypeInstanceWithMultiplePrimary() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition(99);
	RootBeanDefinition bd2 = createConstructorDependencyBeanDefinition(43);
	bd1.setPrimary(true);
	bd2.setPrimary(true);
	lbf.registerBeanDefinition("bd1", bd1);
	lbf.registerBeanDefinition("bd2", bd2);

	assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(() ->
			lbf.getBean(ConstructorDependency.class, 42))
		.withMessageContaining("more than one 'primary'");
}
 
Example 6
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetBeanByTypeWithPrimary() throws Exception {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setPrimary(true);
	lbf.registerBeanDefinition("bd1", bd1);
	lbf.registerBeanDefinition("bd2", bd2);
	TestBean bean = lbf.getBean(TestBean.class);
	assertThat(bean.getBeanName(), equalTo("bd2"));
}
 
Example 7
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutowireBeanByTypePrimaryTakesPrecedenceOverPriority() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
	RootBeanDefinition bd = new RootBeanDefinition(HighPriorityTestBean.class);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setPrimary(true);
	lbf.registerBeanDefinition("test", bd);
	lbf.registerBeanDefinition("spouse", bd2);

	DependenciesBean bean = (DependenciesBean)
			lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
	assertThat(bean.getSpouse(), equalTo(lbf.getBean("spouse")));
}
 
Example 8
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetBeanByTypeInstanceWithMultiplePrimary() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition(99);
	RootBeanDefinition bd2 = createConstructorDependencyBeanDefinition(43);
	bd1.setPrimary(true);
	bd2.setPrimary(true);

	lbf.registerBeanDefinition("bd1", bd1);
	lbf.registerBeanDefinition("bd2", bd2);
	thrown.expect(NoUniqueBeanDefinitionException.class);
	thrown.expectMessage(containsString("more than one 'primary'"));
	lbf.getBean(ConstructorDependency.class, 42);
}
 
Example 9
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testAutowireBeanByTypePrimaryTakesPrecedenceOverPriority() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
	RootBeanDefinition bd = new RootBeanDefinition(HighPriorityTestBean.class);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setPrimary(true);
	lbf.registerBeanDefinition("test", bd);
	lbf.registerBeanDefinition("spouse", bd2);

	DependenciesBean bean = (DependenciesBean)
			lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
	assertThat(bean.getSpouse(), equalTo(lbf.getBean("spouse")));
}
 
Example 10
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutowireBeanByTypeWithTwoMatchesAndOnePrimary() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPrimary(true);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	lbf.registerBeanDefinition("test", bd);
	lbf.registerBeanDefinition("spouse", bd2);

	DependenciesBean bean = (DependenciesBean)
			lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
	assertThat(bean.getSpouse(), equalTo(lbf.getBean("test")));
}
 
Example 11
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testGetBeanByTypeWithPrimary() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
	bd1.setLazyInit(true);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setPrimary(true);
	lbf.registerBeanDefinition("bd1", bd1);
	lbf.registerBeanDefinition("bd2", bd2);
	TestBean bean = lbf.getBean(TestBean.class);
	assertThat(bean.getBeanName(), equalTo("bd2"));
	assertFalse(lbf.containsSingleton("bd1"));
}
 
Example 12
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testGetBeanByTypePrimaryHasPrecedenceOverPriority() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
	RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setPrimary(true);
	lbf.registerBeanDefinition("bd1", bd1);
	lbf.registerBeanDefinition("bd2", bd2);
	TestBean bean = lbf.getBean(TestBean.class);
	assertThat(bean.getBeanName(), equalTo("bd2"));
}
 
Example 13
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testGetBeanByTypeWithMultiplePrimary() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
	bd1.setPrimary(true);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setPrimary(true);
	lbf.registerBeanDefinition("bd1", bd1);
	lbf.registerBeanDefinition("bd2", bd2);
	assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(() ->
			lbf.getBean(TestBean.class))
		.withMessageContaining("more than one 'primary'");
}
 
Example 14
Source File: DataSourceBeanFactoryPostProcessor.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
private void createDataSourceProxy(ConfigurableListableBeanFactory beanFactory,
                                   String beanName, BeanDefinition originDataSource,
                                   String jdbcUrl) {
    // re-register origin datasource bean
    BeanDefinitionRegistry beanDefinitionRegistry = (BeanDefinitionRegistry) beanFactory;
    beanDefinitionRegistry.removeBeanDefinition(beanName);
    boolean isPrimary = originDataSource.isPrimary();
    originDataSource.setPrimary(false);
    beanDefinitionRegistry.registerBeanDefinition(transformDatasourceBeanName(beanName),
        originDataSource);
    // register proxied datasource
    RootBeanDefinition proxiedBeanDefinition = new RootBeanDefinition(SmartDataSource.class);
    proxiedBeanDefinition.setRole(BeanDefinition.ROLE_APPLICATION);
    proxiedBeanDefinition.setPrimary(isPrimary);
    proxiedBeanDefinition.setInitMethodName("init");
    proxiedBeanDefinition.setDependsOn(transformDatasourceBeanName(beanName));
    MutablePropertyValues originValues = originDataSource.getPropertyValues();
    MutablePropertyValues values = new MutablePropertyValues();
    String appName = environment.getProperty(TRACER_APPNAME_KEY);
    Assert.isTrue(!StringUtils.isBlank(appName), TRACER_APPNAME_KEY + " must be configured!");
    values.add("appName", appName);
    values.add("delegate", new RuntimeBeanReference(transformDatasourceBeanName(beanName)));
    values.add("dbType",
        DataSourceUtils.resolveDbTypeFromUrl(unwrapPropertyValue(originValues.get(jdbcUrl))));
    values.add("database",
        DataSourceUtils.resolveDatabaseFromUrl(unwrapPropertyValue(originValues.get(jdbcUrl))));
    proxiedBeanDefinition.setPropertyValues(values);
    beanDefinitionRegistry.registerBeanDefinition(beanName, proxiedBeanDefinition);
}
 
Example 15
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testGetBeanByTypeWithMultiplePrimary() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
	bd1.setPrimary(true);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setPrimary(true);
	lbf.registerBeanDefinition("bd1", bd1);
	lbf.registerBeanDefinition("bd2", bd2);
	thrown.expect(NoUniqueBeanDefinitionException.class);
	thrown.expectMessage(containsString("more than one 'primary'"));
	lbf.getBean(TestBean.class);
}
 
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: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testGetBeanByTypeInstanceWithPrimary() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition(99);
	RootBeanDefinition bd2 = createConstructorDependencyBeanDefinition(43);
	bd2.setPrimary(true);
	lbf.registerBeanDefinition("bd1", bd1);
	lbf.registerBeanDefinition("bd2", bd2);

	ConstructorDependency bean = lbf.getBean(ConstructorDependency.class);
	assertThat(bean.beanName, equalTo("bd2"));
	assertThat(bean.spouseAge, equalTo(43));
	bean = lbf.getBean(ConstructorDependency.class, 42);
	assertThat(bean.beanName, equalTo("bd2"));
	assertThat(bean.spouseAge, equalTo(42));

	ObjectProvider<ConstructorDependency> provider = lbf.getBeanProvider(ConstructorDependency.class);
	bean = provider.getObject();
	assertThat(bean.beanName, equalTo("bd2"));
	assertThat(bean.spouseAge, equalTo(43));
	bean = provider.getObject(42);
	assertThat(bean.beanName, equalTo("bd2"));
	assertThat(bean.spouseAge, equalTo(42));
	bean = provider.getIfAvailable();
	assertThat(bean.beanName, equalTo("bd2"));
	assertThat(bean.spouseAge, equalTo(43));
	bean = provider.getIfUnique();
	assertThat(bean.beanName, equalTo("bd2"));
	assertThat(bean.spouseAge, equalTo(43));

	Set<Object> resolved = new HashSet<>();
	for (ConstructorDependency instance : provider) {
		resolved.add(instance);
	}
	assertEquals(2, resolved.size());
	assertTrue(resolved.contains(lbf.getBean("bd1")));
	assertTrue(resolved.contains(lbf.getBean("bd2")));

	resolved = new HashSet<>();
	provider.forEach(resolved::add);
	assertEquals(2, resolved.size());
	assertTrue(resolved.contains(lbf.getBean("bd1")));
	assertTrue(resolved.contains(lbf.getBean("bd2")));

	resolved = provider.stream().collect(Collectors.toSet());
	assertEquals(2, resolved.size());
	assertTrue(resolved.contains(lbf.getBean("bd1")));
	assertTrue(resolved.contains(lbf.getBean("bd2")));
}
 
Example 18
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testGetBeanByTypeInstanceWithPrimary() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = createConstructorDependencyBeanDefinition(99);
	RootBeanDefinition bd2 = createConstructorDependencyBeanDefinition(43);
	bd2.setPrimary(true);
	lbf.registerBeanDefinition("bd1", bd1);
	lbf.registerBeanDefinition("bd2", bd2);

	ConstructorDependency bean = lbf.getBean(ConstructorDependency.class);
	assertThat(bean.beanName, equalTo("bd2"));
	assertThat(bean.spouseAge, equalTo(43));
	bean = lbf.getBean(ConstructorDependency.class, 42);
	assertThat(bean.beanName, equalTo("bd2"));
	assertThat(bean.spouseAge, equalTo(42));

	ObjectProvider<ConstructorDependency> provider = lbf.getBeanProvider(ConstructorDependency.class);
	bean = provider.getObject();
	assertThat(bean.beanName, equalTo("bd2"));
	assertThat(bean.spouseAge, equalTo(43));
	bean = provider.getObject(42);
	assertThat(bean.beanName, equalTo("bd2"));
	assertThat(bean.spouseAge, equalTo(42));
	bean = provider.getIfAvailable();
	assertThat(bean.beanName, equalTo("bd2"));
	assertThat(bean.spouseAge, equalTo(43));
	bean = provider.getIfUnique();
	assertThat(bean.beanName, equalTo("bd2"));
	assertThat(bean.spouseAge, equalTo(43));

	Set<Object> resolved = new HashSet<>();
	for (ConstructorDependency instance : provider) {
		resolved.add(instance);
	}
	assertEquals(2, resolved.size());
	assertTrue(resolved.contains(lbf.getBean("bd1")));
	assertTrue(resolved.contains(lbf.getBean("bd2")));

	resolved = new HashSet<>();
	provider.forEach(resolved::add);
	assertEquals(2, resolved.size());
	assertTrue(resolved.contains(lbf.getBean("bd1")));
	assertTrue(resolved.contains(lbf.getBean("bd2")));

	resolved = provider.stream().collect(Collectors.toSet());
	assertEquals(2, resolved.size());
	assertTrue(resolved.contains(lbf.getBean("bd1")));
	assertTrue(resolved.contains(lbf.getBean("bd2")));
}
 
Example 19
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 20
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());
}