Java Code Examples for org.springframework.beans.factory.support.DefaultListableBeanFactory#freezeConfiguration()

The following examples show how to use org.springframework.beans.factory.support.DefaultListableBeanFactory#freezeConfiguration() . 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 spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Test that by-type bean lookup caching is working effectively by searching for a
 * bean of type B 10K times within a container having 1K additional beans of type A.
 * Prior to by-type caching, each bean lookup would traverse the entire container
 * (all 1001 beans), performing expensive assignability checks, etc. Now these
 * operations are necessary only once, providing a dramatic performance improvement.
 * On load-free modern hardware (e.g. an 8-core MPB), this method should complete well
 * under the 1000 ms timeout, usually ~= 300ms. With caching removed and on the same
 * hardware the method will take ~13000 ms. See SPR-6870.
 */
@Test(timeout = 1000)
public void testByTypeLookupIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();

	for (int i = 0; i < 1000; i++) {
		bf.registerBeanDefinition("a" + i, new RootBeanDefinition(A.class));
	}
	bf.registerBeanDefinition("b", new RootBeanDefinition(B.class));

	bf.freezeConfiguration();

	for (int i = 0; i < 10000; i++) {
		bf.getBean(B.class);
	}
}
 
Example 2
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSingletonLookupByNameIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	Assume.notLogging(factoryLog);
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class));
	lbf.freezeConfiguration();
	StopWatch sw = new StopWatch();
	sw.start("singleton");
	for (int i = 0; i < 1000000; i++) {
		lbf.getBean("test");
	}
	sw.stop();
	// System.out.println(sw.getTotalTimeMillis());
	assertTrue("Singleton lookup took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 1000);
}
 
Example 3
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Test that by-type bean lookup caching is working effectively by searching for a
 * bean of type B 10K times within a container having 1K additional beans of type A.
 * Prior to by-type caching, each bean lookup would traverse the entire container
 * (all 1001 beans), performing expensive assignability checks, etc. Now these
 * operations are necessary only once, providing a dramatic performance improvement.
 * On load-free modern hardware (e.g. an 8-core MPB), this method should complete well
 * under the 1000 ms timeout, usually ~= 300ms. With caching removed and on the same
 * hardware the method will take ~13000 ms. See SPR-6870.
 */
@Test(timeout = 1000)
public void testByTypeLookupIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();

	for (int i = 0; i < 1000; i++) {
		bf.registerBeanDefinition("a" + i, new RootBeanDefinition(A.class));
	}
	bf.registerBeanDefinition("b", new RootBeanDefinition(B.class));

	bf.freezeConfiguration();

	for (int i = 0; i < 10000; i++) {
		bf.getBean(B.class);
	}
}
 
Example 4
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testPrototypeCreationWithPropertiesIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	Assume.notLogging(factoryLog);
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	rbd.getPropertyValues().add("name", "juergen");
	rbd.getPropertyValues().add("age", "99");
	lbf.registerBeanDefinition("test", rbd);
	lbf.freezeConfiguration();
	StopWatch sw = new StopWatch();
	sw.start("prototype");
	for (int i = 0; i < 100000; i++) {
		TestBean tb = (TestBean) lbf.getBean("test");
		assertEquals("juergen", tb.getName());
		assertEquals(99, tb.getAge());
	}
	sw.stop();
	// System.out.println(sw.getTotalTimeMillis());
	assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000);
}
 
Example 5
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testPrototypeCreationWithResolvedConstructorArgumentsIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	Assume.notLogging(factoryLog);
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("spouse"));
	lbf.registerBeanDefinition("test", rbd);
	lbf.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
	lbf.freezeConfiguration();
	TestBean spouse = (TestBean) lbf.getBean("spouse");
	StopWatch sw = new StopWatch();
	sw.start("prototype");
	for (int i = 0; i < 100000; i++) {
		TestBean tb = (TestBean) lbf.getBean("test");
		assertSame(spouse, tb.getSpouse());
	}
	sw.stop();
	// System.out.println(sw.getTotalTimeMillis());
	assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000);
}
 
Example 6
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@Ignore  // TODO re-enable when ConstructorResolver TODO sorted out
public void testPrototypeCreationWithConstructorArgumentsIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	Assume.notLogging(factoryLog);
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	rbd.getConstructorArgumentValues().addGenericArgumentValue("juergen");
	rbd.getConstructorArgumentValues().addGenericArgumentValue("99");
	lbf.registerBeanDefinition("test", rbd);
	lbf.freezeConfiguration();
	StopWatch sw = new StopWatch();
	sw.start("prototype");
	for (int i = 0; i < 100000; i++) {
		TestBean tb = (TestBean) lbf.getBean("test");
		assertEquals("juergen", tb.getName());
		assertEquals(99, tb.getAge());
	}
	sw.stop();
	// System.out.println(sw.getTotalTimeMillis());
	assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 3000);
}
 
