美文网首页
spring bean生命周期

spring bean生命周期

作者: 何德何能者 | 来源:发表于2020-12-15 12:14 被阅读0次

所谓的生命周期其实是状态变化

jvm对象状态变化

简略带过jvm对象的生成到销毁状态, 只做简单了解

  1. 分配内存地址(可见状态)
  2. 顺序给static变量赋值
  3. 触发构造函数(完成初始化)
  4. gc root不可达(带回收)
    这几个步骤只有2,3这两个步骤可由Java代码控制
    static示例
  static {
    System.out.println("------ userComponent static block");
  }

构造函数示例

public UserComponent() {
   System.out.println("------------- bean instant -----------");
}

spring beanFactory 管理bean生命周期

beanFactory提供了两个接口,用于感知bean的状态

InitializingBean 接口

只有一个方法
afterPropertiesSet()

Invoked by the containing {@code BeanFactory} after it has set all bean properties
 and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
This method allows the bean instance to perform validation of its overall
configuration and final initialization when all bean properties have been set.

该方法,由BeanFactory在当前的bean都完成属性设置和完成BeanFactoryAware,ApplicationContextAware设置后主动触发。 一般用于检查属性配置或者最终初始化个性配置。

DisposableBean 接口

只有一个方法
destory()
在beanFactory 销毁bean时主动触发

ApplicationContext 管理bean生命周期

Lifecycle 接口

提供start(),stop(),isRunning三个方法。分别感知component的可用,销毁,检查可用功能. bean一般不会直接实现这个接口

SmartLifecycle 接口

该接口继承Lifecycle接口.

  1. isAutoStartup() 用于在ApplicationContext进行refresh时是否start
    2.stop(callback) 添加了回调的停止方法. 实现方法需要callback.run(),不然会阻塞30秒

ApplicationContext在refresh方法的里会触发finishRefresh()方法,该在ApplicationContext实例化单例后执行。 finishRefresh()方法触发LifecycleProcessor,实现了Lifecyel接口的bean就是委派给LifecycleProcessor进行管理.
主要代码

@Component
public class UserComponent implements SmartLifecycle , InitializingBean, DisposableBean {

    static {
        System.out.println("------ userComponent static block");
    }
    public UserComponent() {
        System.out.println("------------- bean instant -----------");
    }
    private String name;

    @Autowired
    private UserProfileComponent userProfileComponent;
  
    @Override
    public boolean isAutoStartup() {
        return true;
    }
    @Override
    public void stop(Runnable callback) {
        System.out.println("------------  stop callback");
        isRunning = false;
        callback.run();
    }
    @Override
    public void start() {
        System.out.println("------------  start");
        isRunning = true;
    }
    @Override
    public void stop() {
        System.out.println("------------  stop");
    }
    @Override
    public boolean isRunning() {
        return isRunning;
    }
    @Override
    public int getPhase() {
        return 0;
    }
    private static boolean isRunning = false;

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("--------------- afterPropertiesSet uerProfileComponent:" + userProfileComponent);
    }
    @Override
    public void destroy() throws Exception {
        System.out.println("---------------- destroy----");
    }
}
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
annotationConfigApplicationContext.scan("com.xie.java.beans");
annotationConfigApplicationContext.refresh();    
// 扫描放这又会不一样
// annotationConfigApplicationContext.scan("com.xie.java.beans");
annotationConfigApplicationContext.getBeanFactory().destroyBean(annotationConfigApplicationContext.getBean(UserComponent.class));
annotationConfigApplicationContext.stop();

运行结果

12月 15, 2020 12:10:57 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@dc24521: startup date [Tue Dec 15 12:10:57 CST 2020]; root of context hierarchy
------ userComponent static block
------------- bean instant -----------
--------------- afterPropertiesSet uerProfileComponent:com.xie.java.beans.UserProfileComponent@4d910fd6
12月 15, 2020 12:10:57 下午 org.springframework.context.support.DefaultLifecycleProcessor start
信息: Starting beans in phase 0
12月 15, 2020 12:10:57 下午 org.springframework.context.support.DefaultLifecycleProcessor stop
信息: Stopping beans in phase 0
------------  start
---------------- destroy----
------------  stop callback

相关文章

网友评论

      本文标题:spring bean生命周期

      本文链接:https://www.haomeiwen.com/subject/umaigktx.html