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


Java DefaultListableBeanFactory.preInstantiateSingletons方法代码示例

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


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

示例1: byTypeAutowireWithPrimaryOverridingParentFactory

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

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testImplicitDependsOnCycle() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
	bd1.setDependsOn("tb2");
	lbf.registerBeanDefinition("tb1", bd1);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setDependsOn("tb3");
	lbf.registerBeanDefinition("tb2", bd2);
	RootBeanDefinition bd3 = new RootBeanDefinition(TestBean.class);
	bd3.setDependsOn("tb1");
	lbf.registerBeanDefinition("tb3", bd3);
	try {
		lbf.preInstantiateSingletons();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		assertTrue(ex.getMessage().contains("Circular"));
		assertTrue(ex.getMessage().contains("'tb3'"));
		assertTrue(ex.getMessage().contains("'tb1'"));
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:24,代码来源:DefaultListableBeanFactoryTests.java

示例3: testInitializingBeanAndInitMethod

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
/**
 * Check that InitializingBean method is called first.
 */
@Test
public void testInitializingBeanAndInitMethod() throws Exception {
	InitAndIB.constructed = false;
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(INITIALIZERS_CONTEXT);
	assertFalse(InitAndIB.constructed);
	xbf.preInstantiateSingletons();
	assertFalse(InitAndIB.constructed);
	InitAndIB iib = (InitAndIB) xbf.getBean("init-and-ib");
	assertTrue(InitAndIB.constructed);
	assertTrue(iib.afterPropertiesSetInvoked && iib.initMethodInvoked);
	assertTrue(!iib.destroyed && !iib.customDestroyed);
	xbf.destroySingletons();
	assertTrue(iib.destroyed && iib.customDestroyed);
	xbf.destroySingletons();
	assertTrue(iib.destroyed && iib.customDestroyed);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:21,代码来源:XmlBeanFactoryTests.java

示例4: testExtensiveCircularReference

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testExtensiveCircularReference() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	for (int i = 0; i < 1000; i++) {
		MutablePropertyValues pvs = new MutablePropertyValues();
		pvs.addPropertyValue(new PropertyValue("spouse", new RuntimeBeanReference("bean" + (i < 99 ? i + 1 : 0))));
		RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
		bd.setPropertyValues(pvs);
		lbf.registerBeanDefinition("bean" + i, bd);
	}
	lbf.preInstantiateSingletons();
	for (int i = 0; i < 1000; i++) {
		TestBean bean = (TestBean) lbf.getBean("bean" + i);
		TestBean otherBean = (TestBean) lbf.getBean("bean" + (i < 99 ? i + 1 : 0));
		assertTrue(bean.getSpouse() == otherBean);
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:18,代码来源:DefaultListableBeanFactoryTests.java

示例5: testWithRequiredPropertyOmitted

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testWithRequiredPropertyOmitted() {
	try {
		DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
		BeanDefinition beanDef = BeanDefinitionBuilder
			.genericBeanDefinition(RequiredTestBean.class)
			.addPropertyValue("name", "Rob Harrop")
			.addPropertyValue("favouriteColour", "Blue")
			.addPropertyValue("jobTitle", "Grand Poobah")
			.getBeanDefinition();
		factory.registerBeanDefinition("testBean", beanDef);
		factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
		factory.preInstantiateSingletons();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		String message = ex.getCause().getMessage();
		assertTrue(message.contains("Property"));
		assertTrue(message.contains("age"));
		assertTrue(message.contains("testBean"));
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:23,代码来源:RequiredAnnotationBeanPostProcessorTests.java

示例6: testWithThreeRequiredPropertiesOmitted

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testWithThreeRequiredPropertiesOmitted() {
	try {
		DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
		BeanDefinition beanDef = BeanDefinitionBuilder
			.genericBeanDefinition(RequiredTestBean.class)
			.addPropertyValue("name", "Rob Harrop")
			.getBeanDefinition();
		factory.registerBeanDefinition("testBean", beanDef);
		factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
		factory.preInstantiateSingletons();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		String message = ex.getCause().getMessage();
		assertTrue(message.contains("Properties"));
		assertTrue(message.contains("age"));
		assertTrue(message.contains("favouriteColour"));
		assertTrue(message.contains("jobTitle"));
		assertTrue(message.contains("testBean"));
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:23,代码来源:RequiredAnnotationBeanPostProcessorTests.java

示例7: testWithAllRequiredPropertiesSpecified

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testWithAllRequiredPropertiesSpecified() {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	BeanDefinition beanDef = BeanDefinitionBuilder
		.genericBeanDefinition(RequiredTestBean.class)
		.addPropertyValue("age", "24")
		.addPropertyValue("favouriteColour", "Blue")
		.addPropertyValue("jobTitle", "Grand Poobah")
		.getBeanDefinition();
	factory.registerBeanDefinition("testBean", beanDef);
	factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
	factory.preInstantiateSingletons();
	RequiredTestBean bean = (RequiredTestBean) factory.getBean("testBean");
	assertEquals(24, bean.getAge());
	assertEquals("Blue", bean.getFavouriteColour());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:17,代码来源:RequiredAnnotationBeanPostProcessorTests.java

示例8: testWithStaticFactoryMethod

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testWithStaticFactoryMethod() {
	try {
		DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
		BeanDefinition beanDef = BeanDefinitionBuilder
				.genericBeanDefinition(RequiredTestBean.class)
				.setFactoryMethod("create")
				.addPropertyValue("name", "Rob Harrop")
				.addPropertyValue("favouriteColour", "Blue")
				.addPropertyValue("jobTitle", "Grand Poobah")
				.getBeanDefinition();
		factory.registerBeanDefinition("testBean", beanDef);
		factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
		factory.preInstantiateSingletons();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		String message = ex.getCause().getMessage();
		assertTrue(message.contains("Property"));
		assertTrue(message.contains("age"));
		assertTrue(message.contains("testBean"));
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:24,代码来源:RequiredAnnotationBeanPostProcessorTests.java

示例9: testWithStaticFactoryMethodAndRequiredPropertiesSpecified

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testWithStaticFactoryMethodAndRequiredPropertiesSpecified() {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	BeanDefinition beanDef = BeanDefinitionBuilder
			.genericBeanDefinition(RequiredTestBean.class)
			.setFactoryMethod("create")
			.addPropertyValue("age", "24")
			.addPropertyValue("favouriteColour", "Blue")
			.addPropertyValue("jobTitle", "Grand Poobah")
			.getBeanDefinition();
	factory.registerBeanDefinition("testBean", beanDef);
	factory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());
	factory.preInstantiateSingletons();
	RequiredTestBean bean = (RequiredTestBean) factory.getBean("testBean");
	assertEquals(24, bean.getAge());
	assertEquals("Blue", bean.getFavouriteColour());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:18,代码来源:RequiredAnnotationBeanPostProcessorTests.java

示例10: testInstanceFactoryMethodWithoutArgs

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testInstanceFactoryMethodWithoutArgs() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
	reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));

	InstanceFactory.count = 0;
	xbf.preInstantiateSingletons();
	assertEquals(1, InstanceFactory.count);
	FactoryMethods fm = (FactoryMethods) xbf.getBean("instanceFactoryMethodWithoutArgs");
	assertEquals("instanceFactory", fm.getTestBean().getName());
	assertEquals(1, InstanceFactory.count);
	FactoryMethods fm2 = (FactoryMethods) xbf.getBean("instanceFactoryMethodWithoutArgs");
	assertEquals("instanceFactory", fm2.getTestBean().getName());
	assertSame(fm2, fm);
	assertEquals(1, InstanceFactory.count);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:18,代码来源:FactoryMethodTests.java

示例11: testDependsOnCycle

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testDependsOnCycle() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
	bd1.setDependsOn("tb2");
	lbf.registerBeanDefinition("tb1", bd1);
	RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
	bd2.setDependsOn("tb1");
	lbf.registerBeanDefinition("tb2", bd2);
	try {
		lbf.preInstantiateSingletons();
		fail("Should have thrown BeanCreationException");
	}
	catch (BeanCreationException ex) {
		// expected
		assertTrue(ex.getMessage().contains("Circular"));
		assertTrue(ex.getMessage().contains("'tb2'"));
		assertTrue(ex.getMessage().contains("'tb1'"));
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:21,代码来源:DefaultListableBeanFactoryTests.java

示例12: testUnreferencedSingletonWasInstantiated

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testUnreferencedSingletonWasInstantiated() {
	KnowsIfInstantiated.clearInstantiationRecord();
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();
	p.setProperty("x1.(class)", KnowsIfInstantiated.class.getName());
	assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated());
	(new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
	lbf.preInstantiateSingletons();
	assertTrue("singleton was instantiated", KnowsIfInstantiated.wasInstantiated());
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:12,代码来源:DefaultListableBeanFactoryTests.java

示例13: testDestroyMethodOnInnerBeanAsPrototype

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testDestroyMethodOnInnerBeanAsPrototype() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition innerBd = new RootBeanDefinition(BeanWithDestroyMethod.class);
	innerBd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	innerBd.setDestroyMethodName("close");
	RootBeanDefinition bd = new RootBeanDefinition(BeanWithDestroyMethod.class);
	bd.setDestroyMethodName("close");
	bd.getPropertyValues().add("inner", innerBd);
	lbf.registerBeanDefinition("test", bd);
	BeanWithDestroyMethod.closeCount = 0;
	lbf.preInstantiateSingletons();
	lbf.destroySingletons();
	assertEquals("Destroy methods invoked", 1, BeanWithDestroyMethod.closeCount);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:16,代码来源:DefaultListableBeanFactoryTests.java

示例14: testDefaultLazyInit

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testDefaultLazyInit() throws Exception {
	InitAndIB.constructed = false;
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(DEFAULT_LAZY_CONTEXT);
	assertFalse(InitAndIB.constructed);
	xbf.preInstantiateSingletons();
	assertTrue(InitAndIB.constructed);
	try {
		xbf.getBean("lazy-and-bad");
	}
	catch (BeanCreationException ex) {
		assertTrue(ex.getCause() instanceof IOException);
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:16,代码来源:XmlBeanFactoryTests.java

示例15: testDestroyMethodOnInnerBean

import org.springframework.beans.factory.support.DefaultListableBeanFactory; //导入方法依赖的package包/类
@Test
public void testDestroyMethodOnInnerBean() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition innerBd = new RootBeanDefinition(BeanWithDestroyMethod.class);
	innerBd.setDestroyMethodName("close");
	RootBeanDefinition bd = new RootBeanDefinition(BeanWithDestroyMethod.class);
	bd.setDestroyMethodName("close");
	bd.getPropertyValues().add("inner", innerBd);
	lbf.registerBeanDefinition("test", bd);
	BeanWithDestroyMethod.closeCount = 0;
	lbf.preInstantiateSingletons();
	lbf.destroySingletons();
	assertEquals("Destroy methods invoked", 2, BeanWithDestroyMethod.closeCount);
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:15,代码来源:DefaultListableBeanFactoryTests.java


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