Spring框架学习[创建AOP代理对象并对目标对象切面拦截](二)
istableBeanFactory"); } //向容器中添加全局通知器 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 PrototypePlaceholderAdvisor(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 && !isProxyTargetClass()) { //设置代理接口 copy.setInterfaces( ClassUtils.getAllInterfacesForClass(targetSource.getTargetClass(), this.proxyClassLoader)); } copy.setFrozen(this.freezeProxy); if (logger.isTraceEnabled()) { logger.trace("Using ProxyCreatorSupport copy: " + copy); } //调用ProxyFactory生成AOPProxy代理 return getProxy(copy.createAopProxy()); } //使用createAopProxy方法返回的AOPProxy对象产生AOPProxy代理对象 protected Object getProxy(AopProxy aopProxy) { return aopProxy.getProxy(this.proxyClassLoader); } …… }