AbstractAutoProxyCreator#postProcessBeforeInstantiation
一、定义
postProcessBeforeInstantiation 是 Spring AOP 动态代理的核心扩展点,通过提前创建代理对象优化性能,并支持丰富的自定义逻辑(如事务、安全)
二、代码分析
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
Object cacheKey = getCacheKey(beanClass, beanName);
if (beanName == null || !this.targetSourcedBeans.contains(beanName)) {
if (this.advisedBeans.containsKey(cacheKey)) {
return null;
}
if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return null;
}
}
// Create proxy here if we have a custom TargetSource.
// Suppresses unnecessary default instantiation of the target bean:
// The TargetSource will handle target instances in a custom fashion.
if (beanName != null) {
TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
if (targetSource != null) {
this.targetSourcedBeans.add(beanName);
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
}
return null;
}
1、生成缓存键

- 作用: 根据 Bean 的类名或 Bean 名称生成唯一的缓存键,用于后续缓存操作
2、检查是否需要处理当前 Bean

作用: 确定当前 Bean 是否已经被处理过
条件解释:
beanName == null: 处理匿名 Bean。
!targetSourcedBeans.contains(beanName): 确保 Bean 未被标记为已处理。
集合说明: targetSourcedBeans: 记录已通过自定义 TargetSource 处理的 Bean,避免重复代理
3、查是否已处理 或 需跳过

分支 1: 如果缓存键已经存在于
advisedBeans中,直接返回null。这说明这个Bean已经被处理过,不需要再次处理,直接跳过分支 2:
isInfrastructureClass(beanClass): 判断是否为 Spring 内部类(如 Advisor, Advice),实现逻辑: 检查类是否实现 Advice、Advisor 等接口
shouldSkip(beanClass, beanName): 根据用户配置或注解(如 @SkipAop)决定跳过代理
标记为不代理: 将 cacheKey 对应值设为 FALSE,避免重复检查
4、处理自定义 TargetSource

作用: 检查是否有自定义的 TargetSource,若有则创建代理。
getCustomTargetSource 逻辑:
查找 Bean 定义中是否指定了 TargetSource。
示例:通过 @Scope 配置 proxyMode 或 XML 中定义 target-source 属性。
示例场景:
- 动态数据源切换: 根据请求上下文动态选择数据源实现。
5、创建代理对象

步骤详解:
1、标记为已处理: 将 beanName 加入 targetSourcedBeans。
2、获取拦截器:
getAdvicesAndAdvisorsForBean: 收集匹配该 Bean 的所有拦截器(如事务、日志 Advice)。
匹配逻辑: 根据切点表达式(Pointcut)判断拦截器是否适用
3、生成代理:
createProxy: 根据配置选择 JDK 动态代理或 CGLIB。
JDK 代理: Bean 实现至少一个接口。
CGLIB 代理: Bean 无接口或强制使用 CGLIB。
参数 SingletonTargetSource: 确保代理持有单例目标对象
6、默认返回 null

作用: 若无自定义 TargetSource 或无需代理,返回 null,触发 Spring 默认实例化流程。
后续流程: Spring 将调用构造器创建 Bean,进行属性注入和初始化。
AbstractAutoProxyCreator#postProcessBeforeInstantiation的更多相关文章
- spring异常-aoperror at :0 formal unbound in pointcut
八月 17, 2016 10:15:21 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRe ...
- Java AOP nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice || Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0' 两个异常解决办法
贴出applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...
- Pointcut is not well-formed: expecting 'identifier' at character position 0
异常如下: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDa ...
- Spring AOP报错
八月 01, 2016 10:08:48 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRe ...
- Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWo
1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...
- 解决 spring-cloud-starter-zipkin 启动错误
应用场景:Spring Boot 服务添加 Zipkin 依赖,进行服务调用的数据采集,然后进行 Zipkin-Server 服务调用追踪显示. 示例pom.xml配置: <parent> ...
- Spring中的AOP 专题
Caused by: java.lang.IllegalArgumentException: ProceedingJoinPoint is only supported for around advi ...
- 开发Spring过程中几个常见异常(二):Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'a' define
本异常是小编在运行自己另外一篇博文中的例子时遇到的.(附博文:http://www.cnblogs.com/dudududu/p/8482487.html) 完整异常信息: 警告: Exception ...
- spring源码分析之初始化过程
1.org.springframework.web.context.ContextLoaderListener 一个ServletContextListener,web容器启动监听器 1.1内有成员C ...
- Linux下Tomcat项目启动报错
Linux下Tomcat项目启动报错 org.springframework.beans.factory.CannotLoadBeanClassException: Error loading cla ...
随机推荐
- 再谈Redux
2025年再聊前端状态管理似乎是一件不必要的事,毕竟相关文章已堆积得如山如海.但在这些文章或视频内容中,我并没有找到自己喜欢的方案,准确的说是使用方式.所以这篇文章不做技术分析,主要聊聊个人对状态管理 ...
- e-prime3安装
e-prime2.0版本太老,现在安装尝试3.0. 下载 链接: https://pan.baidu.com/s/1XJFDqhoArpIwEf0NpKvoIQ 提取码: h5xk 安装 解压安装包后 ...
- 「NOIP2024」 树上查询
update 2024/12/28 题目描述 给定一棵树,每次询问区间 \([l,r]\) 的 \[\max_{l \le l' \le r' \le r \land r' - l' + 1 \ge ...
- 调试存储过程中出现 [Microsoft][ODBC SQL Server Driver]对于造型说明无效的字符值
调试存储时如果有日期类型的参数,传入格式为:2020-07-13 12:00:00 ,无需用引号括起来. 否则会提示[Microsoft][ODBC SQL Server Driver]对于造型说明无 ...
- dart中所有的循环详解
List MyList = ['苹果', '栗子', '小苹果']; for (var i = 0; i < MyList.length; i++) { print(MyList[i]); } ...
- 从存钱罐到子数组:一个关于累加和的精妙问题|LeetCode 560 和为K的子数组
LeetCode 560 和为K的子数组 点此看全部题解 LeetCode必刷100题:一份来自面试官的算法地图(题解持续更新中) 生活中的算法 你有没有这样的经历:每天往存钱罐里存一些零钱,某一天突 ...
- 天翼云存储资源盘活系统HBlock,全面释放企业数据价值
9月6日,天翼云与科技媒体InfoQ联合举办的以"存储难题新解法,揭秘极/致易用的HBlock"为主题的线上技术分享会圆满落幕.天翼云国际业务事业部研发专家武志民与存储产品线总监魏 ...
- Django项目实战:解除跨域限制
Django项目实战:解除跨域限制 在Web开发中,跨域资源共享(CORS)是一个重要的安全特性,它限制了网页只能与其同源的服务器进行交互.然而,在开发过程中,我们经常需要前端(如Vue.js.Rea ...
- SQL注入的业务场景以及危害
SQL注入的业务场景以及危害 在现代Web应用中,数据库是存储和检索数据的核心组件.然而,当Web应用未能正确验证和过滤用户输入时,就可能会遭受SQL注入攻击.SQL注入是一种严重的安全漏洞,它允许攻 ...
- 告别 DeepSeek 系统繁忙,七个 DeepSeek 曲线救国平替入口,官网崩溃也能用!
前言 DeepSeek作为一款备受瞩目的国产大模型,以其强大的功能和卓越的性能赢得了众多用户的青睐.然而,随着用户量的激增,DeepSeek官网近期频繁遭遇服务器繁忙甚至崩溃的问题,给广大用户带来了不 ...