Spring Bean的生命周期

Spring Bean的生命周期

Spring Ioc容器的目的是管理Bean。Bean 是存放在Spring Ioc容器中的,所以它也会在容器中存在生命周期,它的初始化,存活和销毁也需要一个过程。对于一些需要自定义生命周期的Bean,我们可以插入代码去改变它们的一些行为,以满足特定的需求,这就需要使用Spring Bean生命周期的知识了。

在这里插入图片描述

最好把生命周期记下来,面试可能会问

如果自定义Bean的生命周期,这个Bean类要实现 BeanNameAware, BeanFactoryAware,
ApplicationContextAware, InitializingBean接口,并重写对应方法。自定义初始化,销毁方法。

package com.yao.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author YaoHui
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements BeanNameAware, BeanFactoryAware,
        ApplicationContextAware, InitializingBean {
    private String username;
    private String password;
    private Computer computer;

    public void init(){
        System.out.println("【" + this.getClass().getSimpleName() +"】执行自定义初始化方法");
    }

    public void destroy0(){
        System.out.println("【" + this.getClass().getSimpleName() +"】执行自定义销毁方法");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("【" + this.getClass().getSimpleName() +"】调用BeanFactoryAware接口的setBeanFactory方法");
    }

    @Override
    public void setBeanName(String s) {
        System.out.println("【" + this.getClass().getSimpleName() +"】调用BeanNameAware接口的setBeanName方法");
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("【" + this.getClass().getSimpleName() +"】调用InitializingBean接口的afterPropertiesSet方法");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("【" + this.getClass().getSimpleName() +"】调用ApplicationContextAware接口的setApplicationContext方法");
    }
}

在这里插入图片描述

通过一个bean包,实现BeanPostProcess,DisposableBean接口.并重写对应的postProcessBeforeInitialization postProcessAfterInitialization, destroy 方法这三个方法每个Bean都会调用.

public class BeanPostProcessImpl implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("【" + bean.getClass().getSimpleName() +"】对象"+ beanName + "开始实列化");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("【" + bean.getClass().getSimpleName() +"】对象"+ beanName + "实列化完成");
        return bean;
    }
}
public class DisposableBeanImpl implements DisposableBean {
    @Override
    public void destroy() throws Exception {
        System.out.println("【" + this.getClass().getSimpleName() +"】调用Disposable的destroy方法");

    }
}

使用自定义初始化,销毁方法时要在配置文件中声明 init-method destroy-method

在这里插入图片描述

证明生命周期效果图

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值