账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Spring Aop源码提问--getCustomTargetSource
    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()`方法实现,而是需要开发者按照实际需求去编写相应逻辑。
    更多回答
    网站公告
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部