org.springframework.tests.sample.beans.LifecycleBean Java Examples

The following examples show how to use org.springframework.tests.sample.beans.LifecycleBean. 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: ContextLoaderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testContextLoaderListenerWithDefaultContext() {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
			"/org/springframework/web/context/WEB-INF/applicationContext.xml " +
			"/org/springframework/web/context/WEB-INF/context-addition.xml");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	listener.contextInitialized(event);
	String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
	WebApplicationContext context = (WebApplicationContext) sc.getAttribute(contextAttr);
	assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext);
	assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext);
	LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
	assertTrue("Has father", context.containsBean("father"));
	assertTrue("Has rod", context.containsBean("rod"));
	assertTrue("Has kerry", context.containsBean("kerry"));
	assertTrue("Not destroyed", !lb.isDestroyed());
	assertFalse(context.containsBean("beans1.bean1"));
	assertFalse(context.containsBean("beans1.bean2"));
	listener.contextDestroyed(event);
	assertTrue("Destroyed", lb.isDestroyed());
	assertNull(sc.getAttribute(contextAttr));
	assertNull(WebApplicationContextUtils.getWebApplicationContext(sc));
}
 
Example #2
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 #3
Source File: ContextLoaderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testContextLoaderListenerWithDefaultContext() {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
			"/org/springframework/web/context/WEB-INF/applicationContext.xml " +
			"/org/springframework/web/context/WEB-INF/context-addition.xml");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	listener.contextInitialized(event);
	String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
	WebApplicationContext context = (WebApplicationContext) sc.getAttribute(contextAttr);
	assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext);
	assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext);
	LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
	assertTrue("Has father", context.containsBean("father"));
	assertTrue("Has rod", context.containsBean("rod"));
	assertTrue("Has kerry", context.containsBean("kerry"));
	assertTrue("Not destroyed", !lb.isDestroyed());
	assertFalse(context.containsBean("beans1.bean1"));
	assertFalse(context.containsBean("beans1.bean2"));
	listener.contextDestroyed(event);
	assertTrue("Destroyed", lb.isDestroyed());
	assertNull(sc.getAttribute(contextAttr));
	assertNull(WebApplicationContextUtils.getWebApplicationContext(sc));
}
 
Example #4
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 #5
Source File: ContextLoaderTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testContextLoaderListenerWithDefaultContext() {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
			"/org/springframework/web/context/WEB-INF/applicationContext.xml " +
			"/org/springframework/web/context/WEB-INF/context-addition.xml");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	listener.contextInitialized(event);
	WebApplicationContext context = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext);
	assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext);
	LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
	assertTrue("Has father", context.containsBean("father"));
	assertTrue("Has rod", context.containsBean("rod"));
	assertTrue("Has kerry", context.containsBean("kerry"));
	assertTrue("Not destroyed", !lb.isDestroyed());
	assertFalse(context.containsBean("beans1.bean1"));
	assertFalse(context.containsBean("beans1.bean2"));
	listener.contextDestroyed(event);
	assertTrue("Destroyed", lb.isDestroyed());
	assertNull(sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
	assertNull(WebApplicationContextUtils.getWebApplicationContext(sc));
}
 
Example #6
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 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());
	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 #7
Source File: AbstractApplicationContextTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void closeTriggersDestroy() {
	LifecycleBean lb = (LifecycleBean) applicationContext.getBean("lifecycle");
	assertTrue("Not destroyed", !lb.isDestroyed());
	applicationContext.close();
	if (applicationContext.getParent() != null) {
		((ConfigurableApplicationContext) applicationContext.getParent()).close();
	}
	assertTrue("Destroyed", lb.isDestroyed());
	applicationContext.close();
	if (applicationContext.getParent() != null) {
		((ConfigurableApplicationContext) applicationContext.getParent()).close();
	}
	assertTrue("Destroyed", lb.isDestroyed());
}
 
Example #8
Source File: AbstractBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Test that InitializingBean/BeanFactoryAware/DisposableBean objects receive the
 * afterPropertiesSet() callback before BeanFactoryAware callbacks
 */
@Test
public void lifecycleCallbacks() {
	LifecycleBean lb = (LifecycleBean) getBeanFactory().getBean("lifecycle");
	assertEquals("lifecycle", lb.getBeanName());
	// The dummy business method will throw an exception if the
	// necessary callbacks weren't invoked in the right order.
	lb.businessMethod();
	assertTrue("Not destroyed", !lb.isDestroyed());
}
 
