美文网首页
spring中的代理

spring中的代理

作者: Lolipiza | 来源:发表于2020-12-16 09:20 被阅读0次

1.静态代理:

public class Main2 {
    //这里传入的是接口类型的对象,方便向上转型,实现多态
    public static void consumer(ProxyInterface pi){
        pi.say();
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        consumer(new ProxyObject());
    }
}

//代理接口
interface ProxyInterface{
    public void say();
}


//被代理者
class RealObject implements ProxyInterface{
    //实现接口方法
    @Override
    public void say() {
        // TODO Auto-generated method stub
        System.out.println("say");
    }
    
}


//代理者
class ProxyObject implements ProxyInterface{

    @Override
    public void say() {
        // TODO Auto-generated method stub
        //dosomething for example
        System.out.println("hello proxy");
        new RealObject().say();
        System.out.println("this is method end");
    }
    
}
hello proxy
say
this is method end

控制台输出:

hello proxy
say
this is method end

2.动态代理:

package docker.demo;

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

/**
 * @program:dockerdemo
 * @desc:
 * @author:qiyihang
 * @date:2020/12/15
 */
public class DynamicProxy {
    static void customer(ProxyInterface pi){
        pi.say();
    }
    public static void main(String[] args){
        RealObject real = new RealObject();
        ProxyInterface proxy = (ProxyInterface) Proxy.newProxyInstance(ProxyInterface.class.getClassLoader(),new Class[]{ProxyInterface.class}, new ProxyObject(real));
        customer(proxy);
    }
}

interface ProxyInterface{
    void say();
}

//被代理类
class RealObject implements ProxyInterface{
    public void say(){
        System.out.println("i'm talking");
    }
}

//代理类,实现InvocationHandler 接口
class ProxyObject implements InvocationHandler {
    private Object proxied = null;
    public ProxyObject(){

    }
    public ProxyObject(Object proxied){
        this.proxied  = proxied;
    }
    public Object invoke(Object arg0, Method arg1, Object[] arg2) throws Throwable {
        System.out.println("hello");
        return arg1.invoke(proxied, arg2);
    };
}

控制台输出:

hello
i'm talking

代理类实现了InvocationHandler接口,

ProxyInterface proxy = (ProxyInterface) Proxy.newProxyInstance(ProxyInterface.class.getClassLoader(),new Class[]{ProxyInterface.class}, new ProxyObject(real));

得到接口的实例,作为参数传递到customer(),这里每一个在代理类上处理的东西也会被重定向到调用处理器上。

Proxy.newProxyInstance()方法,接收三个参数:第一个参数指定当前目标对象使用的类加载器,获取加载器的方法是固定的;第二个参数指定目标对象实现的接口的类型;第三个参数指定动态处理器,执行目标对象的方法时,会触发事件处理器的方法。


源码分析之后补上,先做个记录
摘抄自: Java动态代理与反射详解

相关文章

  • Spring事务代理机制总结(含代码Demo)

    Spring事务代理机制总结: 1、Spring声明式事务的底层是怎么实现的? 通过Spring中的代理,那在Sp...

  • Spring 基于 AspectJ 的 AOP 开发

    Spring 基于 AspectJ 的 AOP 开发 在 Spring 的 aop 代理方式中, AspectJ ...

  • Spring Boot 2.5.x能支持Java 17了 - c

    在 Spring Framework 中,AOP 代理是 JDK 动态代理或 CGLIB 代理。 ASM 是 Ja...

  • spring中的代理

    1.静态代理: 控制台输出: hello proxysaythis is method end 2.动态代理: 控...

  • 2018-10-03

    Spring aop 是通过代理实现的,代理有静态代理,jdk动态代理和cglib动态代理,代理就像我们生活中的房...

  • AOP代理

    Spring中的AOP代理由Spring的IOC容器负责生成、管理,其依赖关系也由IOC容器负责管理。AOP代理可...

  • Java Spring中的动态代理cglib

    总结 JDK的动态代理和 Spring中的动态代理cglib区别 JDK 的动态代理 :针对实现了接口的类产生代理...

  • Spring AOP中的动态代理

    Spring AOP中的动态代理主要有两种方式,JDK动态代理和CGLIB动态代理: (3)静态代理与动态代理区别...

  • 瓴岳科技

    Spring中的HandlerInterceptor 是否有了解 cglib动态代理与JDK动态代理的区别 联合索...

  • AOP代理:

    AOP代理:AOP框架创建的对象,代理就是对目标对象的增强。Spring中的AOP代理可以是JDK动态代理,也可以...

网友评论

      本文标题:spring中的代理

      本文链接:https://www.haomeiwen.com/subject/cziwgktx.html