Spring框架学习[创建AOP代理对象并对目标对象切面拦截](四)

2014-11-24 03:05:49 · 作者: · 浏览: 3
sorChain() throws AopConfigException, BeansException { //如果通知器链已经被初始化,则直接返回,即通知器链只在第一次获取代理对象时产生 if (this.advisorChainInitialized) { return; } //如果ProxyFactoryBean中配置的连接器列名名称不为空 if (!ObjectUtils.isEmpty(this.interceptorNames)) { //如果没有Bean工厂(容器) 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"); } //遍历通知器链,向容器添加通知器 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 { Object advice; //如果通知器是单态模式 if (this.singleton || this.beanFactory.isSingleton(name)) { //从容器获取单态模式的通知或者通知器 advice = this.beanFactory.getBean(name); } //如果通知器是原型模式 else { //创建一个新的通知或者通知器对象 advice = new PrototypePlacehold
erAdvisor(name); } //添加通知器 addAdvisorOnChainCreation(advice, name); } } } //设置通知器链已初始化标识 this.advisorChainInitialized = true; }//获取一个单态模式的AOPProxy代理对象private synchronized Object getSingletonInstance() { //如果单态模式的代理对象还未被创建 if (this.singletonInstance == null) { //获取代理的目标源 this.targetSource = freshTargetSource(); //如果ProxyFactoryBean设置了自动探测接口属性,并且没有配置代理接 //且不是目标对象的直接代理类 if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) { //获取代理对象的目标类 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; }//获取一个原型模式的代理对象private synchronized Object newPrototypeInstance() { if (logger.isTraceEnabled()) { logger.trace("Creating copy of prototype ProxyFactoryBean config: " + this); } //根据当前的AOPProxyFactory获取一个创建代理的辅助类 ProxyCreatorSupport copy = new ProxyCreatorSupport(getAopProxyFactory()); //获取一个刷新的目标源 TargetSource targetSource = freshTargetSource(); //从当前对象中拷贝AOP的配置,为了保持原型模式对象的独立性,每次创建代理//对象时都需要拷贝AOP的配置,以保证原型模式AOPProxy代理对象的独立性 copy.copyConfigurationFrom(this, targetSource, freshAdvisorChain()); if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClas