java代理模式

  1. 静态代理
    定义接口:
package cn.sky.testProxy.service;

public interface IUserService {
    String getName(int id);
    int getAge(int id);
}

实现接口:

package cn.sky.testProxy.service.impl;

import cn.sky.testProxy.service.IUserService;

public class UserServiceImpl implements IUserService {
    @Override
    public String getName(int id) {
        return "sky";
    }

    @Override
    public int getAge(int id) {
        return 99;
    }
}

测试接口实现类:

    @Test
    public void test1(){
        IUserService userService = new UserServiceImpl();
        System.out.println(userService.getName(1));
        System.out.println(userService.getAge(1));
    }

java代理模式_第1张图片
实现接口静态代理类:

package cn.sky.testProxy.service.proxy;

import cn.sky.testProxy.service.IUserService;

public class UserServiceProxy implements IUserService {

    private IUserService userService;

    public UserServiceProxy(IUserService userService){
        this.userService = userService;
    }

    @Override
    public String getName(int id) {
        System.out.println("getName方法前置增强处理");
        return userService.getName(id);
    }

    @Override
    public int getAge(int id) {
        System.out.println("getAge方法前置增强处理");
        return userService.getAge(id);
    }
}

测试静态代理类:

   @Test
    public void test2(){
        IUserService userService = new UserServiceProxy(new UserServiceImpl());
        System.out.println(userService.getName(1));
        System.out.println(userService.getAge(1));
    }

java代理模式_第2张图片

  1. jdk动态代理
         @Test
    public void test3() throws InterruptedException {
        IUserService userService = new UserServiceImpl();
        ClassLoader classLoader = userService.getClass().getClassLoader();
        Class<?>[] interfaces = userService.getClass().getInterfaces();
        InvocationHandler invocationHandler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("方法前置增强");
                return method.invoke(userService,args);
            }
        };
        IUserService proxyInstance = (IUserService) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
        System.out.println("代理类的名称:"+proxyInstance.getClass().getName());
        System.out.println(proxyInstance.getName(1));
        System.out.println(proxyInstance.getAge(1));
        Thread.sleep(999999999);
    }

java代理模式_第3张图片
使用arthas反编译获取jdk动态代理产生的代理类代码:
java代理模式_第4张图片

java代理模式_第5张图片
得到代理类代码后,再简化一下(去掉异常处理以及Object方法的代理),如下:

package com.sun.proxy;

import cn.sky.testProxy.service.IUserService;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;

public final class $Proxy4 extends Proxy implements IUserService {
   
    private static Method m3;
    private static Method m4;

    public $Proxy4(InvocationHandler invocationHandler) {
        super(invocationHandler);
    }

    static {
		m3 = Class.forName("cn.sky.testProxy.service.IUserService").getMethod("getName", Integer.TYPE);
		m4 = Class.forName("cn.sky.testProxy.service.IUserService").getMethod("getAge", Integer.TYPE);  
    }

    public final String getName(int n) {
        return (String)this.h.invoke(this, m3, new Object[]{n});
    }

    public final int getAge(int n) {
        return (Integer)this.h.invoke(this, m4, new Object[]{n});
    }
}

由此看出,动态生成的代理类,与静态代理类一样,都实现了IUserService接口,不同点是:静态代理类直接持有UserServiceImpl对象,而动态代理类则是通过InvocationHandler间接持有UserServiceImpl对象。

你可能感兴趣的:(java,代理模式,java,开发语言)