bean装载到Spring应用上下文的生命周期
在基于spring的应用中,应用对象生存于spring容器中,spring容器负责创建对象,装配对象,配置对象,并管理对象的整个生命周期,从生存到死亡。
bean装载到spring应用上下文中的生命周期过程如图所示:
过程解释:
1. spring对bean进行实例化;
2. spring将值和bean的引用注入到bean对应的属性中;
3. 如果bean实现了beannameaware接口,spring将bean的id传递给setbeanname()方法;
4. 如果bean实现了beanfactoryaware接口,spring将调用setbeanfactory()方法,将beanfactory容器实例传入;
5. 如果bean实现了applicationcontextaware接口,spring将调用setapplicationcontext()方法,将bean所在的应用上下文的引用传入进来;
6. 如果bean实现了beanpostprocessor接口,spring将调用它们的postprocessbeforeiniitialization()方法;
7. 如果bean实现了initializingbean接口,spring将调用它们的afterpropertiesset()方法。
8. 如果bean使用initmethod声明了初始化方法,该方法也会被调用;
9. 如果bean实现了beanpostprocessor接口,spring将调用的postprocessafterinitialization()方法;
10. 此时,bean已经准备就绪,可以被应用程序使用了,它们将一直驻留在应用上下文中,直到该应用上下文被销毁;
11. 如果bean实现了disposablebean接口,spring将调用它的destroy()接口方法。
12. 如果bean使用destroy-method声明了销毁方法,该方法也会被调用。
测试代码:
package beanlifecycle; import org.springframework.beans.beansexception; import org.springframework.beans.factory.*; import org.springframework.context.applicationcontext; import org.springframework.context.applicationcontextaware; /** * bean的定义 */ public class myspringbeanlifecycle implements beannameaware, beanfactoryaware, applicationcontextaware, initializingbean, disposablebean { private applicationcontext applicationcontext; public myspringbeanlifecycle() { system.out.println("实例化 myspringbeanlifecycle ..."); } @override public void setbeanname(string s) { system.out.println("beannameaware -> setbeanname ..."); } @override public void setbeanfactory(beanfactory beanfactory) throws beansexception { system.out.println("beanfactoryaware --> setbeanfactory ..."); } @override public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception { system.out.println("applicationcontextaware -> setapplicationcontext ..."); } @override public void afterpropertiesset() throws exception { system.out.println("initializingbean -> afterpropertiesset ..."); } @override public void destroy() throws exception { system.out.println("disposablebean -> destroy..."); } public void custominit() { system.out.println("自定义初始化方法..."); } public void customdestroy() { system.out.println("自定义销毁方法..."); } }
package beanlifecycle; import org.springframework.beans.beansexception; import org.springframework.beans.factory.config.beanpostprocessor; /** * 实现beanpostprocessor接口 */ public class myspringbeanpostprocessor implements beanpostprocessor { @override public object postprocessbeforeinitialization(object bean, string beanname) throws beansexception { if (bean instanceof myspringbeanlifecycle) { system.out.println("beanpostprocessor -> postprocessbeforeinitialization ..."); } return bean; } @override public object postprocessafterinitialization(object bean, string beanname) throws beansexception { if (bean instanceof myspringbeanlifecycle) { system.out.println("beanpostprocessor -> postprocessafterinitialization ..."); } return bean; } }
package beanlifecycle; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; /** * bean配置类 */ @configuration public class beanlifecycleconfig { @bean(initmethod = "custominit", destroymethod = "customdestroy") public myspringbeanlifecycle myspringbeanlifecycle() { return new myspringbeanlifecycle(); } @bean public myspringbeanpostprocessor myspringbeanpostprocessor() { return new myspringbeanpostprocessor(); } }
package beanlifecycle; import org.springframework.context.annotation.annotationconfigapplicationcontext; public class main { public static void main(string[] args) { annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(beanlifecycleconfig.class); context.getbean(myspringbeanlifecycle.class); context.destroy(); } }
运行结果
实例化 myspringbeanlifecycle ... beannameaware -> setbeanname ... beanfactoryaware --> setbeanfactory ... applicationcontextaware -> setapplicationcontext ... beanpostprocessor -> postprocessbeforeinitialization ... initializingbean -> afterpropertiesset ... 自定义初始化方法... beanpostprocessor -> postprocessafterinitialization ... disposablebean -> destroy... 自定义销毁方法...
参考《spring实战(第4版)》
上一篇: 冬季养生防病五秘方 按摩可促进血液循环
下一篇: 肾炎能喝醋吗,恢复期该吃什么