当前位置: 首页>>代码示例>>Java>>正文


Java RootBeanDefinition.setPrimary方法代码示例

本文整理汇总了Java中org.springframework.beans.factory.support.RootBeanDefinition.setPrimary方法的典型用法代码示例。如果您正苦于以下问题:Java RootBeanDefinition.setPrimary方法的具体用法?Java RootBeanDefinition.setPrimary怎么用?Java RootBeanDefinition.setPrimary使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.springframework.beans.factory.support.RootBeanDefinition的用法示例。


在下文中一共展示了RootBeanDefinition.setPrimary方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: byTypeAutowireWithPrimaryOverridingParentFactory

import org.springframework.beans.factory.support.RootBeanDefinition; //导入方法依赖的package包/类
@Test
public void byTypeAutowireWithPrimaryOverridingParentFactory() throws Exception {
	CountingFactory.reset();
	DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
	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());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:19,代码来源:AutowireWithExclusionTests.java

示例2: byTypeAutowireWithPrimaryInParentAndChild

import org.springframework.beans.factory.support.RootBeanDefinition; //导入方法依赖的package包/类
@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());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:20,代码来源:AutowireWithExclusionTests.java

示例3: testAutowireBeanByTypeWithTwoPrimaryCandidates

import org.springframework.beans.factory.support.RootBeanDefinition; //导入方法依赖的package包/类
@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());
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:21,代码来源:DefaultListableBeanFactoryTests.java

示例4: postProcessBeanDefinitionRegistry

import org.springframework.beans.factory.support.RootBeanDefinition; //导入方法依赖的package包/类
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
    ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) registry;

    BeanDefinitionHolder dataSourceInfo = getDataSourceBeanDefinition(beanFactory, databaseAnnotation);

    RootBeanDefinition dataSourceDefinition = new RootBeanDefinition();
    dataSourceDefinition.setPrimary(dataSourceInfo.getBeanDefinition().isPrimary());

    BeanDefinitionHolder flywayInfo = getFlywayBeanDefinition(beanFactory, flywayAnnotation);
    if (flywayInfo == null) {
        dataSourceDefinition.setBeanClass(EmptyEmbeddedPostgresDataSourceFactoryBean.class);
    } else {
        String contextBeanName = flywayInfo.getBeanName() + "DataSourceContext";
        RootBeanDefinition dataSourceContextDefinition = new RootBeanDefinition();
        dataSourceContextDefinition.setBeanClass(FlywayDataSourceContext.class);
        registry.registerBeanDefinition(contextBeanName, dataSourceContextDefinition);

        dataSourceDefinition.setBeanClass(FlywayEmbeddedPostgresDataSourceFactoryBean.class);

        dataSourceDefinition.getConstructorArgumentValues()
                .addIndexedArgumentValue(0, flywayInfo.getBeanName());
        dataSourceDefinition.getConstructorArgumentValues()
                .addIndexedArgumentValue(1, new RuntimeBeanReference(contextBeanName));
    }

    logger.info("Replacing '{}' DataSource bean with embedded version", dataSourceInfo.getBeanName());
    registry.registerBeanDefinition(dataSourceInfo.getBeanName(), dataSourceDefinition);
}
 
开发者ID:zonkyio,项目名称:embedded-database-spring-test,代码行数:30,代码来源:EmbeddedPostgresContextCustomizerFactory.java

示例5: testGetBeanByTypeWithPrimary

import org.springframework.beans.factory.support.RootBeanDefinition; //导入方法依赖的package包/类
@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"));
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:12,代码来源:DefaultListableBeanFactoryTests.java

示例6: testGetBeanByTypeWithMultiplePrimary

import org.springframework.beans.factory.support.RootBeanDefinition; //导入方法依赖的package包/类
@Test
public void testGetBeanByTypeWithMultiplePrimary() throws Exception {
	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);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:14,代码来源:DefaultListableBeanFactoryTests.java

示例7: testGetBeanByTypePrimaryHasPrecedenceOverPriority

import org.springframework.beans.factory.support.RootBeanDefinition; //导入方法依赖的package包/类
@Test
public void testGetBeanByTypePrimaryHasPrecedenceOverPriority() throws Exception {
	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"));
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:13,代码来源:DefaultListableBeanFactoryTests.java

示例8: testGetBeanByTypeInstanceWithPrimary

import org.springframework.beans.factory.support.RootBeanDefinition; //导入方法依赖的package包/类
@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, 42);
	assertThat(bean.beanName, equalTo("bd2"));
	assertThat(bean.spouseAge, equalTo(42));
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:13,代码来源:DefaultListableBeanFactoryTests.java

示例9: testGetBeanByTypeInstanceWithMultiplePrimary

import org.springframework.beans.factory.support.RootBeanDefinition; //导入方法依赖的package包/类
@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);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:15,代码来源:DefaultListableBeanFactoryTests.java

示例10: testAutowireBeanByTypeWithTwoMatchesAndOnePrimary

import org.springframework.beans.factory.support.RootBeanDefinition; //导入方法依赖的package包/类
@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")));
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:14,代码来源:DefaultListableBeanFactoryTests.java

示例11: testAutowireBeanByTypePrimaryTakesPrecedenceOverPriority

import org.springframework.beans.factory.support.RootBeanDefinition; //导入方法依赖的package包/类
@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")));
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:15,代码来源:DefaultListableBeanFactoryTests.java

示例12: createScopedProxy

import org.springframework.beans.factory.support.RootBeanDefinition; //导入方法依赖的package包/类
/**
 * 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());
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:51,代码来源:ScopedProxyUtils.java


注:本文中的org.springframework.beans.factory.support.RootBeanDefinition.setPrimary方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。