Spring的生命周期(三)

bean的生命周期: bean创建—初始化----销毁的过程

容器管理bean的生命周期:我们可以自定义初始化和销毁方法;容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法.

构造对象创建

  •  单实例:在容器启动的时候创建对象.
    
  •  多实例:在每次获取的时候创建对象.
    

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

销毁

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

通过@Bean指定init-method和destroy-method指定初始化和销毁方法;

下面举个例子说明一下:

  • 创建一个car实例对象:
@Component
public class Car {
	//Car的构造方法
	public Car(){
		System.out.println("car constructor...");
	}
	//初始化方法(需在@Bean中定义)
	public void init(){
		System.out.println("car ... init...");
	}
	//销毁方法(需在@Bean中定义)
	public void detory(){
		System.out.println("car ... detory...");
	}

}

创建Configuration对象:

@ComponentScan("com.atguigu.bean")
@Configuration
public class MainConfigOfLifeCycle {
	//@Scope("prototype")
	@Bean(initMethod="init",destroyMethod="detory")
	public Car car(){
		return new Car();
	}
}

写一个测试类:

public class IOCTest_LifeCycle {
	
	@Test
	public void test01(){
		//1、创建ioc容器
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
		System.out.println("容器创建完成...");
		//关闭容器
		applicationContext.close();
	}

}

打印结果如下:
在这里插入图片描述
注: 当使用@Scope(“prototype”) 直接定义Bean时,需使用applicationContext.getBean(“car”)方法才能获取Bean.容器不会管理这个bean;不会调用销毁方法.打印结果如下:(并不会调用销毁方法)
在这里插入图片描述

  • 使用InitializingBean,DisposableBean实现对象的初始化和销毁方法.
@Component
public class Cat implements InitializingBean,DisposableBean {
	
	public Cat(){
		System.out.println("cat constructor...");
	}

	public void destroy() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("cat...destroy...");
	}

	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("cat...afterPropertiesSet...");
	}

}

测试一下:

	@Test
	public void test01(){
		//1、创建ioc容器
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
		System.out.println("容器创建完成...");

		applicationContext.close();
	}

打印结果如下:
在这里插入图片描述

  • 使用可以使用JSR250,@PostConstruct,@PreDestroy
@Component
public class Dog{
	public Dog(){
		System.out.println("dog constructor...");
	}
	
	//对象创建并赋值之后调用
	@PostConstruct
	public void init(){
		System.out.println("Dog....@PostConstruct...");
	}
	
	//容器移除对象之前
	@PreDestroy
	public void detory(){
		System.out.println("Dog....@PreDestroy...");
	}
}

执行测试类,打印如下:
在这里插入图片描述

  • BeanPostProcessor【interface】:bean的后置处理器
    在bean初始化前后进行一些处理工作:
  •  postProcessBeforeInitialization:在初始化之前工作
    
  •  postProcessAfterInitialization:在初始化之后工作
    

定义MyBeanPostProcessor

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean);
		return bean;
	}

	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		// TODO Auto-generated method stub
		System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean);
		return bean;
	}
}

运行测试类,打印结果如下:

postProcessBeforeInitialization…org.springframework.context.event.internalEventListenerProcessor=>org.springframework.context.event.EventListenerMethodProcessor@5b0abc94
postProcessAfterInitialization…org.springframework.context.event.internalEventListenerProcessor=>org.springframework.context.event.EventListenerMethodProcessor@5b0abc94
postProcessBeforeInitialization…org.springframework.context.event.internalEventListenerFactory=>org.springframework.context.event.DefaultEventListenerFactory@1f1c7bf6
postProcessAfterInitialization…org.springframework.context.event.internalEventListenerFactory=>org.springframework.context.event.DefaultEventListenerFactory@1f1c7bf6
postProcessBeforeInitialization…mainConfigOfLifeCycle=>com.atguigu.config.MainConfigOfLifeCycleEnhancerBySpringCGLIB16959f53@2b546384
postProcessAfterInitialization…mainConfigOfLifeCycle=>com.atguigu.config.MainConfigOfLifeCycleEnhancerBySpringCGLIB16959f53@2b546384
car constructor…
postProcessBeforeInitialization…car=>com.atguigu.bean.Car@b62fe6d
car … init…
postProcessAfterInitialization…car=>com.atguigu.bean.Car@b62fe6d
Boss…有参构造器
postProcessBeforeInitialization…boss=>Boss [car=com.atguigu.bean.Car@b62fe6d]
postProcessAfterInitialization…boss=>Boss [car=com.atguigu.bean.Car@b62fe6d]
cat constructor…
postProcessBeforeInitialization…cat=>com.atguigu.bean.Cat@7c417213
cat…afterPropertiesSet…
postProcessAfterInitialization…cat=>com.atguigu.bean.Cat@7c417213
dog constructor…
postProcessBeforeInitialization…dog=>com.atguigu.bean.Dog@3745e5c6
Dog…@PostConstruct…
postProcessAfterInitialization…dog=>com.atguigu.bean.Dog@3745e5c6
Dog…@PreDestroy…
cat…destroy…
car … detory…

BeanPostProcessor原理:

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章