一、源码解释
二、基本过程
1、从xml配置的Bean,@Bean注解,或者Java代码BeanDefinitionBuilder中读取Bean的定义,实例化Bean对象;
2、为bean的属性设置值和对其他bean引用(调用set方法)
3、调用bean的初始化方法(需要进行配置初始化方法 xml文件中配置 init-method)
4、bean可以使用了(对象获取到了)
5、当容器关闭的时候,调用bean的销毁方法(需要进行配置销毁的方法 xml中配置 destroy-method)
测试生命周期
init-method:初始化方法; 对应注解:@PostConstruct(优先级在set之前)
destroy-method:销毁方法;对应注解:@PreDestroy
<bean id="orders" class="com.supking.bean.Orders" init-method="initMethod" destroy-method="destroyMethod"><property name="name" value="phone"></property></bean>
public class Orders {public Orders() {System.out.println("第一步....执行了无法参数的构造,创建了bean实例");}private String name;public void setName(String name) {this.name = name;System.out.println("第二步。。。。调用set方法设置属性的值。。。。。");}// 创建初始化方法public void initMethod(){System.out.println("第三步。。。执行初始化方法。。。。");}public void destroyMethod(){System.out.println("第五步。。。。执行销毁方法");}}
public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");Orders orders = context.getBean("orders", Orders.class);System.out.println("第四步。。。获取到创建bean实例对象");// 手动让bean销毁((ClassPathXmlApplicationContext)context).close();}// 输出结果如下第一步....执行了无法参数的构造,创建了bean实例第二步。。。。调用set方法设置属性的值。。。。。第三步。。。执行初始化方法。。。。第四步。。。获取到创建bean实例对象第五步。。。。执行销毁方法
三、完整过程
1、xml 方式
基于第二种方法,如果加上了bean的后置处理器,即实现接口BeanPostProcessor,可以有七个步骤
1、从xml配置的Bean,@Bean注解,或者Java代码BeanDefinitionBuilder中读取Bean的定义,实例化Bean对象;
2、为bean的属性设置值和对其他bean引用(调用set方法)
3、把bean的实例传递给bean的后置处理器(postProcessBeforeInitialization)
4、调用bean的初始化方法(需要进行配置初始化方法)initializeBean的的invokeAwareMethods
5、把bean的实例传递给bean的后置处理器(postProcessAfterInitialization)
6、bean可以使用了(对象获取到了)
7、当容器关闭的时候,调用bean的销毁方法(需要进行配置销毁的方法)
添加后置处理器
创建类,实现接口BeanPostProcessor,创建后置处理器
public class MyBeanPost implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("在初始化之前执行的方法");return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("在初始化之后执行的方法");return bean;}}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="orders" class="com.supking.bean.Orders" init-method="initMethod" destroy-method="destroyMethod"><property name="name" value="phone"></property></bean><!-- 后置处理器,会给容器中的所有bean添加,所以在此时这个的时候,记得将上面不需要的bean(user,dog)注释掉--><bean id="myBeanPost" class="com.supking.bean.MyBeanPost"></bean></beans>
public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");Orders orders = context.getBean("orders", Orders.class);System.out.println("第四步。。。获取到创建bean实例对象");// 手动让bean销毁((ClassPathXmlApplicationContext)context).close();}// 输出结果第一步....执行了无法参数的构造,创建了bean实例第二步。。。。调用set方法设置属性的值。。。。。在初始化之前执行的方法第三步。。。执行初始化方法。。。。在初始化之后执行的方法第四步。。。获取到创建bean实例对象第五步。。。。执行销毁方法
2、注解方式
@Configuration@ComponentScan(basePackages = {"com.supkingx"})public class SpringConfig {}
@Servicepublic class UserService {private String name;public UserService() {System.out.println("第一步,无参构造");}public void setName(String name) {System.out.println("第三步,调用set方法设置值");this.name = name;}public void add() {System.out.println("UserService add.....");}@PostConstructpublic void init(){System.out.println("第二步,@PostConstruct 执行初始化方法");}@PreDestroypublic void destroy(){System.out.println("第五步,@PreDestroy 执行销毁方法");}}
@Servicepublic class PostBean implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("初始化之前,postProcessBeforeInitialization");return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("初始化之后,postProcessAfterInitialization");return bean;}}
public static void main(String[] args) {ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);UserService userService = context.getBean(UserService.class);userService.setName("king");System.out.println("第四步,获取到创建bean的实例");((AnnotationConfigApplicationContext) context).close();}结果:初始化之前,postProcessBeforeInitialization初始化之后,postProcessAfterInitialization第一步,无参构造初始化之前,postProcessBeforeInitialization第二步,@PostConstruct 执行初始化方法初始化之后,postProcessAfterInitialization第三步,调用set方法设置值第四步,获取到创建bean的实例第五步,@PreDestroy 执行销毁方法
