springAOP理解——java中的proxy實現AOP功能

來源:互聯網
上載者:User

 

之前學習的例子,貼上來供大家共同學習。大家都知道,spring的AOP功能實際上是實現了java的動態代理功能,下面分別給出java動態代理、spring中CGLIB實現AOP功能、及spring 中實現AOP的配置(設定檔方式和註解方式)。

 

1.動態代理:在調用介面前,提前對要代理的介面進行攔截,攔截中做一些事,處理後繼續調用介面方法,不影響正常邏輯

可用於許可權控制

 

2.實現例子(沒有列出相關jar)

1介面

package com.mooing.service;

public interface MyService {
 public void save();
 public void findAll();
 public void update();
 public void delete();
}

2實作類別

package com.mooing.service;

public class MyServiceImpl implements MyService {

 private String temp = null;

 public MyServiceImpl() {
 }

 public MyServiceImpl(String temp) {
  this.temp = temp;
 }

 public void save() {
  System.out.println("this is save()");
 }

 @Override
 public void findAll() {
  System.out.println("this is findAll()");
 }

 @Override
 public void update() {
  System.out.println("this is update()");
 }

 @Override
 public void delete() {
  System.out.println("this is delete()");
 }

 public String getTemp() {
  return temp;
 }
}

 

3代理類

package com.mooing.service;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**

為目標對象建立代理類
 * 代理類,需實現InvocationHandle介面
 * @author mooing
 *
 */
public class MyProxyService implements InvocationHandler {

 private Object targetObj;

 /**
  * 為目標對象建立代理類
  *
  * 傳入要代理類,建立此類的代理執行個體
  *
  * @param targetObj 目標對象
  * @return 返回目標對象的代理類
  */ public Object createProxyInstances(Object targetObj) {
  this.targetObj = targetObj;

  /**
   * 參數1 要代理對象的類裝載器
   * 參數2 要代理對象的所有介面
   * 參數3 觸發哪個類裡的攔截方法(回調方法invoke,處理實際調用方法前的操作) this代表當前類
   */

  return Proxy.newProxyInstance(targetObj.getClass().getClassLoader(), targetObj.getClass().getInterfaces(), this);
 }

 /**
  * 回調方法
  */

 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  // MyService ms=(MyService) createProxyInstances(new MyServiceImpl());
  //MyServiceImpl myservice = (MyServiceImpl) targetObj;
  //Object result = null;
 // if (myservice.getTemp() != null) {
  // result = method.invoke(targetObj, args);
  //}
  //return result;

  System.out.println("進入攔截方法");
  return  method.invoke(targetObj, args);
 }
}

 

測試類別

package com.mooing.test;

import org.junit.Test;

import com.mooing.service.MyProxyService;
import com.mooing.service.MyService;
import com.mooing.service.MyServiceImpl;

public class MyProxyTest {

 @Test
 public void testJdkProxy() {
  MyProxyService proxy = new MyProxyService();

//代理目的:如果給MyServiceImpl類中temp賦值,則調用介面方法,否則不調用,類似許可權控制
  MyService ms = (MyService) proxy.createProxyInstances(new MyServiceImpl("xxx"));
  ms.delete();
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.