Bean的初始化过程
bean创建-->初始化-->摧毁
容器管理bean的声明周期
我们可以自定义初始化和摧毁方法;容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和摧毁方法
构造(对象创建)
单实例:在容器启动时创建对象
多实例:在每次获取时创建对象
初始化:
对象创建完成,并赋值,调用初始化方法
摧毁:
单实例:容器关闭的时候
多实例:容器不会管理这个bean,容器不会调用摧毁方法
1)指定初始化和摧毁方法
通过@Bean指定init-method和destroy-method;
2)通过让Bean实现InitializingBean(定义初始化逻辑),DisposableBean(定义摧毁逻辑)
3)使用JSR250规范中的注解
@PostConstruct:在bean创建完成并且属性赋值完成,来执行初始化方法
@PreDestory:在容器摧毁bean之前通知我们进行清理工作
4)BeanPostProcessor:bean的后置处理器;
在bean初始化前后进行一些处理工作;
postProcessBeforeInitialization初始化方法之前执行
postProcessAfterInitialization初始化方法之后执行
BeanPostProcessor 原理
populateBean(beanName, mbd, instanceWrapper);给bean进行属性赋值
initializeBean
{
applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
invokeInitMethods(beanName, wrappedBean, mbd);执行自定义初始化
applyBeanPostProcessorsAfterInitialization(Object bean, String beanName)
}
Spring底层对BeanPostProcessor的使用:
bean赋值,注入其他组件,@Autowired,声明周期注解功能,@Async,xxxBeanPostProcessor;
[AppleScript] 纯文本查看 复制代码 @ComponentScan("com.spring.bean")
@Configuration
public class MainConfigOfLifeCycle {
//@Scope("prototype")
@Bean(initMethod="init",destroyMethod="destroy")
public Car car(){
return new Car();
}
}
[AppleScript] 纯文本查看 复制代码
public class Car {
public Car(){
System.out.println("new Car ...");
}
public void init(){
System.out.println("init ...");
}
public void destroy(){
System.out.println("destroy ...");
}
}
[AppleScript] 纯文本查看 复制代码 @Component
public class Cat implements InitializingBean,DisposableBean{
public Cat(){
System.out.println("new Cat .........................");
}
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("init ...........................");
[mw_shl_code=applescript,true]@Component
public class Dog {
public Dog(){
System.out.println("new Dog ~~~~~~~~~~~~~~~~~");
}
@PostConstruct
public void init(){
System.out.println("init Dog ~~~~~~~~~~~~~~~~~");
}
@PreDestroy
public void destroy(){
System.out.println("destroy Dog ~~~~~~~~~~~~~~~~~");
}
}
}
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("destroy ..........................");
}
[/mw_shl_code]
[AppleScript] 纯文本查看 复制代码 /**
* 后置处理器:初始化前后进行处理工作
* 将后置处理器加入到容器中
*
*/
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println(bean.getClass().getName()+">>>>>>>>postProcessAfterInitialization<<<<<<<<<<");
return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println(bean.getClass().getName()+">>>>>>>>postProcessBeforeInitialization<<<<<<<<<<");
return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
}
}
[AppleScript] 纯文本查看 复制代码 public class IOCTest_LeftCycle {
@Test
public void test01(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
System.out.println("........");
//context.getBean("car");
context.close();
}
}
|