- 109
- 0
AbstractAutoProxyCreator类的postProcessBeforeInstantiation
@Override
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;
}
protected TargetSource getCustomTargetSource(Class<?> beanClass, String beanName) {
// We can't create fancy target sources for directly registered singletons.
if (this.customTargetSourceCreators != null &&
this.beanFactory != null && this.beanFactory.containsBean(beanName)) {
for (TargetSourceCreator tsc : this.customTargetSourceCreators) {
TargetSource ts = tsc.getTargetSource(beanClass, beanName);
if (ts != null) {
// Found a matching TargetSource.
if (logger.isDebugEnabled()) {
logger.debug("TargetSourceCreator \[" \+ tsc +
" found custom TargetSource for bean with name '" \+ beanName + "'");
}
return ts;
}
}
}
// No custom TargetSource found.
return null;
}
问题:
TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
这行代码的作用是什么? 什么情况下发挥作用?
0
打赏
收藏
点击回答
- 共 0 条
- 全部回答
-
要走就滚别墨迹 普通会员 1楼在Spring AOP的源码中,`getCustomTargetSource()`方法主要是在 AdvisorChainFactoryBean 类或者相关的代理创建逻辑中出现,用于获取自定义的目标源(TargetSource)。 `TargetSource`是Spring AOP的一个核心接口,它代表了被代理对象的来源,定义了如何获取、释放目标对象以及是否为每个方法调用都创建新的目标实例等行为。 `getCustomTargetSource()`方法通常由开发者自定义实现,其目的是根据特定的条件或策略来返回一个自定义的`TargetSource`对象。例如,可能根据目标类的类型、注解或其他属性来决定返回何种类型的`TargetSource`。 例如,在某些场景下,你可能需要使用PrototypeTargetSource来为每次方法调用都创建一个新的目标对象实例,或者使用ThreadLocalTargetSource来为每个线程维持一个单独的目标对象实例,这些特殊的TargetSource就需要通过`getCustomTargetSource()`这样的方法进行获取和配置。 具体到Spring AOP框架内部并没有一个固定的`getCustomTargetSource()`方法实现,而是需要开发者按照实际需求去编写相应逻辑。
更多回答
网站公告
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