Example 7
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testPrototypeCreationWithDependencyCheckIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	Assume.notLogging(factoryLog);
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition rbd = new RootBeanDefinition(LifecycleBean.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	rbd.setDependencyCheck(RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
	lbf.registerBeanDefinition("test", rbd);
	lbf.addBeanPostProcessor(new LifecycleBean.PostProcessor());
	lbf.freezeConfiguration();
	StopWatch sw = new StopWatch();
	sw.start("prototype");
	for (int i = 0; i < 100000; i++) {
		lbf.getBean("test");
	}
	sw.stop();
	// System.out.println(sw.getTotalTimeMillis());
	assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 3000);
}
 
Example 8
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Test that by-type bean lookup caching is working effectively by searching for a
 * bean of type B 10K times within a container having 1K additional beans of type A.
 * Prior to by-type caching, each bean lookup would traverse the entire container
 * (all 1001 beans), performing expensive assignability checks, etc. Now these
 * operations are necessary only once, providing a dramatic performance improvement.
 * On load-free modern hardware (e.g. an 8-core MPB), this method should complete well
 * under the 1000 ms timeout, usually ~= 300ms. With caching removed and on the same
 * hardware the method will take ~13000 ms. See SPR-6870.
 */
@Test(timeout = 1000)
public void testByTypeLookupIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();

	for (int i = 0; i < 1000; i++) {
		bf.registerBeanDefinition("a" + i, new RootBeanDefinition(A.class));
	}
	bf.registerBeanDefinition("b", new RootBeanDefinition(B.class));

	bf.freezeConfiguration();

	for (int i = 0; i < 10000; i++) {
		bf.getBean(B.class);
	}
}
 
Example 9
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSingletonLookupByTypeIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	Assume.notLogging(factoryLog);
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class));
	lbf.freezeConfiguration();
	StopWatch sw = new StopWatch();
	sw.start("singleton");
	for (int i = 0; i < 1000000; i++) {
		lbf.getBean(TestBean.class);
	}
	sw.stop();
	// System.out.println(sw.getTotalTimeMillis());
	assertTrue("Singleton lookup took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 1000);
}
 
Example 10
Source File: ConfigurationClassProcessingTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new {@link BeanFactory}, populates it with a {@link BeanDefinition} for
 * each of the given {@link Configuration} <var>configClasses</var>, and then
 * post-processes the factory using JavaConfig's {@link ConfigurationClassPostProcessor}.
 * When complete, the factory is ready to service requests for any {@link Bean} methods
 * declared by <var>configClasses</var>.
 */
private ListableBeanFactory initBeanFactory(Class<?>... configClasses) {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	for (Class<?> configClass : configClasses) {
		String configBeanName = configClass.getName();
		factory.registerBeanDefinition(configBeanName, new RootBeanDefinition(configClass));
	}
	ConfigurationClassPostProcessor ccpp = new ConfigurationClassPostProcessor();
	ccpp.postProcessBeanDefinitionRegistry(factory);
	ccpp.postProcessBeanFactory(factory);
	RequiredAnnotationBeanPostProcessor rapp = new RequiredAnnotationBeanPostProcessor();
	rapp.setBeanFactory(factory);
	factory.addBeanPostProcessor(rapp);
	factory.freezeConfiguration();
	return factory;
}
 
Example 11
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSingletonLookupByTypeIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	Assume.notLogging(factoryLog);
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class));
	lbf.freezeConfiguration();
	StopWatch sw = new StopWatch();
	sw.start("singleton");
	for (int i = 0; i < 1000000; i++) {
		lbf.getBean(TestBean.class);
	}
	sw.stop();
	// System.out.println(sw.getTotalTimeMillis());
	assertTrue("Singleton lookup took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 1000);
}
 
Example 12
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSingletonLookupByNameIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	Assume.notLogging(factoryLog);
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class));
	lbf.freezeConfiguration();
	StopWatch sw = new StopWatch();
	sw.start("singleton");
	for (int i = 0; i < 1000000; i++) {
		lbf.getBean("test");
	}
	sw.stop();
	// System.out.println(sw.getTotalTimeMillis());
	assertTrue("Singleton lookup took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 1000);
}
 
Example 13
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPrototypeCreationWithResolvedPropertiesIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	Assume.notLogging(factoryLog);
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse"));
	lbf.registerBeanDefinition("test", rbd);
	lbf.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
	lbf.freezeConfiguration();
	TestBean spouse = (TestBean) lbf.getBean("spouse");
	StopWatch sw = new StopWatch();
	sw.start("prototype");
	for (int i = 0; i < 100000; i++) {
		TestBean tb = (TestBean) lbf.getBean("test");
		assertSame(spouse, tb.getSpouse());
	}
	sw.stop();
	// System.out.println(sw.getTotalTimeMillis());
	assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000);
}
 
