溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在Java中實現(xiàn)動態(tài)代理

發(fā)布時間:2021-05-31 17:27:42 來源:億速云 閱讀:134 作者:Leah 欄目:編程語言

本篇文章為大家展示了如何在Java中實現(xiàn)動態(tài)代理,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

公用的接口和實現(xiàn)類

public interface UserService {
  public String getName(int id);
  public Integer getAge(int id);
}
public class UserServiceImpl implements UserService {
  @Override
  public String getName(int id) {
    System.out.println("------getName------");
    return "Tom";
  }
  @Override
  public Integer getAge(int id) {
    System.out.println("------getAge------");
    return 10;
  }
}

JDK的動態(tài)代理實現(xiàn)

jdk的動態(tài)代理,依賴的是反射包下的invocationHandler接口,我們的代理類實現(xiàn)invocationHandler,重寫invoke()方法,每當我們的代理類調(diào)用方法時,都會默認先經(jīng)過invoke()方法。

public class UserInvocationHandler implements InvocationHandler {
  private Object target;
  UserInvocationHandler() {
    super();
  }
  UserInvocationHandler(Object target) {
    super();
    this.target = target;
  }
  @Override
  public Object invoke(Object o, Method method, Object[] args) throws Throwable {
    if("getName".equals(method.getName())){
      System.out.println("++++++before " + method.getName() + "++++++");
      Object result = method.invoke(target, args);
      System.out.println("++++++after " + method.getName() + "++++++");
      return result;
    }else{
      Object result = method.invoke(target, args);
      return result;
    }
  }
}

測試類

public class M {
  public static void main(String[] args) {
    UserService userService = new UserServiceImpl();
    InvocationHandler invocationHandler = new UserInvocationHandler(userService);
    UserService userServiceProxy = (UserService) Proxy.newProxyInstance(
        userService.getClass().getClassLoader(),
        userService.getClass().getInterfaces(),
        invocationHandler);
    System.out.println(userServiceProxy.getName(1));
    System.out.println(userServiceProxy.getAge(1));
  }
}

測試效果

如何在Java中實現(xiàn)動態(tài)代理

CGLIB的動態(tài)代理實現(xiàn)

cglib依賴的是cglib包下的methodInterceptor接口,每調(diào)用代理類的方法,都會調(diào)用intercept方法

public class CglibMethodInterceptor implements MethodInterceptor {
  @Override
  public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
    System.out.println("------before " + methodProxy.getSuperName() + "------");
    Object o1 = methodProxy.invokeSuper(o, args);
    System.out.println("------after " + methodProxy.getSuperName() + "------");
    return o1;
  }
}

測試類

public class M {
  public static void main(String[] args) {
    CglibMethodInterceptor cglibProxy = new CglibMethodInterceptor();
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(UserServiceImpl.class);
    enhancer.setCallback(cglibProxy);
    UserService o = (UserService) enhancer.create();
    o.getName(1);
    o.getAge(1);
  }
}

測試結(jié)果

如何在Java中實現(xiàn)動態(tài)代理

上述內(nèi)容就是如何在Java中實現(xiàn)動態(tài)代理,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI