@PostConstruct

@PostConstruct

initializeBean方法–> PostProcessor.postProcessBeforeInitialization–> InitDestroyAnnotationBeanPostProcessor.postProcessBeforeDestruction

被@PostConstruct注解的方法会在Bean初始化的时候被调用,如下图:
在这里插入图片描述
继承关系如下图:
在这里插入图片描述
所以到bean初始化时,会调用到InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization方法。
在这里插入图片描述
在这里插入图片描述

  1. InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization–>findLifecycleMetadata->buildLifecycleMetadata
  2. 通过method.isAnnotationPresent(this.initAnnotationType)判断是否有@PostConstruct,将其方法放入initMethods
 private InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata buildLifecycleMetadata(Class<?> clazz) {
        if (!AnnotationUtils.isCandidateClass(clazz, Arrays.asList(this.initAnnotationType, this.destroyAnnotationType))) {
            return this.emptyLifecycleMetadata;
        } else {
            List<InitDestroyAnnotationBeanPostProcessor.LifecycleElement> initMethods = new ArrayList();
            List<InitDestroyAnnotationBeanPostProcessor.LifecycleElement> destroyMethods = new ArrayList();
            Class targetClass = clazz;

            do {
                List<InitDestroyAnnotationBeanPostProcessor.LifecycleElement> currInitMethods = new ArrayList();
                List<InitDestroyAnnotationBeanPostProcessor.LifecycleElement> currDestroyMethods = new ArrayList();
                ReflectionUtils.doWithLocalMethods(targetClass, (method) -> {
                    if (this.initAnnotationType != null && method.isAnnotationPresent(this.initAnnotationType)) {
                        InitDestroyAnnotationBeanPostProcessor.LifecycleElement element = new InitDestroyAnnotationBeanPostProcessor.LifecycleElement(method);
                        currInitMethods.add(element);
                        if (this.logger.isTraceEnabled()) {
                            this.logger.trace("Found init method on class [" + clazz.getName() + "]: " + method);
                        }
                    }

                    if (this.destroyAnnotationType != null && method.isAnnotationPresent(this.destroyAnnotationType)) {
                        currDestroyMethods.add(new InitDestroyAnnotationBeanPostProcessor.LifecycleElement(method));
                        if (this.logger.isTraceEnabled()) {
                            this.logger.trace("Found destroy method on class [" + clazz.getName() + "]: " + method);
                        }
                    }

                });
                initMethods.addAll(0, currInitMethods);
                destroyMethods.addAll(currDestroyMethods);
                targetClass = targetClass.getSuperclass();
            } while(targetClass != null && targetClass != Object.class);

            return initMethods.isEmpty() && destroyMethods.isEmpty() ? this.emptyLifecycleMetadata : new InitDestroyAnnotationBeanPostProcessor.LifecycleMetadata(clazz, initMethods, destroyMethods);
        }
    }
  1. initAnnotationType在子类CommonAnnotationBeanPostProcessor的构造方法中被初始化
 public CommonAnnotationBeanPostProcessor() {
    this.setOrder(2147483644);
    this.setInitAnnotationType(PostConstruct.class);
    this.setDestroyAnnotationType(PreDestroy.class);
    this.ignoreResourceType("javax.xml.ws.WebServiceContext");
    if (jndiPresent) {
        this.jndiFactory = new SimpleJndiBeanFactory();
    }

}
  1. InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization–>metadata.invokeDestroyMethods(bean, beanName)。在该方法中会调用被@PostConstruct注解的方法,即第二步initMethods中的方法。
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值