1、初始化Bean的生命周期
2、销毁bean的生命周期
代码描述
Bean生命周期:
Bean创建—————初始化——————销毁的过程
构造(对象实例)
- 1)单实例:在容器启动的时候创建对象
- 2)多实例:在每次获取的时候创建对象
BeanPostProcessor.postProcessBeforeInitialization
初始化:
- 对象创建完成,并赋值完成,调用初始化方法。。。。
BeanPostProcessor.postProcessAfterInitialization
销毁:
- 单实例:容器关闭的时候
- 多实例:容器不会管理这个Bean,容器不会调用销毁方法
容器管理Bean的生命周期:
@Configuration
@ComponentScan("com.ysy.bean")
public class MainConfigOfLifeCycle {
//@Scope("prototype")
@Bean(initMethod = "init",destroyMethod = "destroy")
public Car car(){
return new Car();
}
}
*我们可以自定义初始化和销毁方法,容器在进行到对应的步骤时,会调用我们自定义的方法
- 1)指定初始化和销毁方法
通过@Bean指定init-method=" " destroy-method=""
package com.ysy.bean;
/**
* @author shanyangyang
* @date 2020/5/31
*/
public class Car {
public Car() {
System.out.println("Car constructor....");
}
/**
* 一般用于数据源配置,赋值
* */
public void init(){
System.out.println("Car init ...");
}
/**
* 数据源关闭
* */
public void destroy(){
System.out.println("Car destroy...");
}
}
- 2)通过让bean实现InitializingBean接口和DisposableBean接口
package com.ysy.bean;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
/**
* @author shanyangyang
* @date 2020/5/31
*/
@Component
public class Cat implements InitializingBean, DisposableBean {
public Cat() {
System.out.println("cat...constructor...");
}
@Override public void destroy() throws Exception {
System.out.println("cat destroy...");
}
@Override public void afterPropertiesSet() throws Exception {
System.out.println("cat...afterPropertiesSet");
}
}
- 3)通过使用JSR250
-
1)@PostConstruct:在Bean创建完成并且属性赋值完成,来执行初始化方法;
-
2)@PreDestroy:在容器销毁Bean之前通知进行清理工作;
- 4)BeanPostProcessor:bean的后置处理器
在Bean的初始化前后进行一些处理工作
postProcessBeforeInitialization:在初始化调用之前
Apply this {@code BeanPostProcessor} to the given new bean instance <i>before</i> any bean
initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
or a custom init-method).
postProcessAfterInitialization:在初始化之后
Apply this {@code BeanPostProcessor} to the given new bean instance <i>after</i> any bean
initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
or a custom init-method).
package com.ysy.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
/**
* 后置处理器:初始化前后进行处理
* bean the new bean instance
* beanName the name of the bean
* @author shanyangyang
* @date 2020/5/31
*/
@Component
public class MyBeanPostProccessor implements BeanPostProcessor {
@Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization......."+beanName+".....>>"+bean);
return bean;
}
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization......."+beanName+".....>>"+bean);
return bean;
}
}
测试类方法
package com.ysy.test;
import com.ysy.config.MainConfigOfLifeCycle;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author shanyangyang
* @date 2020/5/31
*/
public class IOCTestLifeCycle {
@Test
public void test01(){
//创建IOC容器
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
System.out.println("容器创建完成。。。。");
context.getBean("car");
context.close();
}
}