Example 14
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPrototypeCreationWithPropertiesIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	Assume.notLogging(factoryLog);
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	rbd.getPropertyValues().add("name", "juergen");
	rbd.getPropertyValues().add("age", "99");
	lbf.registerBeanDefinition("test", rbd);
	lbf.freezeConfiguration();
	StopWatch sw = new StopWatch();
	sw.start("prototype");
	for (int i = 0; i < 100000; i++) {
		TestBean tb = (TestBean) lbf.getBean("test");
		assertEquals("juergen", tb.getName());
		assertEquals(99, tb.getAge());
	}
	sw.stop();
	// System.out.println(sw.getTotalTimeMillis());
	assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000);
}
 
Example 15
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPrototypeCreationWithResolvedConstructorArgumentsIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	Assume.notLogging(factoryLog);
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	rbd.getConstructorArgumentValues().addGenericArgumentValue(new RuntimeBeanReference("spouse"));
	lbf.registerBeanDefinition("test", rbd);
	lbf.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
	lbf.freezeConfiguration();
	TestBean spouse = (TestBean) lbf.getBean("spouse");
	StopWatch sw = new StopWatch();
	sw.start("prototype");
	for (int i = 0; i < 100000; i++) {
		TestBean tb = (TestBean) lbf.getBean("test");
		assertSame(spouse, tb.getSpouse());
	}
	sw.stop();
	// System.out.println(sw.getTotalTimeMillis());
	assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000);
}
 
Example 16
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPrototypeCreationWithDependencyCheckIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	Assume.notLogging(factoryLog);
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition rbd = new RootBeanDefinition(LifecycleBean.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	rbd.setDependencyCheck(RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
	lbf.registerBeanDefinition("test", rbd);
	lbf.addBeanPostProcessor(new LifecycleBean.PostProcessor());
	lbf.freezeConfiguration();
	StopWatch sw = new StopWatch();
	sw.start("prototype");
	for (int i = 0; i < 100000; i++) {
		lbf.getBean("test");
	}
	sw.stop();
	// System.out.println(sw.getTotalTimeMillis());
	assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 3000);
}
 
Example 17
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPrototypeCreationIsFastEnough() {
	Assume.group(TestGroup.PERFORMANCE);
	Assume.notLogging(factoryLog);
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
	rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	lbf.registerBeanDefinition("test", rbd);
	lbf.freezeConfiguration();
	StopWatch sw = new StopWatch();
	sw.start("prototype");
	for (int i = 0; i < 100000; i++) {
		lbf.getBean("test");
	}
	sw.stop();
	// System.out.println(sw.getTotalTimeMillis());
	assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 3000);
}
 
Example 18
Source File: ConfigurationClassProcessingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Creates a new {@link BeanFactory}, populates it with a {@link BeanDefinition}
 * for each of the given {@link Configuration} {@code configClasses}, and then
 * post-processes the factory using JavaConfig's {@link ConfigurationClassPostProcessor}.
 * When complete, the factory is ready to service requests for any {@link Bean} methods
 * declared by {@code configClasses}.
 */
private DefaultListableBeanFactory initBeanFactory(Class<?>... configClasses) {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	for (Class<?> configClass : configClasses) {
		String configBeanName = configClass.getName();
		factory.registerBeanDefinition(configBeanName, new RootBeanDefinition(configClass));
	}
	ConfigurationClassPostProcessor ccpp = new ConfigurationClassPostProcessor();
	ccpp.postProcessBeanDefinitionRegistry(factory);
	ccpp.postProcessBeanFactory(factory);
	factory.freezeConfiguration();
	return factory;
}
 
Example 19
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetTypeWorksAfterParentChildMerging() {
	RootBeanDefinition parentDefinition = new RootBeanDefinition(TestBean.class);
	ChildBeanDefinition childDefinition = new ChildBeanDefinition("parent", DerivedTestBean.class, null, null);

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition("parent", parentDefinition);
	factory.registerBeanDefinition("child", childDefinition);
	factory.freezeConfiguration();

	assertEquals(TestBean.class, factory.getType("parent"));
	assertEquals(DerivedTestBean.class, factory.getType("child"));
}
 
Example 20
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testGetTypeWorksAfterParentChildMerging() {
	RootBeanDefinition parentDefinition = new RootBeanDefinition(TestBean.class);
	ChildBeanDefinition childDefinition = new ChildBeanDefinition("parent", DerivedTestBean.class, null, null);

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition("parent", parentDefinition);
	factory.registerBeanDefinition("child", childDefinition);
	factory.freezeConfiguration();

	assertEquals(TestBean.class, factory.getType("parent"));
	assertEquals(DerivedTestBean.class, factory.getType("child"));
}