Spring容器中Bean的生命周期
Posted 我是小菜啊1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring容器中Bean的生命周期相关的知识,希望对你有一定的参考价值。
Spring生命周期分为以下步骤:
1.instantiate bean 对象实例化
2.populate properties 封装属性
3.如果Bean实现BeanNameAware执行setBeanName
4.如果Bean实现BeanFactoryAware或者ApplicationContextAware设置工厂setBeanFactory或者上下文对象setApplicationContext
5.如果存在类实现BeanPostProcessor执行postProcessorBeforeInitialization
6.如果Bean实现InitializingBean执行afterPropertiesSet
7.调用<bean init-method="init">指定初始化方法init
8.BeanPostProcessor执行postProcessorAfterInitialization
9.执行业务处理(自己写的方法代码)
10.如果Bean实现DisposableBean执行destroy
11.调用<bean destroy-method="destroy">指定销毁方法
代码示例:
配置文件:applicationContext.xml
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="man" class="com.imooc.ioc.demo3.Man" init-method="setup" destroy-method="teardown"> <property name="name" value="张三"/> </bean> <bean class="com.imooc.ioc.demo3.MyBeanPostProcessor"/> </beans>
Bean:Man.java
package com.imooc.ioc.demo3; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class Man implements BeanNameAware,ApplicationContextAware,InitializingBean,DisposableBean{ private String name; public String getName() { return name; } public void setName(String name) { System.out.println("第二步:设置属性"); this.name = name; } public Man(){ System.out.println("第一步:初始化"); } public void setup(){ System.out.println("第七步:MAN被初始化了"); } public void teardown(){ System.out.println("第十一步:MAN被销毁了"); } @Override public void setBeanName(String s) { System.out.println("第三步:设置Bean的名称"+s); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println("第四步:了解工厂信息"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("第六步:属性设置后"); } public void run(){ System.out.println("第九步:执行业务代码"); } @Override public void destroy() throws Exception { System.out.println("第十步:执行Spring的销毁对象"); } }
最上方5.中的所说的存在的另一个类:MyBeanPostProcessor.java
package com.imooc.ioc.demo3; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String s) throws BeansException { System.out.println("第五步:初始化前方法"); return bean; } @Override public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException { System.out.println("第八步:初始化后方法"); return bean; } }
测试类:SpringDemo3.java
package com.imooc.ioc.demo3; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringDemo3 { @Test public void demo2(){ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Man man = (Man) applicationContext.getBean("man"); man.run(); applicationContext.close(); } }
执行demo2方法,打印结果:
以上是关于Spring容器中Bean的生命周期的主要内容,如果未能解决你的问题,请参考以下文章