LifecycleProcessor 負責管理ApplicationContext生命週期。是ApplicationContext很重要的一環,須要他的地方實在太少了。java
Lifecycle的接口,沒有明顯的調用。因此Lifecycle基本無用spring
public interface Lifecycle { void start(); void stop(); boolean isRunning(); }
public void start() { getLifecycleProcessor().start(); publishEvent(new ContextStartedEvent(this)); } @Override public void stop() { getLifecycleProcessor().stop(); publishEvent(new ContextStoppedEvent(this)); } @Override public boolean isRunning() { return (this.lifecycleProcessor != null && this.lifecycleProcessor.isRunning()); }
LifecycleProcessor的onRefresh與onClose是比較重要的方法,onRefresh做用是容器啓動成功,onClose是隻應用要關閉的時候ide
public interface LifecycleProcessor extends Lifecycle { void onRefresh(); void onClose(); }
onRefresh調用點this
public void refresh() throws BeansException, IllegalStateException { ..... finishRefresh(); ...... } protected void finishRefresh() { // Clear context-level resource caches (such as ASM metadata from scanning). clearResourceCaches(); // Initialize lifecycle processor for this context. initLifecycleProcessor(); // Propagate refresh to lifecycle processor first. getLifecycleProcessor().onRefresh(); // Publish the final event. publishEvent(new ContextRefreshedEvent(this)); // Participate in LiveBeansView MBean, if active. LiveBeansView.registerApplicationContext(this); }
onClose調用點代理
public void close() { synchronized (this.startupShutdownMonitor) { doClose(); // If we registered a JVM shutdown hook, we don't need it anymore now: // We've already explicitly closed the context. if (this.shutdownHook != null) { try { Runtime.getRuntime().removeShutdownHook(this.shutdownHook); } catch (IllegalStateException ex) { // ignore - VM is already shutting down } } } }
當經過kill或者pkill 正常關閉應用程序的時候會出發下面方式中的 Thread.runcode
public void registerShutdownHook() { if (this.shutdownHook == null) { // No shutdown hook registered yet. this.shutdownHook = new Thread() { @Override public void run() { synchronized (startupShutdownMonitor) { doClose(); } } }; Runtime.getRuntime().addShutdownHook(this.shutdownHook); } }
DefaultLifecycleProcessor 是默認LifecycleProcessor實現,主要是負責全部的LifecycleProcessor實現執行,DefaultLifecycleProcessor是LifecycleProcessor的代理對象。只要不想spring容易裏面注入LifecycleProcessor實現類,那麼DefaultLifecycleProcessor 基本有行爲。orm
protected void initLifecycleProcessor() { ConfigurableListableBeanFactory beanFactory = getBeanFactory(); if (beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) { this.lifecycleProcessor = beanFactory.getBean(LIFECYCLE_PROCESSOR_BEAN_NAME, LifecycleProcessor.class); if (logger.isTraceEnabled()) { logger.trace("Using LifecycleProcessor [" + this.lifecycleProcessor + "]"); } } else { DefaultLifecycleProcessor defaultProcessor = new DefaultLifecycleProcessor(); defaultProcessor.setBeanFactory(beanFactory); this.lifecycleProcessor = defaultProcessor; beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor); if (logger.isTraceEnabled()) { logger.trace("No '" + LIFECYCLE_PROCESSOR_BEAN_NAME + "' bean, using " + "[" + this.lifecycleProcessor.getClass().getSimpleName() + "]"); } } }
全部xxBeans方法都會得到LifecycleProcessor的實現類對象
public void start() { startBeans(false); this.running = true; } @Override public void stop() { stopBeans(); this.running = false; } @Override public void onRefresh() { startBeans(true); this.running = true; } @Override public void onClose() { stopBeans(); this.running = false; } protected Map<String, Lifecycle> getLifecycleBeans() { ConfigurableListableBeanFactory beanFactory = getBeanFactory(); Map<String, Lifecycle> beans = new LinkedHashMap<>(); String[] beanNames = beanFactory.getBeanNamesForType(Lifecycle.class, false, false); for (String beanName : beanNames) { String beanNameToRegister = BeanFactoryUtils.transformedBeanName(beanName); boolean isFactoryBean = beanFactory.isFactoryBean(beanNameToRegister); String beanNameToCheck = (isFactoryBean ? BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName); if ((beanFactory.containsSingleton(beanNameToRegister) && (!isFactoryBean || matchesBeanType(Lifecycle.class, beanNameToCheck, beanFactory))) || matchesBeanType(SmartLifecycle.class, beanNameToCheck, beanFactory)) { Object bean = beanFactory.getBean(beanNameToCheck); if (bean != this && bean instanceof Lifecycle) { beans.put(beanNameToRegister, (Lifecycle) bean); } } } return beans; }