静态代理和JDK动态代理

静态代理模式:

首先实现代理模式,代理者和真正的实现者都要实现同一个接口,代理者真正调用接口方法时是由被代理者去实现的,代理者可在实现方法的前后加上自己的逻辑,例如,Spring aop在进入切点的时候会执行自己的逻辑.下面是静态代理的类图结构

代码实现:

接口定义:

public interface Subject
{
    void excute();

    void hello();
}


实现类实现: 

@Component
public class RealSubject implements Subject
{
    @Override
    public void excute()
    {
        System.out.println("this is real implements method");
    }

    @Override
    public void hello()
    {
        System.out.println("this is real implements say hello");
    }
}

代理类实现:

public class StaticProxy implements Subject
{
    private RealSubject realSubject;

    public StaticProxy(RealSubject realSubject)
    {
        this.realSubject = realSubject;
    }

    @Override
    public void excute()
    {
        System.out.println("proxy doing someting before");
        realSubject.excute();
        System.out.println("proxy doing something after");
    }

    @Override
    public void hello()
    {
        System.out.println("proxy  sayhello before");
        realSubject.hello();
        System.out.println("proxy sayhello after");
    }
}

客户端实现:

public class Client
{
    public static void main(String[] args)
    {
        StaticProxy  proxy= new StaticProxy(new RealSubject());
        proxy.excute();
    }
}

运行结果:

proxy doing someting before
this is real implements method
proxy doing something after
proxy  sayhello before
this is real implements say hello
proxy sayhello after

JDK动态代理

使用动态代理,需要将要扩展的功能写在一个InvocationHandler 实现类里:

另外,jdk代理是面向接口的,也就是说被代理者是需要实现接口才可被代理

JDK动态代理类: 

public class JdkProxy implements InvocationHandler
{
    private RealSubject realSubject;

    public JdkProxy(RealSubject realSubject)
    {
        this.realSubject = realSubject;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
        throws Throwable
    {
        System.out.println("jdk proxy doing someting before...");
        Object invoke = null;
        try
        {
            invoke = method.invoke(realSubject, args);
            System.out.println("output proxy return value..." + invoke);
        }
        catch (Exception e)
        {
            System.out.println("catch exception ...");
            throw e;
        }
        finally
        {
            System.out.println("jdk proxy doing something after...");
        }
        return invoke;
    }
}

客户端:

public class Client
{
    public static void main(String[] args)
    {
        Subject proxy = (Subject)Proxy.newProxyInstance(Client.class.getClassLoader(),
            new Class[] {Subject.class},
            new JdkProxy(new RealSubject()));
        proxy.excute();
        proxy.hello();
    }
}

 运行结果:

jdk proxy doing someting before...
this is real implements method
output proxy return value...null
jdk proxy doing something after...
jdk proxy doing someting before...
this is real implements say hello
output proxy return value...null
jdk proxy doing something after...

猜你喜欢

转载自blog.csdn.net/u010859650/article/details/81160381