所有分类
  • 所有分类
  • 未分类

Spring容器生命周期-Lifecycle

简介

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

对此,该有一张黑人问号脸。。。留到以后再看为什么会这样吧

SmartLifecycle

见 :Spring容器生命周期–SmartLifecycle的用法 – 自学精灵

2

评论1

请先

  1. 说明 SmartLiftcycle继承自Lifecycle,可以自动执行start方法,Lifecycle需要手动执行容器的start/stop方法 常用方法 start() //容器启动后 stop() //容器停止时调用 isAutoStartup() //默认返回true,即自动调用start getPhase() //用于多个SmartLifecycle排序,值越小优先级越高(支持负数)
    秋风扫落叶 2023-08-12 0
显示验证码
没有账号?注册  忘记密码?

社交账号快速登录