org.springframework.context.Lifecycle#start ( )源码实例Demo

下面列出了org.springframework.context.Lifecycle#start ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
 * Start the specified bean as part of the given set of Lifecycle beans,
 * making sure that any beans that it depends on are started first.
 * @param lifecycleBeans a Map with bean name as key and Lifecycle instance as value
 * @param beanName the name of the bean to start
 */
private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly) {
	Lifecycle bean = lifecycleBeans.remove(beanName);
	if (bean != null && bean != this) {
		String[] dependenciesForBean = getBeanFactory().getDependenciesForBean(beanName);
		for (String dependency : dependenciesForBean) {
			doStart(lifecycleBeans, dependency, autoStartupOnly);
		}
		if (!bean.isRunning() &&
				(!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
			if (logger.isTraceEnabled()) {
				logger.trace("Starting bean '" + beanName + "' of type [" + bean.getClass().getName() + "]");
			}
			try {
				bean.start();
			}
			catch (Throwable ex) {
				throw new ApplicationContextException("Failed to start bean '" + beanName + "'", ex);
			}
			if (logger.isDebugEnabled()) {
				logger.debug("Successfully started bean '" + beanName + "'");
			}
		}
	}
}
 
/**
 * Start the specified bean as part of the given set of Lifecycle beans,
 * making sure that any beans that it depends on are started first.
 * @param lifecycleBeans a Map with bean name as key and Lifecycle instance as value
 * @param beanName the name of the bean to start
 */
private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly) {
	Lifecycle bean = lifecycleBeans.remove(beanName);
	if (bean != null && bean != this) {
		String[] dependenciesForBean = getBeanFactory().getDependenciesForBean(beanName);
		for (String dependency : dependenciesForBean) {
			doStart(lifecycleBeans, dependency, autoStartupOnly);
		}
		if (!bean.isRunning() &&
				(!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
			if (logger.isTraceEnabled()) {
				logger.trace("Starting bean '" + beanName + "' of type [" + bean.getClass().getName() + "]");
			}
			try {
				bean.start();
			}
			catch (Throwable ex) {
				throw new ApplicationContextException("Failed to start bean '" + beanName + "'", ex);
			}
			if (logger.isDebugEnabled()) {
				logger.debug("Successfully started bean '" + beanName + "'");
			}
		}
	}
}
 
源代码3 项目: lams   文件: DefaultLifecycleProcessor.java
/**
 * Start the specified bean as part of the given set of Lifecycle beans,
 * making sure that any beans that it depends on are started first.
 * @param lifecycleBeans Map with bean name as key and Lifecycle instance as value
 * @param beanName the name of the bean to start
 */
private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly) {
	Lifecycle bean = lifecycleBeans.remove(beanName);
	if (bean != null && !this.equals(bean)) {
		String[] dependenciesForBean = this.beanFactory.getDependenciesForBean(beanName);
		for (String dependency : dependenciesForBean) {
			doStart(lifecycleBeans, dependency, autoStartupOnly);
		}
		if (!bean.isRunning() &&
				(!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
			if (logger.isDebugEnabled()) {
				logger.debug("Starting bean '" + beanName + "' of type [" + bean.getClass() + "]");
			}
			try {
				bean.start();
			}
			catch (Throwable ex) {
				throw new ApplicationContextException("Failed to start bean '" + beanName + "'", ex);
			}
			if (logger.isDebugEnabled()) {
				logger.debug("Successfully started bean '" + beanName + "'");
			}
		}
	}
}
 
/**
 * Start the specified bean as part of the given set of Lifecycle beans,
 * making sure that any beans that it depends on are started first.
 * @param lifecycleBeans Map with bean name as key and Lifecycle instance as value
 * @param beanName the name of the bean to start
 */
private void doStart(Map<String, ? extends Lifecycle> lifecycleBeans, String beanName, boolean autoStartupOnly) {
	Lifecycle bean = lifecycleBeans.remove(beanName);
	if (bean != null && !this.equals(bean)) {
		String[] dependenciesForBean = this.beanFactory.getDependenciesForBean(beanName);
		for (String dependency : dependenciesForBean) {
			doStart(lifecycleBeans, dependency, autoStartupOnly);
		}
		if (!bean.isRunning() &&
				(!autoStartupOnly || !(bean instanceof SmartLifecycle) || ((SmartLifecycle) bean).isAutoStartup())) {
			if (logger.isDebugEnabled()) {
				logger.debug("Starting bean '" + beanName + "' of type [" + bean.getClass() + "]");
			}
			try {
				bean.start();
			}
			catch (Throwable ex) {
				throw new ApplicationContextException("Failed to start bean '" + beanName + "'", ex);
			}
			if (logger.isDebugEnabled()) {
				logger.debug("Successfully started bean '" + beanName + "'");
			}
		}
	}
}
 
@Test
public void singleLifecycleShutdown() throws Exception {
	CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<>();
	Lifecycle bean = new TestLifecycleBean(null, stoppedBeans);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("bean", bean);
	context.refresh();
	assertFalse(bean.isRunning());
	bean.start();
	assertTrue(bean.isRunning());
	context.stop();
	assertEquals(1, stoppedBeans.size());
	assertFalse(bean.isRunning());
	assertEquals(bean, stoppedBeans.get(0));
}
 
源代码6 项目: spring-analysis-note   文件: SockJsClient.java
@Override
public void start() {
	if (!isRunning()) {
		this.running = true;
		for (Transport transport : this.transports) {
			if (transport instanceof Lifecycle) {
				Lifecycle lifecycle = (Lifecycle) transport;
				if (!lifecycle.isRunning()) {
					lifecycle.start();
				}
			}
		}
	}
}
 
@Test
public void singleLifecycleShutdown() throws Exception {
	CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<>();
	Lifecycle bean = new TestLifecycleBean(null, stoppedBeans);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("bean", bean);
	context.refresh();
	assertFalse(bean.isRunning());
	bean.start();
	assertTrue(bean.isRunning());
	context.stop();
	assertEquals(1, stoppedBeans.size());
	assertFalse(bean.isRunning());
	assertEquals(bean, stoppedBeans.get(0));
}
 
源代码8 项目: java-technology-stack   文件: SockJsClient.java
@Override
public void start() {
	if (!isRunning()) {
		this.running = true;
		for (Transport transport : this.transports) {
			if (transport instanceof Lifecycle) {
				Lifecycle lifecycle = (Lifecycle) transport;
				if (!lifecycle.isRunning()) {
					lifecycle.start();
				}
			}
		}
	}
}
 
@Test
public void singleLifecycleShutdown() throws Exception {
	CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<Lifecycle>();
	Lifecycle bean = new TestLifecycleBean(null, stoppedBeans);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("bean", bean);
	context.refresh();
	assertFalse(bean.isRunning());
	bean.start();
	assertTrue(bean.isRunning());
	context.stop();
	assertEquals(1, stoppedBeans.size());
	assertFalse(bean.isRunning());
	assertEquals(bean, stoppedBeans.get(0));
}
 
@Test
public void mixedShutdown() throws Exception {
	CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<>();
	Lifecycle bean1 = TestLifecycleBean.forShutdownTests(stoppedBeans);
	Lifecycle bean2 = TestSmartLifecycleBean.forShutdownTests(500, 200, stoppedBeans);
	Lifecycle bean3 = TestSmartLifecycleBean.forShutdownTests(Integer.MAX_VALUE, 100, stoppedBeans);
	Lifecycle bean4 = TestLifecycleBean.forShutdownTests(stoppedBeans);
	Lifecycle bean5 = TestSmartLifecycleBean.forShutdownTests(1, 200, stoppedBeans);
	Lifecycle bean6 = TestSmartLifecycleBean.forShutdownTests(-1, 100, stoppedBeans);
	Lifecycle bean7 = TestSmartLifecycleBean.forShutdownTests(Integer.MIN_VALUE, 300, stoppedBeans);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("bean1", bean1);
	context.getBeanFactory().registerSingleton("bean2", bean2);
	context.getBeanFactory().registerSingleton("bean3", bean3);
	context.getBeanFactory().registerSingleton("bean4", bean4);
	context.getBeanFactory().registerSingleton("bean5", bean5);
	context.getBeanFactory().registerSingleton("bean6", bean6);
	context.getBeanFactory().registerSingleton("bean7", bean7);
	context.refresh();
	assertTrue(bean2.isRunning());
	assertTrue(bean3.isRunning());
	assertTrue(bean5.isRunning());
	assertTrue(bean6.isRunning());
	assertTrue(bean7.isRunning());
	assertFalse(bean1.isRunning());
	assertFalse(bean4.isRunning());
	bean1.start();
	bean4.start();
	assertTrue(bean1.isRunning());
	assertTrue(bean4.isRunning());
	context.stop();
	assertFalse(bean1.isRunning());
	assertFalse(bean2.isRunning());
	assertFalse(bean3.isRunning());
	assertFalse(bean4.isRunning());
	assertFalse(bean5.isRunning());
	assertFalse(bean6.isRunning());
	assertFalse(bean7.isRunning());
	assertEquals(7, stoppedBeans.size());
	assertEquals(Integer.MAX_VALUE, getPhase(stoppedBeans.get(0)));
	assertEquals(500, getPhase(stoppedBeans.get(1)));
	assertEquals(1, getPhase(stoppedBeans.get(2)));
	assertEquals(0, getPhase(stoppedBeans.get(3)));
	assertEquals(0, getPhase(stoppedBeans.get(4)));
	assertEquals(-1, getPhase(stoppedBeans.get(5)));
	assertEquals(Integer.MIN_VALUE, getPhase(stoppedBeans.get(6)));
}
 
@Test
public void mixedShutdown() throws Exception {
	CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<>();
	Lifecycle bean1 = TestLifecycleBean.forShutdownTests(stoppedBeans);
	Lifecycle bean2 = TestSmartLifecycleBean.forShutdownTests(500, 200, stoppedBeans);
	Lifecycle bean3 = TestSmartLifecycleBean.forShutdownTests(Integer.MAX_VALUE, 100, stoppedBeans);
	Lifecycle bean4 = TestLifecycleBean.forShutdownTests(stoppedBeans);
	Lifecycle bean5 = TestSmartLifecycleBean.forShutdownTests(1, 200, stoppedBeans);
	Lifecycle bean6 = TestSmartLifecycleBean.forShutdownTests(-1, 100, stoppedBeans);
	Lifecycle bean7 = TestSmartLifecycleBean.forShutdownTests(Integer.MIN_VALUE, 300, stoppedBeans);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("bean1", bean1);
	context.getBeanFactory().registerSingleton("bean2", bean2);
	context.getBeanFactory().registerSingleton("bean3", bean3);
	context.getBeanFactory().registerSingleton("bean4", bean4);
	context.getBeanFactory().registerSingleton("bean5", bean5);
	context.getBeanFactory().registerSingleton("bean6", bean6);
	context.getBeanFactory().registerSingleton("bean7", bean7);
	context.refresh();
	assertTrue(bean2.isRunning());
	assertTrue(bean3.isRunning());
	assertTrue(bean5.isRunning());
	assertTrue(bean6.isRunning());
	assertTrue(bean7.isRunning());
	assertFalse(bean1.isRunning());
	assertFalse(bean4.isRunning());
	bean1.start();
	bean4.start();
	assertTrue(bean1.isRunning());
	assertTrue(bean4.isRunning());
	context.stop();
	assertFalse(bean1.isRunning());
	assertFalse(bean2.isRunning());
	assertFalse(bean3.isRunning());
	assertFalse(bean4.isRunning());
	assertFalse(bean5.isRunning());
	assertFalse(bean6.isRunning());
	assertFalse(bean7.isRunning());
	assertEquals(7, stoppedBeans.size());
	assertEquals(Integer.MAX_VALUE, getPhase(stoppedBeans.get(0)));
	assertEquals(500, getPhase(stoppedBeans.get(1)));
	assertEquals(1, getPhase(stoppedBeans.get(2)));
	assertEquals(0, getPhase(stoppedBeans.get(3)));
	assertEquals(0, getPhase(stoppedBeans.get(4)));
	assertEquals(-1, getPhase(stoppedBeans.get(5)));
	assertEquals(Integer.MIN_VALUE, getPhase(stoppedBeans.get(6)));
}
 
@Test
public void mixedShutdown() throws Exception {
	CopyOnWriteArrayList<Lifecycle> stoppedBeans = new CopyOnWriteArrayList<Lifecycle>();
	Lifecycle bean1 = TestLifecycleBean.forShutdownTests(stoppedBeans);
	Lifecycle bean2 = TestSmartLifecycleBean.forShutdownTests(500, 200, stoppedBeans);
	Lifecycle bean3 = TestSmartLifecycleBean.forShutdownTests(Integer.MAX_VALUE, 100, stoppedBeans);
	Lifecycle bean4 = TestLifecycleBean.forShutdownTests(stoppedBeans);
	Lifecycle bean5 = TestSmartLifecycleBean.forShutdownTests(1, 200, stoppedBeans);
	Lifecycle bean6 = TestSmartLifecycleBean.forShutdownTests(-1, 100, stoppedBeans);
	Lifecycle bean7 = TestSmartLifecycleBean.forShutdownTests(Integer.MIN_VALUE, 300, stoppedBeans);
	StaticApplicationContext context = new StaticApplicationContext();
	context.getBeanFactory().registerSingleton("bean1", bean1);
	context.getBeanFactory().registerSingleton("bean2", bean2);
	context.getBeanFactory().registerSingleton("bean3", bean3);
	context.getBeanFactory().registerSingleton("bean4", bean4);
	context.getBeanFactory().registerSingleton("bean5", bean5);
	context.getBeanFactory().registerSingleton("bean6", bean6);
	context.getBeanFactory().registerSingleton("bean7", bean7);
	context.refresh();
	assertTrue(bean2.isRunning());
	assertTrue(bean3.isRunning());
	assertTrue(bean5.isRunning());
	assertTrue(bean6.isRunning());
	assertTrue(bean7.isRunning());
	assertFalse(bean1.isRunning());
	assertFalse(bean4.isRunning());
	bean1.start();
	bean4.start();
	assertTrue(bean1.isRunning());
	assertTrue(bean4.isRunning());
	context.stop();
	assertFalse(bean1.isRunning());
	assertFalse(bean2.isRunning());
	assertFalse(bean3.isRunning());
	assertFalse(bean4.isRunning());
	assertFalse(bean5.isRunning());
	assertFalse(bean6.isRunning());
	assertFalse(bean7.isRunning());
	assertEquals(7, stoppedBeans.size());
	assertEquals(Integer.MAX_VALUE, getPhase(stoppedBeans.get(0)));
	assertEquals(500, getPhase(stoppedBeans.get(1)));
	assertEquals(1, getPhase(stoppedBeans.get(2)));
	assertEquals(0, getPhase(stoppedBeans.get(3)));
	assertEquals(0, getPhase(stoppedBeans.get(4)));
	assertEquals(-1, getPhase(stoppedBeans.get(5)));
	assertEquals(Integer.MIN_VALUE, getPhase(stoppedBeans.get(6)));
}