1、配置
package com.hao.config;
import com.hao.bean.Car;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
/**
* @author haoxiansheng
* @date 2020-05-29
*
* 1、bean的生命周期
* bean 创建 ==> 初始化 ==> 销毁的过程
* 2、 容器管理bean的生命周期
* 可以自定义初始化和销毁方法:容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法
* 3、构造方法
* 单实例:容器创建时完成对象创建
* 多实例:在每次获取的时候创建对象
* 方式一
* 1) 指定初始化和销毁方法 @Bean 中指定初始化和销毁方法
* 指定init-method和destroy-method
* 2)单实例 对象创建完成,并赋值好调用初始化方法、容器关闭 调用销毁方法
* 3)多实例获取bean的时候执行init 方法 但容器不会调用多实例的销毁方法
*
* 方式二
* 1、让bean实现{@link org.springframework.beans.factory.InitializingBean()}接口,定义初始化方法
* 2、实现{@link org.springframework.beans.factory.DisposableBean()}接口(定义销毁方法)
*
* 方式三 JSR250
* 1、@PostConstruct : 在bean创建完成并且属性赋值完成,来执行初始化方法
* 2、@PreDestroy:在容器销毁bean之前通知进行清理工作
*
* 方法四
* 1、{@link org.springframework.beans.factory.config.BeanPostProcessor)[interface] bean 的后置处理器
* 1)postProcessBeforeInitialization 在bean初始化前后进行一些操作
* 2) postProcessAfterInitialization 在初始化之后工作
*/
@ComponentScan("com.hao")
@Configuration
public class MainConfigOfLifeCycle {
/**
* 指定对象的初始化和销毁方法
* @return
*/
//@Scope("prototype")
@Bean(initMethod = "init", destroyMethod = "destroy")
public Car car() {
return new Car();
}
}
package com.hao.bean;
/**
* @author haoxiansheng
* @date 2020-05-29
*/
public class Car {
public Car() {
System.out.println("car ---construct..");
}
public void init() {
System.out.println("car ... init..");
}
public void destroy() {
System.out.println("car ... destroy... ");
}
}
package com.hao.bean;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
/**
* @author haoxiansheng
* @date 2020-05-29
*/
@Component // 声明为一个组件
public class Cat implements InitializingBean, DisposableBean {
public Cat() {
System.out.println("cat ---construct..");
}
// 销毁方法
public void destroy() {
System.out.println("cat ... destroy... ");
}
// 初始化方法
public void afterPropertiesSet() throws Exception {
System.out.println("cat ... init..");
}
}
package com.hao.bean;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* @author haoxiansheng
* @date 2020-05-29
*/
@Component
public class Dog {
public Dog() {
System.out.println("dog construct ...");
}
// 在对象创建并赋值之后调用
@PostConstruct
public void init() {
System.out.println("dog construct 之后调用");
}
// 在容器销毁之前调用
@PreDestroy
public void destroy() {
System.out.println("dog 容器销毁之前调用");
}
}
package com.hao.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
/**
* @author haoxiansheng
* @date 2020-05-29
* 初始化前后进行处理
*/
@Component // 将后置处理器加入到容器中
public class MyBeanPostProcessor implements BeanPostProcessor {
public MyBeanPostProcessor() {
System.out.println("MyBeanPostProcessor");
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println(" MyBeanPostProcessor ...postProcessBeforeInitialization" + beanName + bean);
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("MyBeanPostProcessor... postProcessAfterInitialization" + beanName + bean);
return bean;
}
}
2、测试
package com.hao.test;
import com.hao.config.MainConfigOfLifeCycle;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author haoxiansheng
* @date 2020-05-29
*/
public class IOCLifeCycleTest {
// 测试第一种
@Test
public void test01() {
// 1、传入配置类 创建IOC 容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
System.out.println("ioc 容器创建完成");
// 获取
applicationContext.getBean("car");
// 2、关闭容器
applicationContext.close();
}
// 测试第二种
@Test
public void test02() {
// 1、传入配置类 创建IOC 容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
System.out.println("ioc 容器创建完成");
// 2、关闭容器
applicationContext.close();
}
}