(五)Spring AOP建立代理對象

@EnableAspectJAutoProxy註解的介紹

使用@EnableAspectJAutoProxy註解開啓AOP後,此註解會像容器中添加一個AnnotationAwareAspectJAutoProxyCreator類型的beanjava

此bean的繼承體系如圖post

AnnotationAwareAspectJAutoProxyCreator是一個BeanPostProcessorui

BeanPostProcessor接口方法的實如今AbstractAutoProxyCreatorthis

將此bean添加到容器中後,在建立其餘bean時調用後置處理器時若是有須要,AnnotationAwareAspectJAutoProxyCreator會調用postProcessAfterInitialization方法爲bean建立代理對象lua

後置處理器建立bean的代理對象

public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) throws BeansException {
    if (bean != null) {
        Object cacheKey = getCacheKey(bean.getClass(), beanName);
        if (!this.earlyProxyReferences.contains(cacheKey)) {
            // 若是有須要就爲bean建立一個代理對象並返回
            return wrapIfNecessary(bean, beanName, cacheKey);
        }
    }
    return bean;
}
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
    if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
        return bean;
    }
    if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
        return bean;
    }
    if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
        this.advisedBeans.put(cacheKey, Boolean.FALSE);
        return bean;
    }

    // 獲取此bean須要的通知器
    Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
    // 若是specificInterceptors!=null就爲bean建立代理對象
    if (specificInterceptors != DO_NOT_PROXY) {
        this.advisedBeans.put(cacheKey, Boolean.TRUE);
        // 爲bean建立代理對象
        Object proxy = createProxy(
            bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
        this.proxyTypes.put(cacheKey, proxy.getClass());
        return proxy;
    }

    this.advisedBeans.put(cacheKey, Boolean.FALSE);
    return bean;
}

createProxy()方法將會調用Proxyfactory類型的對象建立代理對象設計

AopProxy代理對象

Spring AOP建立的代理對象是AopProxy類型的代理

AopProxy的實現類分兩種code

Jdk實現的動態代理和Cglib實現的動態代理對象

Proxyfactory的設計

  • ProcyConfig 提供默認配置
  • AdvisedSupport 用於保存,管理通知器
  • ProxyCreatorSupport擁有建立代理對象的功能,且持有一個ProxyFactory類型的成員變量

ProxyFactory建立代理對象

createProxy()方法調用者是AnnotationAwareAspectJAutoProxyCreatorblog

方法實如今其父類AbstractAutoProxyCreator

protected Object createProxy(Class<?> beanClass, @Nullable String beanName, @Nullable Object[] specificInterceptors, TargetSource targetSource) {

    if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
        AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
    }

    // 由代理工廠建立代理對象
    ProxyFactory proxyFactory = new ProxyFactory();
    // 將AnnotationAwareAspectJAutoProxyCreator做爲ProxyConfig賦給ProxyFactory作配置
    proxyFactory.copyFrom(this);

    if (!proxyFactory.isProxyTargetClass()) {
        if (shouldProxyTargetClass(beanClass, beanName)) {
            proxyFactory.setProxyTargetClass(true);
        }
        else {
            evaluateProxyInterfaces(beanClass, proxyFactory);
        }
    }

    Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
    // 將全部通知器添加到proxyFactory中(添加到父類AdvisedSupport的this.advisors變量中)
    proxyFactory.addAdvisors(advisors);
    proxyFactory.setTargetSource(targetSource);
    customizeProxyFactory(proxyFactory);

    proxyFactory.setFrozen(this.freezeProxy);
    if (advisorsPreFiltered()) {
        proxyFactory.setPreFiltered(true);
    }
	
    // 用ProxyFactory建立代理對象並返回
    return proxyFactory.getProxy(getProxyClassLoader());
}
public Object getProxy(@Nullable ClassLoader classLoader) {
    // createAopProxy()建立代理對象並將AdvisedSupport傳遞給建立的AopProxy對象
    // getProxy()將通知器織入到目標類中
    return createAopProxy().getProxy(classLoader);
}

protected final synchronized AopProxy createAopProxy() {
    if (!this.active) {
        activate();
    }
    // this是ProxyFactory對象,同時也是AdvisedSupport,對象中保存了通知器
    return getAopProxyFactory().createAopProxy(this);
}

// 此方法建立代理對象
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
    if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
        Class<?> targetClass = config.getTargetClass();
        if (targetClass == null) {
            throw ...
        }
        if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
            return new JdkDynamicAopProxy(config);
        }
        return new ObjenesisCglibAopProxy(config);
    }
    else {
        return new JdkDynamicAopProxy(config);
    }
}
相關文章
相關標籤/搜索