posts - 66,  comments - 11,  trackbacks - 0
          引入與其他類型的通知不同,引入影響的是整個(gè)類。他們通過給需要消息的類添加方法和屬性來實(shí)現(xiàn)。引入讓你能夠動(dòng)態(tài)地建立復(fù)合對(duì)象,提供了多態(tài)繼承的好處。
          Spring通過一個(gè)特殊的方法攔截器接口IntroductionMethodInterceptor來實(shí)現(xiàn)引入。這個(gè)接口添加一個(gè)方法:
          boolean implementsInterface(Class intf);
          如果IntroductionMethodInterceptor是為了實(shí)現(xiàn)指定接口,那么方法implementsInterface應(yīng)該返回true.就是說,調(diào)用這個(gè)接口聲明的方法的任何調(diào)用將被委派給IntroductionMethodInterceptor的invoke()方法.invoke()方法負(fù)責(zé)實(shí)現(xiàn)這個(gè)方法,不能調(diào)用MethodInvocation.proceed().他引入了新的接口,調(diào)用目標(biāo)對(duì)象是沒有用的。

          Spring提供了一個(gè)方便的類來處理我們的大多數(shù)應(yīng)用:DelegatingintroductionInterceptor.代碼:
          package com.wyq.spring.base.aopinstance;

          import java.util.Date;

          /** 
           * 
          @author 作者 
           * 
          @version 創(chuàng)建時(shí)間:2009-11-6 下午02:58:21 
           * 類說明 
           
          */
          public interface Auditable {
              
          void setLastModifiedDate(Date date);
              Date getLastModifiedDate();
          }

          package com.wyq.spring.base.aopinstance;

          import java.util.Date;

          import org.springframework.aop.support.DelegatingIntroductionInterceptor;

          /** 
           * 
          @author 作者 
           * 
          @version 創(chuàng)建時(shí)間:2009-11-6 下午03:00:06 
           * 類說明 
           
          */
          public class AuditableMixin extends DelegatingIntroductionInterceptor implements
                  Auditable {
              
          private Date lastModifiedDate;
              
          /*
               * 注意我們不用實(shí)現(xiàn)invoke()方法了,DelegatingIntroductionInterceptor為我們實(shí)現(xiàn)了這
               * 個(gè)方法。DelegatingIntroductionInterceptor也要實(shí)現(xiàn)你的混合類暴露的任何方法,并且將
               * 任何對(duì)這些方法的調(diào)用委托給這個(gè)混合類。因?yàn)槲覀兊念悓?shí)現(xiàn)了Auditable,對(duì)這個(gè)接口的方法的
               * 所有調(diào)用都將調(diào)用我們的攔截器。任何其他方法將委托給目標(biāo)對(duì)象。
               
          */
              
          public Date getLastModifiedDate() {
                  
          return lastModifiedDate;
              }

              
          public void setLastModifiedDate(Date lastModifiedDate) {
                  
          this.lastModifiedDate = lastModifiedDate;
              }

          }

          創(chuàng)建一個(gè)引入Advisor:
          因?yàn)橐胪ㄖ粦?yīng)用在類層次上,所以引入有他們自己的Advisor:IntroductionAdvisor.Spring也提供了一個(gè)適合大多數(shù)情況的缺省實(shí)現(xiàn)。它的名字叫做DefaultIntroductionAdvisor.
          BeanFactory對(duì)象是一個(gè)負(fù)責(zé)創(chuàng)建其他JavaBean的JavaBean.我們的ProxyFactoryBean創(chuàng)建代理對(duì)象。和其他JavaBean一樣,它有控制行為的屬性。
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

          <beans>
              
          <!-- 創(chuàng)建一個(gè)目標(biāo)對(duì)象 -->
              
          <bean id="courseTarget" class="com.springinaction.training.model.Course"></bean>
              
          <!-- 創(chuàng)建一個(gè)引入 -->
              
          <bean id="auditableMixin" class="com.springinaction.training.advice.AuditableMixin"></bean>
              
          <!-- 創(chuàng)建一個(gè)引入通知 -->
              
          <bean id="auditableAdvisor" class="org.springframework.aop.support.DefaultIntroductionAdvisor">
                  
          <constructor-arg>
                      
          <ref bean="auditableMixin"/>
                  
          </constructor-arg>
              
          </bean>
              
          <!-- 創(chuàng)建一個(gè)代理
                  
          ProxyFactoryBean的屬性
                   target:代理的目標(biāo)對(duì)象
                   proxyinterfaces:代理應(yīng)該實(shí)現(xiàn)的接口列表
                   interceptorNames:需要應(yīng)用到目標(biāo)對(duì)象上的通知Bean的名字,可以是攔截器、Advisor或者其他通知類型的名字。
                   singleton:在每次抵用getBean時(shí),工廠是否返回的是同一個(gè)代理實(shí)例。如果是有狀態(tài)通知,應(yīng)該設(shè)置為false.
                   aopProxyFactory:通常不需要使用這個(gè)屬性。
                   ProxyTargetClass:是否代理目標(biāo)類,而不是接口類。
                -->
              
          <bean id="course" class="org.springframework.aop.framework.ProxyFactoryBean">
                  
          <property name="proxyTargetClass">
                      
          <value>true</value>
                  
          </property>
                  
          <property name="singleton">
                      
          <value>false</value>
                  
          </property>
                  
          <property name="proxyInterfaces">
                      
          <value>com.springinaction.training.advice.Auditable</value>
                  
          </property>
                  
          <property name="interceptorNames">
                      
          <ref bean="auditableAdvisor"/>
                  
          </property>
                  
          <property name="target">
                      
          <ref bean="courseTarget"/>
                  
          </property>
              
          </bean>
          </beans>

          posted on 2009-11-06 15:35 王永慶 閱讀(169) 評(píng)論(0)  編輯  收藏 所屬分類: SPRING
          <2009年11月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          關(guān)注blogs

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 财经| 调兵山市| 互助| 安乡县| 深州市| 营山县| 通河县| 韶山市| 台中市| 和硕县| 类乌齐县| 绥宁县| 长沙县| 桂平市| 定南县| 濮阳县| 临海市| 邛崃市| 广昌县| 平定县| 潼南县| 河北区| 平原县| 广河县| 休宁县| 屏山县| 谢通门县| 绩溪县| 平原县| 措美县| 昌吉市| 固安县| 龙胜| 山阴县| 台东县| 淳化县| 长顺县| 云安县| 池州市| 清河县| 蓬安县|