日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring aop实现过程之一代理对象的生成

發布時間:2025/4/5 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring aop实现过程之一代理对象的生成 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

AOP實現中,可以看到三個主要的步驟,一個是代理對象的生成,然后是攔截器的作用,然后是Aspect編織的實現。

?1.ProxyFactoryBean生成AopProxy

ProxyFactoryBean生成AOP proxy

1 /** 2 * Return a proxy. Invoked when clients obtain beans from this factory bean. 3 * Create an instance of the AOP proxy to be returned by this factory. 4 * The instance will be cached for a singleton, and create on each call to 5 * <code>getObject()</code> for a proxy. 6 * @return a fresh AOP proxy reflecting the current state of this factory 7 */ 8 public Object getObject() throws BeansException { 9 initializeAdvisorChain(); 10 if (isSingleton()) { 11 return getSingletonInstance(); 12 } 13 else { 14 if (this.targetName == null) { 15 logger.warn("Using non-singleton proxies with singleton targets is often undesirable. " + 16 "Enable prototype proxies by setting the 'targetName' property."); 17 } 18 return newPrototypeInstance(); 19 } 20 }

初始化Advisor chain

1 /** 2 * Create the advisor (interceptor) chain. Aadvisors that are sourced 3 * from a BeanFactory will be refreshed each time a new prototype instance 4 * is added. Interceptors added programmatically through the factory API 5 * are unaffected by such changes. 6 */ 7 private synchronized void initializeAdvisorChain() throws AopConfigException, BeansException { 8 if (this.advisorChainInitialized) { 9 return; 10 } 11 12 if (!ObjectUtils.isEmpty(this.interceptorNames)) { 13 if (this.beanFactory == null) { 14 throw new IllegalStateException("No BeanFactory available anymore (probably due to serialization) " + 15 "- cannot resolve interceptor names " + Arrays.asList(this.interceptorNames)); 16 } 17 18 // Globals can't be last unless we specified a targetSource using the property... 19 if (this.interceptorNames[this.interceptorNames.length - 1].endsWith(GLOBAL_SUFFIX) && 20 this.targetName == null && this.targetSource == EMPTY_TARGET_SOURCE) { 21 throw new AopConfigException("Target required after globals"); 22 } 23 24 // Materialize interceptor chain from bean names. 25 for (int i = 0; i < this.interceptorNames.length; i++) { 26 String name = this.interceptorNames[i]; 27 if (logger.isTraceEnabled()) { 28 logger.trace("Configuring advisor or advice '" + name + "'"); 29 } 30 31 if (name.endsWith(GLOBAL_SUFFIX)) { 32 if (!(this.beanFactory instanceof ListableBeanFactory)) { 33 throw new AopConfigException( 34 "Can only use global advisors or interceptors with a ListableBeanFactory"); 35 } 36 addGlobalAdvisor((ListableBeanFactory) this.beanFactory, 37 name.substring(0, name.length() - GLOBAL_SUFFIX.length())); 38 } 39 40 else { 41 // If we get here, we need to add a named interceptor. 42 // We must check if it's a singleton or prototype. 43 Object advice = null; 44 if (this.singleton || this.beanFactory.isSingleton(this.interceptorNames[i])) { 45 // Add the real Advisor/Advice to the chain. 46 advice = this.beanFactory.getBean(this.interceptorNames[i]); 47 } 48 else { 49 // It's a prototype Advice or Advisor: replace with a prototype. 50 // Avoid unnecessary creation of prototype bean just for advisor chain initialization. 51 advice = new PrototypePlaceholderAdvisor(this.interceptorNames[i]); 52 } 53 addAdvisorOnChainCreation(advice, this.interceptorNames[i]); 54 } 55 } 56 } 57 58 this.advisorChainInitialized = true; 59 }

增加advisor chain(AdvisedSupport.java)

1 private void addAdvisorInternal(int pos, Advisor advisor) throws AopConfigException { 2 Assert.notNull(advisor, "Advisor must not be null"); 3 if (isFrozen()) { 4 throw new AopConfigException("Cannot add advisor: Configuration is frozen."); 5 } 6 if (pos > this.advisors.size()) { 7 throw new IllegalArgumentException( 8 "Illegal position " + pos + " in advisor list with size " + this.advisors.size()); 9 } 10 this.advisors.add(pos, advisor); 11 updateAdvisorArray(); 12 adviceChanged(); 13 }

?

轉載于:https://www.cnblogs.com/davidwang456/archive/2013/03/18/2960969.html

總結

以上是生活随笔為你收集整理的spring aop实现过程之一代理对象的生成的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。