spring注解开发-Bean的生命周期

Bean的生命周期:

bean的创建------>初始化------>销毁的过程

由容器来管理bean的生命周期

我们可以自定义bean的初始化和销毁方法,让容器在bean进行到当前生命周期时来调用这些方法。

构造(对象创建)

单实例:在容器启动的时候创建

多实例:在每一次获取的时候创建

初始化:

对象创建完成,并赋值好,调用初始化方法

销毁:

单实例:在容器关闭的时候关闭

多实例:容器不管理这个bean,容器不会调用销毁方法

 

1.指定初始化和销毁方法

通过@Bean的initMethod和destroyMethod指定初始化方法和销毁方法

@Bean(initMethod = "init",destroyMethod = "destroy")

单实例:

测试类:

package cn.it.bean;

/**
 * @author Admin
 * @create 2019-04-21-23:18
 */
public class Car {
    public Car() {
        System.out.println("Car.....创建");
    }

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

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

    @Test
    void BeanTest() {
        AnnotationConfigApplicationContext ioc = new AnnotationConfigApplicationContext(MainConfigOfLife.class);
        System.out.println("容器创建完成");

        ioc.close();
        System.out.println("容器关闭");
    }

打印结果: 


通过多实例:


2.通过让Bean实现InitializingBean,DisposableBean接口

Cat类实现了 InitializingBean,DisposableBean接口

public class Cat implements InitializingBean,DisposableBean {
    public Cat() {
        System.out.println("Cat.....constructor...");
    }

    public void destroy() throws Exception {
        System.out.println("Cat.....destroy......");
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("Cat....afterPropertiesSet.......");
    }
}

测试:


3. 可以使用JSR250:@PostConstruct和@PreDestroy注解


4.使用Spring的后置处理器BeanPostProcessor

public interface BeanPostProcessor {
    @Nullable
    default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Nullable
    default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}

postProcessBeforeInitialization:在bean初始化之前工作

postProcessAfterInitialization:在bean初始化之后工作


 写好实现了BeanPostProcessor 的类

/**
 * 后置处理起,初始化前后进行处理
 * @author Admin
 * @create 2019-04-26-16:29
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization..."+beanName+":::"+bean);
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization..."+beanName+"---"+bean);
        return bean;
    }
}

将后置处理器加入到容器中

测试:


 BeanPostProcessor的原理

源码:

在初始化之前调用:applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName)

	@Override
	public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
			throws BeansException {

		Object result = existingBean;
		for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
			Object current = beanProcessor.postProcessBeforeInitialization(result, beanName);
			if (current == null) {
				return result;
			}
			result = current;
		}
		return result;
	}

遍历得到容器中所有的 BeanPostProcessor,挨个执行postProcessBeforeInitialization方法,一但方法返回null,跳出for循环,不会执行后面的beanProcessor.postProcessBeforeInitialization(result, beanName)。


执行bean的初始化方法

invokeInitMethods(beanName, wrappedBean, mbd)


applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName)

	@Override
	public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
			throws BeansException {

		Object result = existingBean;
		for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
			Object current = beanProcessor.postProcessAfterInitialization(result, beanName);
			if (current == null) {
				return result;
			}
			result = current;
		}
		return result;
	}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值