Spring完成動態代理模式:

來源:互聯網
上載者:User

步驟如下:
1.    寫介面類:
public interface Subject {
        public void request();
}
2.    寫實現介面的真實主題類:
public class RealSubject implements Subject {
    public void request() {
       System.out.println("真實的主題的實現的方法");
    }
}
3.       再寫一個類ProxySubjectBeforeAdvice實現MethodBeforeAdvice介面完成代理方法之前完成的任務:
public class ProxySubjectBeforeAdvice implements MethodBeforeAdvice {
    public void before(Method arg0, Object[] arg1, Object arg2)
           throws Throwable {
           System.out.println("代理之前的任務~~~~~");    
    }
}
4. 寫一個ProxySubjectAfterAdvice類實現AfterReturningAdvice介面完成代理之後完成的任務:
public class ProxySubjectAfterAdvice implements AfterReturningAdvice {
    public void afterReturning(Object arg0, Method arg1, Object[] arg2,
           Object arg3) throws Throwable {
       System.out.println("在代理之後完成的任務~~");
    }
}

5.在Spring提供的設定檔applicationContext.xml中進行配置如下:

<!-- 表示實現代理之前的方法的類的執行個體 -->
    <bean id="before"
   class="org.sqs.spring.aop.demo02.ProxySubjectBeforeAdvice">
    </bean>
    <!-- 表示實現代理之後的方法的類的執行個體 -->
    <bean id="after" class="org.sqs.spring.aop.demo02.ProxySubjectAfterAdvice"></bean>
   <!--定義一個真實主題類的對象 -->
    <bean id="subject" class="org.sqs.spring.aop.demo02.RealSubject"></bean>
   <!-- 寫出Spring中提供的代理類的Bean(一定要注意找准Spring中的那個ProxyFactoryBean類) -->
    <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
          <!-- 說明代理的介面是誰 -->
          <property name="proxyInterfaces">
              <!-- 值表示那個真實的類實現的介面Subject --             <value>org.sqs.spring.aop.demo02.Subject</value>
          </property>
          <!-- 加入一個目標屬性工作表示所代理的真實類-->
          <property name="target">
              <!-- 參考subject(真實的主題類的執行個體) -->
              <ref bean="subject"/>
          </property>
          <!-- 寫入攔截器屬性 -->
          <property name="interceptorNames">
                <!-- 注入攔截器的集合 -->
             <list>
                   <!-- 表示調用剛才定義的ProxySubjectBeforeAdvice的執行個體中的before()方法 -->
                    <!-- value中間的before其實是剛才ProxySubjectBeforeAdvice的Bean的id-->
                   <value>before</value>
                    <!-- 加入代理之後完成任務的攔截器 -->
                   <value>after</value>
             </list>
          </property>
    </bean>

6.編寫測試類別:

publicclass TestDemo {

      publicstaticvoid main(String args[]){

  ApplicationContext context = null;

  context = new ClassPathXmlApplicationContext("applicationContext.xml");

  //得到代理類的Bean(是Spring中提供的(已經在設定檔中實現了Subject介面))

  Subject sub = (Subject) context.getBean("proxy");

  sub.request();//實現真實的方法(同時代理也完成了!)

}

}

最後列印的結果是:

代理之前的任務~~~~~

真實的主題的實現的方法

在代理之後完成的任務~~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.