BigAminal类,自定义了init和destory方法
/**
* @author GaoYuzhe
* @date 2018/3/13.
*/
public class BigAnimal {
public BigAnimal() {
System.out.println("BigAnimal->Construtor");
}
public void init(){
System.out.println("BigAnimal ->init");
}
public void destory(){
System.out.println("BigAnimal->destory");
}
}
配置类LifeCyle,使用@Bean注解的initMthod和destroyMethod指定初始化和销毁方法
/**
* @author GaoYuzhe
* @date 2018/3/13.
*/
@Configuration
public class LifeCycleConfig {
@Bean(initMethod = "init",destroyMethod = "destory")
public BigAnimal animal(){
return new BigAnimal();
}
}
测试类LifeCycleConfigTest
/**
* @author GaoYuzhe
* @date 2018/3/13.
*/
public class LifeCycleConfigTest {
private AnnotationConfigApplicationContext annotationConfigApplicationContext =new AnnotationConfigApplicationContext(LifeCycleConfig.class);
@Test
public void animal() {
BigAnimal bigAnimal = annotationConfigApplicationContext.getBean(BigAnimal.class);
annotationConfigApplicationContext.close();
}
}
运行结果
通过让Bean实现InitializingBean(定义初始化逻辑),DisposableBean(定义销毁逻辑)
Man实现了InitalizingBean和DisposableBean接口,重写afterPropertiesSet和destroy方法
/**
* @author GaoYuzhe
* @date 2018/3/13.
*/
public class Man implements InitializingBean,DisposableBean {
public Man() {
System.out.println("construtor:你一个男人出生了。。。。");
}
public void afterPropertiesSet() throws Exception {
System.out.println("afterPropertiesSet:他的爸爸给了他¥100000元");
}
public void destroy() throws Exception {
System.out.println("destroy:他把他的¥100000元,给了他儿子");
}
}
LifeCylceConfig配置类中注册Bean
@Bean
public Man man(){
return new Man();
}
测试方法1中加入
@Test
public void man() {
Man man = annotationConfigApplicationContext.getBean(Man.class);
annotationConfigApplicationContext.close();
}
运行结果
WoMan类
/**
* @author GaoYuzhe
* @date 2018/3/13.
*/
public class Woman {
public Woman() {
System.out.println("Woman.Construtor ->一个女人出生了。。。。。。");
}
/**
*
* @throws Exception
*/
@PostConstruct
public void postConstruct() throws Exception {
System.out.println("Woman.postConstruct->她的爸爸给了她¥100000元");
}
/**
*
* @throws Exception
*/
@PreDestroy
public void preDestroy() throws Exception {
System.out.println("Woman->preDestroy她把她的¥100000元,给了她儿子");
}
}
配置类
@Bean
public Woman woman(){
return new Woman();
}
在测试1的测试类中添加测试方法3
@Test
public void woman() {
Woman woman = annotationConfigApplicationContext.getBean(Woman.class);
annotationConfigApplicationContext.close();
}
运行结果
在bean初始化前后进行一些处理工作;
自定义BeanPostProcessor,MyBeanPostProcessor
/**
* @author GaoYuzhe
* @date 2018/3/14.
*/
@Component
public class MyBeanPostProcessor implements BeanPostProcessor{
/**
* 初始化前置处理
* @param bean Bean实例本身
* @param beanName 容器中注册的BeanId
* @return
* @throws BeansException
*/
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if("woman".equals(beanName)){
System.out.println("前置处理");
}
return bean;
}
/**
* 初始化后置处理
* @param bean Bean实例本身
* @param beanName 容器中注册的BeanId
* @return
* @throws BeansException
*/
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if("woman".equals(beanName)){
System.out.println("后置置处理");
}
return bean;
}
}
在原LifeClyceConfig上加上@ComponentScan(“com.daxiong.condition”),来扫描
@Configuration
@ComponentScan("com.daxiong.condition")
运行测试方法3
bean的生命周期:bean创建—初始化—-销毁的过程
容器管理bean的生命周期:我们可以我们可以自定义初始化和销毁方法;容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法
具体步骤:
构造(对象创建):
BeanPostProcessor.postProcessBeforeInitialization
遍历得到容器中所有的BeanPostProcessor;挨个执行beforeInitialization,
一但返回null,跳出for循环,不会执行后面的BeanPostProcessor.postProcessorsBeforeInitialization
BeanPostProcessor原理
伪代码
//给bean进行属性赋值
populateBean(beanName, mbd, instanceWrapper);
initializeBean();
{
//前置处理
applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
//执行自定义初始化
invokeInitMethods(beanName, wrappedBean, mbd);
//后置处理
applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}