Bean的生命周期

在介绍之前给大家安利一个github资源:
https://github.com/crossoverJie/JCSprout
Bean的生命周期:
Ben的创建初始化销毁的过程。
容器管理bean的生命周期,我们还可以自定义初始化和销毁方法,容器在bean进行到当前生命周期的时候来自定义的初始化和销毁。

构造(对象的创建)
单实例:在容器启动的时候创建对象;
多实例:在每次获取的时候创建对象;
BeanPostProcessor.postProcessBeforeInitialization
初始化:
对象创建完成,并赋值好,调用初始化方法。
BeanPostProcessor. postProcessAfterInitialization
销毁
单实例:容器关闭的时候;
多实例:容器不会管理这个bean,容器不会调用销毁方法。

生命周期管理方式:
1) 指定初始化和销毁方法:指定initMethod和destroyMethod。
import org.springframework.stereotype.Component;

@Component
public class Car {
public Car() {
System.out.println(“Car…Contructor…”);
}
public void init() {
System.out.println(“Car…init…”);
}
public void destroy() {
System.out.println(“Car…destroy…”);
}
}

@Configuration
@ComponentScan(“com.xc.annotation.bean”)
public class MyConfig3 {
@Scope(“prototype”)
@Bean(initMethod=“init”,destroyMethod=“destroy”)
public Car car() {
return new Car();
}
}

2) 通过Bean实现InitializingBean接口(定义初始化逻辑)和DisposableBean(定义销毁逻辑)。
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component
public class Cat implements InitializingBean,DisposableBean {
public Cat() {
System.out.println(“Cat…Contructor…”);
}
@Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println(“cat…destroy…”);
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println(“cat…afterPropertiesSet…”);
}
}

3) 可以使用JSR250:
@PostConstruct:在bean创建完成并且属性赋值完成,来执行初始化方法。
@PreDestroy:在容器销毁bean之前通知我们进行清理工作。

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class Dog implements ApplicationContextAware{
@Autowired
private ApplicationContext applicationContext;
public Dog() {
System.out.println(“Dog…Constructor…”);
}
//对象创建并赋值之后调用
@PostConstruct
public void init() {
System.out.println(“Dog…@PostConstruct…”);
}
//容器移除对象之前
@PreDestroy
public void destroy() {
System.out.println(“Dog…@PreDestroy…”);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO Auto-generated method stub
this.applicationContext = applicationContext;
}
}
4) BeanPostProcessor【interface】:bean的后置处理器:
在bean的初始化前后进行一些处理工作。
postProcessBeforeInitialization:在初始化之前工作;
postProcessAfterInitialization:在初始化之后工作。

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

  • 后置处理器:初始化后进行处理工作。
  • 将后置处理器加入到容器中。
  • @author kleine
    */
    @Component
    public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    System.out.println(“postProcessAfterInitialization…”+beanName+"–>"+bean);
    return bean;
    }
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    System.out.println(“postProcessBeforeInitialization…”+beanName+"–>"+bean);
    return bean;
    }
    }

5) 实现XXXWare接口
XXAware接口可以用于初始化bean的时候获得Spring的一些对象,如获取Spring上下文环境。这个bean初始化就会调用setApplicationContext方法,并可以获得applicationContext对象。
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class Dog implements ApplicationContextAware{
@Autowired
private ApplicationContext applicationContext;
public Dog() {
System.out.println(“Dog…Constructor…”);
}
//对象创建并赋值之后调用
@PostConstruct
public void init() {
System.out.println(“Dog…@PostConstruct…”);
}
//容器移除对象之前
@PreDestroy
public void destroy() {
System.out.println(“Dog…@PreDestroy…”);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO Auto-generated method stub
this.applicationContext = applicationContext;
}
}

以上方式的测试类为:
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.xc.annotation.bean.Car;
import com.xc.annotation.config.MyConfig3;
public class IOCTest_LifeCircle {
@SuppressWarnings(“resource”)
@Test
public void test() {
AnnotationConfigApplicationContext atx = new AnnotationConfigApplicationContext(MyConfig3.class);
System.out.println(“容器创建完成。。。。”);
//Car bean = atx.getBean(Car.class);

	//关闭容器
	atx.close();
}

}

输出:
postProcessBeforeInitialization…myConfig3–>com.xc.annotation.config.MyConfig3 E n h a n c e r B y S p r i n g C G L I B EnhancerBySpringCGLIB EnhancerBySpringCGLIB109d160d@3c153a1
postProcessAfterInitialization…myConfig3–>com.xc.annotation.config.MyConfig3 E n h a n c e r B y S p r i n g C G L I B EnhancerBySpringCGLIB EnhancerBySpringCGLIB109d160d@3c153a1
Car…Contructor…
postProcessBeforeInitialization…car–>com.xc.annotation.bean.Car@2133814f
Car…init…
postProcessAfterInitialization…car–>com.xc.annotation.bean.Car@2133814f
Boss… Contruct…
postProcessBeforeInitialization…boss–>Boss [car=com.xc.annotation.bean.Car@2133814f]
postProcessAfterInitialization…boss–>Boss [car=com.xc.annotation.bean.Car@2133814f]
Cat…Contructor…
postProcessBeforeInitialization…cat–>com.xc.annotation.bean.Cat@7692d9cc
cat…afterPropertiesSet…
postProcessAfterInitialization…cat–>com.xc.annotation.bean.Cat@7692d9cc
Dog…Constructor…
postProcessBeforeInitialization…dog–>com.xc.annotation.bean.Dog@358ee631
Dog…@PostConstruct…
postProcessAfterInitialization…dog–>com.xc.annotation.bean.Dog@358ee631
postProcessBeforeInitialization…red–>com.xc.annotation.bean.Red@3835c46
postProcessAfterInitialization…red–>com.xc.annotation.bean.Red@3835c46
Dog…@PreDestroy…
cat…destroy…

补充:BeanPostProcessor的原理(具体查看源码):

遍历得到容器中所有的BeanPostProcessor,这个执行beforeInitialization一旦返回null,跳出for循环,不会执行后面的BeanPostProcessor。

populateBean(beanName,mbd,instanceWrapper):给bean进行属性赋值。
initializeBean
{
applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
invokeInitMethods(beanName, wrappedBean, mbd);执行自定义初始化;
applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}

Spring底层对 BeanPostProcessor 的使用:
bean赋值,注入其他组件,@Autowired,生命周期注解功能等等都是使用 BeanPostProcessor 完成的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值