spring bean的生命周期回调和容器的生命周期

有关Spring的生命周期回调在官网https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-lifecycle有详细讲解。spring的生命周期回调分为Bean的生命周期回调和容器的生命周期回调。

1 Bean的生命周期回调

1.1 Bean的生命周期初始化回调

1.1.1 @PostConstruct注解

@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class A1Service{
   @PostConstruct
   public void  init(){
      System.out.println("A1Service init ... ");
   }
}

1.1.2 实现InitializingBean 接口

@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public  class A3Service implements InitializingBean {
   @Override
   public void afterPropertiesSet() throws Exception {
      System.out.println("A3Service init...");
   }
}

1.1.3 xml中init-method属性指定

Bean类:

public class A2Service{
   public void init(){
      System.out.println("A2Service init ... ");
   }
}	

xml配置:

<bean id="a2Service" class="stu.spring.services.A2Service" init-method="init"></bean>
1

1.1.4 以上三种Bean生命周期初始化回调方式都存在时执行的顺序问题

在源码层面,按照上述顺序依次执行。

Bean生命周期回调初始化在bean初始化完成之后进行执行。
当配置了多种Bean生命周期初始化回调方式并且指向同一个方法时,该方法只被调用一次。

1.2 Bean的生命周期销毁回调

1.2.1 @PreDestroy注解

@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class A1Service{
   @PreDestroy
   public void  destroy(){
      System.out.println("A1Service destroy ... ");
   }
}

1.2.2 实现DisposableBean接口

@Component
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public  class A3Service implements DisposableBean {
   @Override
   public void destroy() throws Exception {
      System.out.println("A3Service destroy...");
   }
}

1.2.3 xml中配置destroy-method属性

Bean类:

public abstract  class A2Service{
   public void destroy() {
      System.out.println("A2Service destroy...");
   }
}

xml配置:

<bean id="a2Service" class="stu.spring.services.A2Service" destroy-method="destroy"></bean>
1

1.2.4 以上三种Bean生命周期销毁回调方式都存在时执行的顺序问题

在源码层面,按照上述顺序依次执行。
当配置了多种Bean生命周期销毁回调方式并且指向同一个方法时,该方法被调用一次。

1.2.5 配置默认的Bean的生命周期销毁回调方法名

spring有个特殊的功能,可以配置Bean的销毁回调方法名默认为close或者shutdown。

在spring源码中AbstractBeanDefinition类有这样一个属性:

public static final String INFER_METHOD = "(inferred)";

只要配置了Bean的销毁方法名为AbstractBeanDefinition.INFER_METHOD属性的值,Bean在销毁时会默认调用方法为close或者shutdown的方法,如果两个方法同时存在只会调用close方法。
配置方式方式一:

在xml中指定destroy-method属性的值为上述属性值。

<bean id="xmlConfigLifeCycle"
     class="stu.spring.lifecycle.bean.XmlConfigLifeCycle" autowire="byType"
     init-method="init" destroy-method="(inferred)"
   ></bean>

配置方式二:
自定义Bean工厂后置处理器修改BeanDefinition对象的destroyMethodName属性值

@Component
public class CustomerBeanFactoryPostProcesser implements BeanFactoryPostProcessor {
   @Override
   public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
      BeanDefinition aService = beanFactory.getBeanDefinition("AService");
   aService.setDestroyMethodName(AbstractBeanDefinition.INFER_METHOD);
   }
}

2 Spring容器的生命周期回调

2.1 实现SmartLifecycle接口

@Component
public class IocLifeCycle1 implements SmartLifecycle {
   private boolean isRunning = false;
   //IOC容器初始化完成调用
   @Override
   public void start() {
      System.out.println("IocLifeCycle1 start ...");
      isRunning=true;//必须设置为true,要不然不会掉调用stop()
   }
   //非web项目需要容器显示的调用close()或者stop()才会调用此处
   @Override
   public void stop() {
      System.out.println("IocLifeCycle1 stop ...");

   }

   @Override
   public boolean isRunning() {
      return isRunning;
   }

   //存在多个SmartLifecycle实现类时执行的优先级
   @Override
   public int getPhase() {
      return 10;
   }
}

比较常用的IOC容器的生命周期回调方式,使用该方式完成容器的生命周期销毁回调时直接调用的时SmartLifecycle接口的

default void stop(Runnable callback) {
   stop();
   callback.run();
}

方法,spring中完成容器的生命周期销毁回调有超时时间设置,默认30s,可配置,
配置方式:

<bean id="lifecycleProcessor" class="org.springframework.context.support.DefaultLifecycleProcessor">
    <property name="timeoutPerShutdownPhase" value="10000"/>
</bean>

2.2 实现Lifecycle接口

使用Lifecycle接口的实现类完成IOC容器的生命周期回调时需要显示的调用容器的start();

@Component
public class IocLifeCycle2 implements Lifecycle {
   private boolean isRunning = false;

   @Override
   public void start() {
      System.out.println("IocLifeCycle2 start ...");
      isRunning=true;
   }
//非web项目需要容器显示的调用close()或者stop()才会调用此处
   @Override
   public void stop() {
      System.out.println("IocLifeCycle2 stop ...");
   }

   @Override
   public boolean isRunning() {
      return isRunning;
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值