Example #9
Source File: AbstractApplicationContextTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void closeTriggersDestroy() {
	LifecycleBean lb = (LifecycleBean) applicationContext.getBean("lifecycle");
	assertTrue("Not destroyed", !lb.isDestroyed());
	applicationContext.close();
	if (applicationContext.getParent() != null) {
		((ConfigurableApplicationContext) applicationContext.getParent()).close();
	}
	assertTrue("Destroyed", lb.isDestroyed());
	applicationContext.close();
	if (applicationContext.getParent() != null) {
		((ConfigurableApplicationContext) applicationContext.getParent()).close();
	}
	assertTrue("Destroyed", lb.isDestroyed());
}
 
Example #10
Source File: AbstractBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Test that InitializingBean/BeanFactoryAware/DisposableBean objects receive the
 * afterPropertiesSet() callback before BeanFactoryAware callbacks
 */
@Test
public void lifecycleCallbacks() {
	LifecycleBean lb = (LifecycleBean) getBeanFactory().getBean("lifecycle");
	assertEquals("lifecycle", lb.getBeanName());
	// The dummy business method will throw an exception if the
	// necessary callbacks weren't invoked in the right order.
	lb.businessMethod();
	assertTrue("Not destroyed", !lb.isDestroyed());
}
 
Example #11
Source File: AbstractApplicationContextTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void closeTriggersDestroy() {
	LifecycleBean lb = (LifecycleBean) applicationContext.getBean("lifecycle");
	assertTrue("Not destroyed", !lb.isDestroyed());
	applicationContext.close();
	if (applicationContext.getParent() != null) {
		((ConfigurableApplicationContext) applicationContext.getParent()).close();
	}
	assertTrue("Destroyed", lb.isDestroyed());
	applicationContext.close();
	if (applicationContext.getParent() != null) {
		((ConfigurableApplicationContext) applicationContext.getParent()).close();
	}
	assertTrue("Destroyed", lb.isDestroyed());
}
 
Example #12
Source File: ContextLoaderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testContextLoaderWithDefaultContextAndParent() throws Exception {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
			"/org/springframework/web/context/WEB-INF/applicationContext.xml "
					+ "/org/springframework/web/context/WEB-INF/context-addition.xml");
	sc.addInitParameter(ContextLoader.LOCATOR_FACTORY_SELECTOR_PARAM,
			"classpath:org/springframework/web/context/ref1.xml");
	sc.addInitParameter(ContextLoader.LOCATOR_FACTORY_KEY_PARAM, "a.qualified.name.of.some.sort");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	listener.contextInitialized(event);
	WebApplicationContext context = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	assertTrue("Correct WebApplicationContext exposed in ServletContext",
			context instanceof XmlWebApplicationContext);
	LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
	assertTrue("Has father", context.containsBean("father"));
	assertTrue("Has rod", context.containsBean("rod"));
	assertTrue("Has kerry", context.containsBean("kerry"));
	assertTrue("Not destroyed", !lb.isDestroyed());
	assertTrue(context.containsBean("beans1.bean1"));
	assertTrue(context.isTypeMatch("beans1.bean1", org.springframework.beans.factory.access.TestBean.class));
	assertTrue(context.containsBean("beans1.bean2"));
	assertTrue(context.isTypeMatch("beans1.bean2", org.springframework.beans.factory.access.TestBean.class));
	listener.contextDestroyed(event);
	assertTrue("Destroyed", lb.isDestroyed());
}
 
Example #13
Source File: AbstractBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Test that InitializingBean/BeanFactoryAware/DisposableBean objects receive the
 * afterPropertiesSet() callback before BeanFactoryAware callbacks
 */
@Test
public void lifecycleCallbacks() {
	LifecycleBean lb = (LifecycleBean) getBeanFactory().getBean("lifecycle");
	assertEquals("lifecycle", lb.getBeanName());
	// The dummy business method will throw an exception if the
	// necessary callbacks weren't invoked in the right order.
	lb.businessMethod();
	assertTrue("Not destroyed", !lb.isDestroyed());
}
 
Example #14
Source File: XmlListableBeanFactoryTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void lifecycleMethods() {
	LifecycleBean bean = (LifecycleBean) getBeanFactory().getBean("lifecycle");
	bean.businessMethod();
}
 
Example #15
Source File: XmlListableBeanFactoryTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void lifecycleMethods() {
	LifecycleBean bean = (LifecycleBean) getBeanFactory().getBean("lifecycle");
	bean.businessMethod();
}
 
Example #16
Source File: XmlListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void lifecycleMethods() throws Exception {
	LifecycleBean bean = (LifecycleBean) getBeanFactory().getBean("lifecycle");
	bean.businessMethod();
}