Spring源码解析(十)-AopProxy对象生成过程

getObject方法式ProxyFactoryBean创建AOPProxy代理的入口方法

ProxyFactoryBean的getObject方法

   public Object getObject() throws BeansException {
        //初始化通知器链
        initializeAdvisorChain();
        //目标对象是单态模式
        if (isSingleton()) {
            return getSingletonInstance();
        }
        else {
            if (this.targetName == null) {
                logger.warn("Using non-singleton proxies with singleton targets is often undesirable. " +
                        "Enable prototype proxies by setting the 'targetName' property.");
            }
            //目标对象是原型模式
            return newPrototypeInstance();
        }
    }

initializeAdvisorChain方法

   private synchronized void initializeAdvisorChain() throws AopConfigException, BeansException {
        //通知器链是否初始化完成的标识
        //换句话说初始化工作只会发生在应用第一次通过ProxyFactoryBean去获取代理对象的时候
        if (this.advisorChainInitialized) {
            return;
        }
        //判断ProxyFactoryBean中配置的连接器列名名称不为空(参考上一节的配置文件)
        if (!ObjectUtils.isEmpty(this.interceptorNames)) {
            if (this.beanFactory == null) {
                throw new IllegalStateException("No BeanFactory available anymore (probably due to serialization) " +
                        "- cannot resolve interceptor names " + Arrays.asList(this.interceptorNames));
            }

            //全局通知器不能是通知器链中最后一个,除非显式使用属性指定了目标 
            if (this.interceptorNames[this.interceptorNames.length - 1].endsWith(GLOBAL_SUFFIX) &&
                    this.targetName == null && this.targetSource == EMPTY_TARGET_SOURCE) {
                throw new AopConfigException("Target required after globals");
            }

            //添加Advisor链的调用,通过interceptorNames属性进行配置.
            for (String name : this.interceptorNames) {
                if (logger.isTraceEnabled()) {
                    logger.trace("Configuring advisor or advice '" + name + "'");
                }

                if (name.endsWith(GLOBAL_SUFFIX)) {
                    if (!(this.beanFactory instanceof ListableBeanFactory)) {
                        throw new AopConfigException(
                                "Can only use global advisors or interceptors with a ListableBeanFactory");
                    }
                    addGlobalAdvisor((ListableBeanFactory) this.beanFactory,
                            name.substring(0, name.length() - GLOBAL_SUFFIX.length()));
                }

                else {
                    //加入命名的拦截器Advice
                    Object advice;
                    if (this.singleton || this.beanFactory.isSingleton(name)) {
                        //单态Advice.
                        advice = this.beanFactory.getBean(name);
                    }
                    else {
                        //原型Advice
                        advice = new PrototypePlaceholderAdvisor(name);
                    }
                    //添加通知器advisor
                    addAdvisorOnChainCreation(advice, name);
                }
            }
        }

        this.advisorChainInitialized = true;
    }

getSingletonInstance方法

   private synchronized Object getSingletonInstance() {
        if (this.singletonInstance == null) {
            //获取代理的目标源
            this.targetSource = freshTargetSource();
            //如果ProxyFactoryBean设置了自动探测接口属性,并且没有配置代理接口
            //而且不是目标对象的直接代理类
            if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) {
                //根据AOP框架来判断需要代理的接口.
                Class targetClass = getTargetClass();
                if (targetClass == null) {
                    throw new FactoryBeanNotInitializedException("Cannot determine target class for proxy");
                }
                //设置代理对象的接口
                setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader));
            }
            super.setFrozen(this.freezeProxy);
            //使用ProxyFactory生成需要的AOPProxy对象
            this.singletonInstance = getProxy(createAopProxy());
        }
        return this.singletonInstance;
    }

ProxyCreatorSupport生成AopProxy对象

   protected final synchronized AopProxy createAopProxy() {
        if (!this.active) {
            activate();
        }
        //通过AopProxyFactory取得AopProxy,使用的是DefaultAopProxyFactory
        return getAopProxyFactory().createAopProxy(this);
    }

DefaultAopProxyFactory中生成AopProxy

   public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
        if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
            Class targetClass = config.getTargetClass();
            if (targetClass == null) {
                throw new AopConfigException("TargetSource cannot determine target class: " +
                        "Either an interface or a target is required for proxy creation.");
            }
            if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
                //使用JDK动态代理
                return new JdkDynamicAopProxy(config);
            }
            //使用Cglib动态代理
            return new ObjenesisCglibAopProxy(config);
        }
        else {
            //使用JDK动态代理
            return new JdkDynamicAopProxy(config);
        }
    }

当目标对象是原型模式的时候同样最后也会去调用DefaultAopProxyFactory中生成AopProxy对象,到此AOPProxy对象就生成完成了。AOPProxy对象具体是通过JDK动态代理还是Cglib动态代理不在博文中进行解析。

你可能感兴趣的:(Spring源码解析(十)-AopProxy对象生成过程)