简介
Bean的初始化方法和销毁方法是Bean生命周期级别的;而Lifecycle是容器生命周期级别的。如果我们想在容器本身的生命周期(比如容器启动完成后、停止之前)事件上做一些工作,可以使用LifeCycle接口。
源码
public interface Lifecycle { void start(); void stop(); boolean isRunning(); }
- start:如果类A没在运行(isRunning返回false),则走该方法;否则不走该方法
- stop:如果类A在运行(isRunning返回true),则走该方法;否则不走该方法
- isRunning:返回true表示类A已经运行,返回false表示类A没运行
简单示例
isRunning返回true
代码
package com.example.config; import org.springframework.context.Lifecycle; import org.springframework.stereotype.Component; @Component public class MyLifeCycle implements Lifecycle { @Override public void start() { System.out.println("start"); } @Override public void stop() { System.out.println("stop"); } @Override public boolean isRunning() { System.out.println("isRunning"); return true; } }
结果
启动时无打印
关闭时:
isRunning stop
isRunning返回false
package com.example.config; import org.springframework.context.Lifecycle; import org.springframework.stereotype.Component; @Component public class MyLifeCycle implements Lifecycle { @Override public void start() { System.out.println("start"); } @Override public void stop() { System.out.println("stop"); } @Override public boolean isRunning() { System.out.println("isRunning"); return false; } }
结果
启动时无打印
关闭时
isRunning
改动isRunning值
我本以为,手动改isRunning返回值即可让它既执行start()又执行stop(),但我错了,如下所示:
package com.example.config; import org.springframework.context.Lifecycle; import org.springframework.stereotype.Component; @Component public class MyLifeCycle implements Lifecycle { private volatile boolean running = false; @Override public void start() { System.out.println("start"); running = true; } @Override public void stop() { System.out.println("stop"); } @Override public boolean isRunning() { System.out.println("isRunning"); return running; } }
结果
启动时无打印
关闭时:
isRunning
显式调用start()
在上边“简单示例” 可以发现,无论怎么搞,都调用不到start()方法!!。有人说必须显式去调用它,如下所示:
public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( "com.example.config.MyLifecycle"); ctx.start(); ctx.close(); }
但是,我只看到如下输出:
19:20:23.914 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3ab39c39 19:20:23.924 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' 19:20:23.944 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor' 19:20:23.944 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory' 19:20:23.944 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' 19:20:23.944 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor' 19:20:23.964 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'spring.liveBeansView.mbeanDomain' in PropertySource 'systemProperties' with value of type String 19:20:23.974 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3ab39c39, started on Tue Mar 09 19:20:23 CST 2021 19:20:23.974 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'spring.liveBeansView.mbeanDomain' in PropertySource 'systemProperties' with value of type String
对此,该有一张黑人问号脸。。。留到以后再看为什么会这样吧
请先
!