天天看點

Spring技術内幕筆記(5)——建立AopProxy代理對象

org.springframework.aop.framework.ProxyFactoryBean#getObject

public Object getObject() throws BeansException {
	//初始化通知器鍊,通知器鍊封裝了一些列攔截器,這些攔截器從配置檔案中擷取
        this.initializeAdvisorChain();
        if (this.isSingleton()) {
            return this.getSingletonInstance();
        } else {
            if (this.targetName == null) {
                this.logger.warn("Using non-singleton proxies with singleton targets is often undesirable. Enable prototype proxies by setting the 'targetName' property.");
            }

            return this.newPrototypeInstance();
        }
    }
           

org.springframework.aop.framework.ProxyFactoryBean#initializeAdvisorChain

private synchronized void initializeAdvisorChain() throws AopConfigException, BeansException {
        if (!this.advisorChainInitialized) {
            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("*") && this.targetName == null && this.targetSource == EMPTY_TARGET_SOURCE) {
                    throw new AopConfigException("Target required after globals");
                }

                String[] arr$ = this.interceptorNames;
                int len$ = arr$.length;

                for(int i$ = 0; i$ < len$; ++i$) {
                    String name = arr$[i$];
                    if (this.logger.isTraceEnabled()) {
                        this.logger.trace("Configuring advisor or advice '" + name + "'");
                    }

                    if (name.endsWith("*")) {
                        if (!(this.beanFactory instanceof ListableBeanFactory)) {
                            throw new AopConfigException("Can only use global advisors or interceptors with a ListableBeanFactory");
                        }
						//這裡是添加Advisor鍊的調用,是通過interceptorNames屬性進行配置的
                        this.addGlobalAdvisor((ListableBeanFactory)this.beanFactory, name.substring(0, name.length() - "*".length()));
                    } else {
					//如果程式走這邊,那麼需要加入命名的攔截器advice,并且需要檢查這個Bean
					//是singleton還是prototype
                        Object advice;
                        if (!this.singleton && !this.beanFactory.isSingleton(name)) {
                            advice = new ProxyFactoryBean.PrototypePlaceholderAdvisor(name);
                        } else {
                            advice = this.beanFactory.getBean(name);
                        }

                        this.addAdvisorOnChainCreation(advice, name);
                    }
                }
            }

            this.advisorChainInitialized = true;
        }
    }
           

org.springframework.aop.framework.ProxyFactoryBean#getSingletonInstance

private synchronized Object getSingletonInstance() {
        if (this.singletonInstance == null) {
            this.targetSource = this.freshTargetSource();
            if (this.autodetectInterfaces && this.getProxiedInterfaces().length == 0 && !this.isProxyTargetClass()) {
			//根據AOP架構來判斷需要代理的接口
                Class<?> targetClass = this.getTargetClass();
                if (targetClass == null) {
                    throw new FactoryBeanNotInitializedException("Cannot determine target class for proxy");
                }
			//這裡設定代理對象的接口
                this.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader));
            }

            super.setFrozen(this.freezeProxy);
			//通過createAopProxy傳回的AOPProxy來得到代理對象
            this.singletonInstance = this.getProxy(this.createAopProxy());
        }

        return this.singletonInstance;
    }

			//如果目标對象是接口類,則通過jdk來生成對象,否則使用CGLIB
                return (AopProxy)(targetClass.isInterface() ? new JdkDynamicAopProxy(config) : DefaultAopProxyFactory.CglibProxyFactory.createCglibProxy(config));