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


Java DefaultListableBeanFactory.autowire方法代码示例

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


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

示例1: testAutowireWithSatisfiedJavaBeanDependency

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testAutowireWithSatisfiedJavaBeanDependency() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("name", "Rod");
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("rod", bd);
	assertEquals(1, lbf.getBeanDefinitionCount());
	// Depends on age, name and spouse (TestBean)
	Object registered = lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
	assertEquals(1, lbf.getBeanDefinitionCount());
	DependenciesBean kerry = (DependenciesBean) registered;
	TestBean rod = (TestBean) lbf.getBean("rod");
	assertSame(rod, kerry.getSpouse());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:17,代码来源:DefaultListableBeanFactoryTests.java

示例2: testAutowireWithTwoMatchesForConstructorDependency

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testAutowireWithTwoMatchesForConstructorDependency() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	lbf.registerBeanDefinition("rod", bd);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	lbf.registerBeanDefinition("rod2", bd2);
	try {
		lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
		assertTrue(ex.getMessage().contains("rod"));
		assertTrue(ex.getMessage().contains("rod2"));
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:18,代码来源:DefaultListableBeanFactoryTests.java

示例3: testAutowireBeanByTypeWithTwoMatches

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testAutowireBeanByTypeWithTwoMatches() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	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
		assertTrue(ex.getMessage().contains("test"));
		assertTrue(ex.getMessage().contains("spouse"));
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:18,代码来源:DefaultListableBeanFactoryTests.java

示例4: testAutowireBeanByTypeWithTwoMatchesAndParameterNameDiscovery

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testAutowireBeanByTypeWithTwoMatchesAndParameterNameDiscovery() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	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
		assertTrue(ex.getMessage().contains("test"));
		assertTrue(ex.getMessage().contains("spouse"));
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:18,代码来源:DefaultListableBeanFactoryTests.java

示例5: testAutowireBeanByTypeWithTwoPrimaryCandidates

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的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

示例6: testAutowireBeanByTypeWithIdenticalPriorityCandidates

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testAutowireBeanByTypeWithIdenticalPriorityCandidates() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
	RootBeanDefinition bd = new RootBeanDefinition(HighPriorityTestBean.class);
	RootBeanDefinition bd2 = new RootBeanDefinition(HighPriorityTestBean.class);
	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());
		assertTrue(ex.getMessage().contains("5")); // conflicting priority
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:21,代码来源:DefaultListableBeanFactoryTests.java

示例7: testAutowireWithNoDependencies

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testAutowireWithNoDependencies() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	lbf.registerBeanDefinition("rod", bd);
	assertEquals(1, lbf.getBeanDefinitionCount());
	Object registered = lbf.autowire(NoDependencies.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
	assertEquals(1, lbf.getBeanDefinitionCount());
	assertTrue(registered instanceof NoDependencies);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:11,代码来源:DefaultListableBeanFactoryTests.java

示例8: testAutowireWithSatisfiedConstructorDependency

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testAutowireWithSatisfiedConstructorDependency() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("name", "Rod");
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("rod", bd);
	assertEquals(1, lbf.getBeanDefinitionCount());
	Object registered = lbf.autowire(ConstructorDependency.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
	assertEquals(1, lbf.getBeanDefinitionCount());
	ConstructorDependency kerry = (ConstructorDependency) registered;
	TestBean rod = (TestBean) lbf.getBean("rod");
	assertSame(rod, kerry.spouse);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:16,代码来源:DefaultListableBeanFactoryTests.java

示例9: testAutowireBeanByTypePrimaryTakesPrecedenceOverPriority

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的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

示例10: testAutowireConstructor

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testAutowireConstructor() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	lbf.registerBeanDefinition("spouse", bd);
	ConstructorDependenciesBean bean = (ConstructorDependenciesBean)
			lbf.autowire(ConstructorDependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, true);
	Object spouse = lbf.getBean("spouse");
	assertTrue(bean.getSpouse1() == spouse);
	assertTrue(BeanFactoryUtils.beanOfType(lbf, TestBean.class) == spouse);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:12,代码来源:DefaultListableBeanFactoryTests.java

示例11: testAutowireBeanByName

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testAutowireBeanByName() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	lbf.registerBeanDefinition("spouse", bd);
	DependenciesBean bean = (DependenciesBean)
			lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);
	TestBean spouse = (TestBean) lbf.getBean("spouse");
	assertEquals(spouse, bean.getSpouse());
	assertTrue(BeanFactoryUtils.beanOfType(lbf, TestBean.class) == spouse);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:12,代码来源:DefaultListableBeanFactoryTests.java

示例12: testAutowireBeanByNameWithDependencyCheck

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testAutowireBeanByNameWithDependencyCheck() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	lbf.registerBeanDefinition("spous", bd);
	try {
		lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);
		fail("Should have thrown UnsatisfiedDependencyException");
	}
	catch (UnsatisfiedDependencyException ex) {
		// expected
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:14,代码来源:DefaultListableBeanFactoryTests.java

示例13: testAutowireBeanByNameWithNoDependencyCheck

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testAutowireBeanByNameWithNoDependencyCheck() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	lbf.registerBeanDefinition("spous", bd);
	DependenciesBean bean = (DependenciesBean)
			lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
	assertNull(bean.getSpouse());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:10,代码来源:DefaultListableBeanFactoryTests.java

示例14: testAutowireBeanByType

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testAutowireBeanByType() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	lbf.registerBeanDefinition("test", bd);
	DependenciesBean bean = (DependenciesBean)
			lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
	TestBean test = (TestBean) lbf.getBean("test");
	assertEquals(test, bean.getSpouse());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:11,代码来源:DefaultListableBeanFactoryTests.java

示例15: testAutowireBeanWithFactoryBeanByType

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
/**
 * Verifies that a dependency on a {@link FactoryBean} can be autowired
 * <em>by type</em>, specifically addressing the JIRA issue raised in <a
 * href="http://opensource.atlassian.com/projects/spring/browse/SPR-4040"
 * target="_blank">SPR-4040</a>.
 */
@Test
public void testAutowireBeanWithFactoryBeanByType() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd = new RootBeanDefinition(LazyInitFactory.class);
	lbf.registerBeanDefinition("factoryBean", bd);
	LazyInitFactory factoryBean = (LazyInitFactory) lbf.getBean("&factoryBean");
	assertNotNull("The FactoryBean should have been registered.", factoryBean);
	FactoryBeanDependentBean bean = (FactoryBeanDependentBean) lbf.autowire(FactoryBeanDependentBean.class,
			AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
	assertEquals("The FactoryBeanDependentBean should have been autowired 'by type' with the LazyInitFactory.",
			factoryBean, bean.getFactoryBean());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:19,代码来源:DefaultListableBeanFactoryTests.java


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