首页 > 编程知识 正文

spring的生命周期的六个阶段,spring所有注解及其作用

时间:2023-05-05 23:26:35 阅读:182257 作者:3798

目录

1 .评论说明

2 .呼叫流程

3 .调用分析

1 .注释说明@PostConstruct,@PreDestroy是由Java规范JSR-250引入的注释,定义了对象的创建和销毁工作。 同一期间的规格还支持注释@Resource,Spring;

在Spring中,@PostConstruct,@PreDestroy注释的分析通过BeanPostProcessor实现, 具体的分析类为org.spring framework.context.annotation.commononotation,其父类为org.spring framework.beans.factory.annotatation

spring ' sorg.spring framework.context.annotation.commonannotationbeanpostprocessorsupportsthejsr-250 javax.annotation 、asinitannotationanddestroyannotation,respectively. Furthermore,italsosupportsthejavax.annotation.resourceannnotation

2 .调用过程的具体过程是,IOC容器先解析各组件的定义信息,解析到@PostConstruct,@PreDestroy时,定义为生命周期相关的方法,将组件的定义信息组成组在创建组件时创建组件并分配属性,然后在执行各种初始化方法之前,从容器中为所有BeanPostProcessor创建实现类(initdestroyannotations ) 执行所有BeanPostProcessor的postprocessbeforeinitialization方法由initdestroyannotationbeanpostprocessor中的@PostConstruct限定

3 .呼叫分析@PostConstruct的呼叫链如下:

org.spring framework.beans.factory.support.abstractautowirecapablebeanfactory.initialize bean (string,Object )、 Object先org.spring framework.beans.factory.config.beanpostprocessor.postprocessbeforeinitialization (object,String )

protectedobjectinitializebean (finalstringbeanname,final Object bean, rootbeandefinitionmbd (if (system.getsecuritymanage=null ) access controller.do privileged (newprivilegedactionobject ) emethododect () (overrridepublicobjectrun ) )返回空值; },getAccessControlContext (); } else { invokeawaremethods (beanname,bean ); }Object wrappedBean=bean; if (mbd==空

|| !mbd.isSynthetic()) {// 在执行初始化方法之前:先执行org.springframework.beans.factory.config.BeanPostProcessor.postProcessBeforeInitialization(Object, String)方法wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);}try {//执行InitializingBean的初始化方法和init-method指定的初始化方法invokeInitMethods(beanName, wrappedBean, mbd);}catch (Throwable ex) {throw new BeanCreationException((mbd != null ? mbd.getResourceDescription() : null),beanName, "Invocation of init method failed", ex);}if (mbd == null || !mbd.isSynthetic()) {wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);}return wrappedBean;}

org.springframework.beans.factory.config.BeanPostProcessor.postProcessBeforeInitialization(Object, String)的说明如下:

Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method). The bean will already be populated with property values. The returned bean instance may be a wrapper around the original.

调用时机: 在组件创建完属性复制完成之后,调用组件初始化方法之前;

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(Object, String)的具体流程如下:

@Overridepublic Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)throws BeansException {Object result = existingBean;for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {//遍历所有BeanPostProcessor的实现类,执行BeanPostProcessor的postProcessBeforeInitialization//在InitDestroyAnnotationBeanPostProcessor中的实现是找出@PostConstruct标记的方法的定义信息,并执行result = beanProcessor.postProcessBeforeInitialization(result, beanName);if (result == null) {return result;}}return result;}

@PreDestroy调用链如下:

@PreDestroy是通过org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor.postProcessBeforeDestruction(Object, String)被调用(InitDestroyAnnotationBeanPostProcessor实现了该接口),该方法的说明如下:

Apply this BeanPostProcessor to the given bean instance before its destruction. Can invoke custom destruction callbacks.

Like DisposableBean's destroy and a custom destroy method, this callback just applies to singleton beans in the factory (including inner beans).

调用时机: 该方法在组件的销毁之前调用;

 

org.springframework.beans.factory.support.DisposableBeanAdapter.destroy()的执行流程如下: 

@Overridepublic void destroy() {if (!CollectionUtils.isEmpty(this.beanPostProcessors)) {//调用所有DestructionAwareBeanPostProcessor的postProcessBeforeDestruction方法for (DestructionAwareBeanPostProcessor processor : this.beanPostProcessors) {processor.postProcessBeforeDestruction(this.bean, this.beanName);}}if (this.invokeDisposableBean) {if (logger.isDebugEnabled()) {logger.debug("Invoking destroy() on bean with name '" + this.beanName + "'");}try {if (System.getSecurityManager() != null) {AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {@Overridepublic Object run() throws Exception {((DisposableBean) bean).destroy();return null;}}, acc);}else {//调用DisposableBean的销毁方法((DisposableBean) bean).destroy();}}catch (Throwable ex) {String msg = "Invocation of destroy method failed on bean with name '" + this.beanName + "'";if (logger.isDebugEnabled()) {logger.warn(msg, ex);}else {logger.warn(msg + ": " + ex);}}}//调用自定义的销毁方法if (this.destroyMethod != null) {invokeCustomDestroyMethod(this.destroyMethod);}else if (this.destroyMethodName != null) {Method methodToCall = determineDestroyMethod();if (methodToCall != null) {invokeCustomDestroyMethod(methodToCall);}}}

所以是先调用DestructionAwareBeanPostProcessor的postProcessBeforeDestruction(@PreDestroy标记的方法被调用),再是DisposableBean的destory方法,最后是自定义销毁方法;

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。