SpingBean生命周期

序言

本文主要介绍,bean在IOC容器中的生命周期

在这里插入图片描述

Bean的生命周期的流程

bean的生命周期主要分为三个周期,
创建->(后置处理器初始化)初始化(后置处理器销毁)->销毁的过程

流程
构造(对象创建)
1.创建
单实例:在容器启动的时候创建对象
多实例:在每次获取的时候创建对象

postProcessBeforeInitIalization:初始化之前工作(后置处理器)
2. 初始化
postProcessAfterInitIalization:在初始化之后工作(后置处理器)

3.销毁
单实例:容器关闭的时候销毁
多实例:容器不会管理这个bean 容器不会调用销毁方法

干预Bean的生命周期

1.指定初始化和销毁方法
在对应bean中创建初始化方法 和销毁方法

package com.example.demo.bean;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * @author 邓琦
 * @version 1.0
 * @description: TODO
 * @date 2021/9/9 10:30
 */
public class Car  {

    public Car() {
        System.out.println("构造方法初始化======");
    }

    public void init(){
        System.out.println("bean初始化方法========");
    }

    public void destroy(){
        System.out.println("bean销毁方法==========");
    }

 
}

配置类添加bean,注解bean中添加对应bean的初始化方法和销毁方法

package com.example.demo.config;

import com.example.demo.bean.Car;
import com.example.demo.bean.bike;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class MainCofigOfLifecycle {
    //添加car的bean
    @Bean(initMethod = "init",destroyMethod = "destroy")
    public Car car(){
        return new Car();
    }
    //添加car的bean
    @Bean
    public bike bike(){
        return new bike();
    }
}

2.通过Bean实现InitializingBean(初始化逻辑)DisposableBean(定义销毁逻辑)

package com.example.demo.bean;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * @author 邓琦
 * @version 1.0
 * @description: TODO
 * @date 2021/9/9 10:30
 */
public class Car implements InitializingBean, DisposableBean {

    public Car() {
        System.out.println("构造方法初始化======");
    }

    //在属性创建完成之后调用
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Car ========== afterPropertiesSet");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("bean销毁方法==========");
    }
}

3.可以使用JSR250
@PostConstruct 在bean创建完成并且属性配置完成时,来执行初始化方法
@preDeStroy 在容器销毁bean之前通知我们进行清理工作

package com.example.demo.bean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @author 邓琦
 * @version 1.0
 * @description: TODO
 * @date 2021/9/9 11:37
 */
public class bike {
    public bike() {
            System.out.println("bike :PostConstruct:构造函数执行========");
    }

    //对象创建并赋值之后调用
    @PostConstruct
    public void init(){
        System.out.println("bike :PostConstruct: bean初始化方法========");
    }
    //容器移除对象之前
    @PreDestroy
    public void destroy(){
        System.out.println("bike: PreDestroy : bean销毁方法==========");
    }
}

4.BeanPostProcessor【interface】bean的后置处理器

  在bean初始化前后进行一些处理操作
   postProcessBeforeInitIalization:初始化之前工作
   postProcessAfterInitIalization:在初始化之后工作
package com.example.demo.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * @author 邓琦
 * @version 1.0
 * @description: TODO
 * @date 2021/9/9 12:47
 */
@Component
public class MyPostProcessor implements BeanPostProcessor{
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization初始化之前===。。。。。"+beanName+bean);
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization初始化之后===。。。。。"+beanName+bean);
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值