spring中BeanPostProcessor之三:InitDestroyAnnotationBeanPostProcessor(01)
在《spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)》一文中,分析到在调用CommonAnnotationBeanPostProcessor类的postProcessMeredBeanDefinition方法时会先调用其父类的postProcessMeredBeanDefinition方法,下面就来分析CommonAnnotationBeanPostProcessor类的父类InitDestroyAnnoatationBeanPostProcessor。
一、概述
我们先看下CommonAnnotationAnnotationBeanPostProcessor类,上篇中介绍到该类有一个静态代码块会在类初始化的时候调用,在该类中还有一个默认的构造函数,
public CommonAnnotationBeanPostProcessor() {
setOrder(Ordered.LOWEST_PRECEDENCE - 3);
//@PostConstruct注解,作用在方法上
setInitAnnotationType(PostConstruct.class);
//@PreDestroy注解,作用在方法上
setDestroyAnnotationType(PreDestroy.class);
ignoreResourceType("javax.xml.ws.WebServiceContext");
}
该构造函数中分别调用了setInitAnnotationType和setDestroyAnnotationType方法,两个方法分别调用其父类的方法,
/**
* Specify the init annotation to check for, indicating initialization
* methods to call after configuration of a bean.
* <p>Any custom annotation can be used, since there are no required
* annotation attributes. There is no default, although a typical choice
* is the JSR-250 {@link javax.annotation.PostConstruct} annotation.
*/
public void setInitAnnotationType(Class<? extends Annotation> initAnnotationType) {
this.initAnnotationType = initAnnotationType;
} /**
* Specify the destroy annotation to check for, indicating destruction
* methods to call when the context is shutting down.
* <p>Any custom annotation can be used, since there are no required
* annotation attributes. There is no default, although a typical choice
* is the JSR-250 {@link javax.annotation.PreDestroy} annotation.
*/
public void setDestroyAnnotationType(Class<? extends Annotation> destroyAnnotationType) {
this.destroyAnnotationType = destroyAnnotationType;
}
分别给的InitDestroyAnnotationBeanPostProcessor的initAnnotationType和destroyAnnotationType赋值@PostConstruct和@PreDestroy注解。
二、详述
上面提到在调用CommonAnnotationBeanPostProcessor类的postProcessMeredBeanDefinition方法时会先调用父类的方法,下面看CommonAnnotationBeanPostProcessor的方法,
@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
super.postProcessMergedBeanDefinition(beanDefinition, beanType, beanName);
InjectionMetadata metadata = findResourceMetadata(beanName, beanType, null);
metadata.checkConfigMembers(beanDefinition);
}
上面是CommonAnnotationBeanPostProcessor的postProcessMergedBeanDefinition方法,可以看到调用了父类的postProcessMergedBeanDefinition方法,其他的方法在上篇博客中已经分析,来重点分析InitDestroyAnnotationBeanPostProcessor的postProcessMergedBeanDefinition方法,
@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
LifecycleMetadata metadata = findLifecycleMetadata(beanType);
metadata.checkConfigMembers(beanDefinition);
}
在方法中调用了findLifecycleMetadata和checkConfigMembers方法,
private LifecycleMetadata findLifecycleMetadata(Class<?> clazz) {
//判断lifecycleMetadataCache是否为空
if (this.lifecycleMetadataCache == null) {
// Happens after deserialization, during destruction...
return buildLifecycleMetadata(clazz);
}
// Quick check on the concurrent map first, with minimal locking.
//从lifecycleMetadataCache中取,如果存在则直接返回,不存在则生成一个LifecycleMetadata对象并返回
LifecycleMetadata metadata = this.lifecycleMetadataCache.get(clazz);
if (metadata == null) {
synchronized (this.lifecycleMetadataCache) {
metadata = this.lifecycleMetadataCache.get(clazz);
if (metadata == null) {
metadata = buildLifecycleMetadata(clazz);
this.lifecycleMetadataCache.put(clazz, metadata);
}
return metadata;
}
}
return metadata;
}
在findLifecycleMetadata方法中主要调用的是buildLifecycleMetadata方法,改方法的返回值是LifecycleMetadata对象,之后把该对象放到lifecycleMetadataCache中,那么这里lifecycleMetadata应该是Map类型的,
@Nullable
private final transient Map<Class<?>, LifecycleMetadata> lifecycleMetadataCache = new ConcurrentHashMap<>(256);
buildLifecycleMetadataf方法如下,
private LifecycleMetadata buildLifecycleMetadata(final Class<?> clazz) {
if (!AnnotationUtils.isCandidateClass(clazz, Arrays.asList(this.initAnnotationType, this.destroyAnnotationType))) {
return this.emptyLifecycleMetadata;
}
List<LifecycleElement> initMethods = new ArrayList<>();
List<LifecycleElement> destroyMethods = new ArrayList<>();
Class<?> targetClass = clazz;
do {
final List<LifecycleElement> currInitMethods = new ArrayList<>();
final List<LifecycleElement> currDestroyMethods = new ArrayList<>();
//判断方法上是否存在initAnnotationType的注解,这里指的就是@PostConstruct注解
ReflectionUtils.doWithLocalMethods(targetClass, method -> {
if (this.initAnnotationType != null && method.isAnnotationPresent(this.initAnnotationType)) {
LifecycleElement element = new LifecycleElement(method);
currInitMethods.add(element);
if (logger.isTraceEnabled()) {
logger.trace("Found init method on class [" + clazz.getName() + "]: " + method);
}
}
//判断方法上是否存在destroyAnnotationType的注解,这里指的就是@PreDestroy注解
if (this.destroyAnnotationType != null && method.isAnnotationPresent(this.destroyAnnotationType)) {
currDestroyMethods.add(new LifecycleElement(method));
if (logger.isTraceEnabled()) {
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 LifecycleMetadata(clazz, initMethods, destroyMethods));
}
从上面的分析,可以知道InitDestroyAnnotationBeanPostProcessor的postProcessMergedBeanDefinition方法完成的功能就是把类中含有@PostConstrunct和@PreDestroy注解的方法信息缓存到lifecycleMetadataCache中。也就是说在一个被spring管理的类中允许定义被这两个注解修饰的方法,那方法有那些要求那,看上面的new LifecycleElement这里,看LifecycleElement类
public LifecycleElement(Method method) {
if (method.getParameterCount() != 0) {
throw new IllegalStateException("Lifecycle method annotation requires a no-arg method: " + method);
}
this.method = method;
this.identifier = (Modifier.isPrivate(method.getModifiers()) ?
ClassUtils.getQualifiedMethodName(method) : method.getName());
}
看上面的异常,意思是不允许此类方法有方法参数,如果有参数则会抛异常。
三、使用场景
上面分析了InitDestroyAnnotationBeanPostProcessor类中postProcessMergedBeanDefinition方法的作用,就是把类中有@PostConstruct、@PreDestroy两个注解的方法信息进行缓存,至于这两个注解的作用及被这两个注解标记的方法何时调用后面会继续分析。
原创不易,有不正之处欢迎指正。
spring中BeanPostProcessor之三:InitDestroyAnnotationBeanPostProcessor(01)的更多相关文章
- spring中BeanPostProcessor之四:AutowiredAnnotationBeanPostProcessor(01)
在<spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)>中分析了CommonAnnotationBeanPos ...
- spring中BeanPostProcessor之一:InstantiationAwareBeanPostProcessor(01)
在spring中beanPostProcessor绝对是开天辟地的产物,给了程序员很多自主权,beanPostProcessor即常说的bean后置处理器. 一.概览 先来说下Instantiatio ...
- Spring中BeanPostProcessor
Spring中BeanPostProcessor 前言: 本文旨在介绍Spring动态配置数据源的方式,即对一个DataSource的配置诸如jdbcUrl,user,password,driverC ...
- spring中BeanPostProcessor之二:CommonAnnotationBeanPostProcessor(01)
在上篇博客中分享了InstantiationAwareBeanPostProcessor接口中的四个方法,分别对其进行了详细的介绍,在文末留下了一个问题,那就是postProcessPropertie ...
- spring(三):spring中BeanPostProcessor的使用
spring中实现BeanPostProcessor的后置处理器 ApplicationContextAwareProcessor 进入该实现类内部 可以看到:该类帮我们组建IOC容器,判断我们的be ...
- spring中BeanPostProcessor之一:InstantiationAwareBeanPostProcessor(03)
前面介绍了InstantiationAwareBeanPostProcessor后置处理器的postProcessBeforeInstantiation和postProcessAfterInstant ...
- spring中BeanPostProcessor之一:InstantiationAwareBeanPostProcessor(02)
在上篇博客中写道了bean后置处理器InstantiationAwareBeanPostProcessor,只介绍了其中一个方法的作用及用法,现在来看postProcessBeforeInstanti ...
- Spring中的BeanPostProcessor
一.何谓BeanProcessor BeanPostProcessor是SpringFramework里非常重要的核心接口之一,我先贴出一段源代码: /* * Copyright 2002-2015 ...
- Spring中的BeanPostProcessor详解
Spring中的BeanPostProcessor详解 概述 BeanPostProcessor也称为Bean后置处理器,它是Spring中定义的接口,在Spring容器的创建过程中(具体为Bean初 ...
随机推荐
- Java 14 开箱,它真香香香香
Java 14 已经发布有一周时间了,我准备来开个箱,和小伙伴们一起来看看新特性里面都有哪些好玩的.我们程序员应该抱着尝鲜.猎奇的心态,否则就容易固步自封,技术停滞不前.先来看看 Java 14 都有 ...
- redis相关命令及应用场景
Redis的应用场景 (1) 配合关系型数据库做高速缓存 l 高频次,热门访问的数据,降低数据库IO l 高频次,热门访问的数据,降低数据库IO (2) 由于其拥有 ...
- vue你真棒
引子:什么是前后端分离和前后端不分离? 前后端分离指的是后端开发人员只负责用来书写后端逻辑代码,不用再去管前端页面的搭建,前端人员只负责做好前端页面效果,不用管数据,数据直接向后端人员要,后端和前端通 ...
- 解决vscode 没有 c++11 的代码提示(如to_string()等)
2019.5.4 更新: 参考了stackflow上的一个问题:to_string is not a member of std, says g++ (mingw),发现直接换新版mingw即可- m ...
- 第七周java实验作业
实验七 继承附加实验实验时间 2018-10-11 1.实验目的与要求 (1)进一步理解4个成员访问权限修饰符的用途: Public 该类或非该类均可访问 Private 只有该类可以访问 Pro ...
- Linux基本操作 ------ 文件处理命令
显示目录文件 ls //显示当前目录下文件 ls /home //显示home文件夹下文件 ls -a //显示当前目录下所有文件,包括隐藏文件 ls -l //显示当前目录下文件的详细信息 ls - ...
- Asp.Net Core 中IdentityServer4 实战之角色授权详解
一.前言 前几篇文章分享了IdentityServer4密码模式的基本授权及自定义授权等方式,最近由于改造一个网关服务,用到了IdentityServer4的授权,改造过程中发现比较适合基于Role角 ...
- 配置ssh免密登录遇到的问题——使用VMware多虚拟机搭建Hadoop集群
搭建环境: 虚拟机 VMware12Pro 操作系统 centos6.8 hadoop 1.2.1 1.导入镜像文件,添加java环境 1.查看当前系统中安装的java,ls ...
- TensorFlow v2.0的基本张量操作
使用TensorFlow v2.0的基本张量操作 from __future__ import print_function import tensorflow as tf # 定义张量常量 a = ...
- 解析PE文件
最近在自学解析PE文件,根据小辣椒(CFF Explorer)以及各论坛上大佬的帖子,做了个黑屏打印PE文件的,历时7天完成,在此想跟有相关需要的同学们分享下思路,有不足之处也希望大家不吝赐教,指点出 ...