Retrofit的动态代理

Retrofit是通过动态代理来生成代理对象作为网络请求的发起者。

今天就来看下动态代理是怎么操作的。或者说是怎么让一个貌似接口的对象调用它的抽象方法呢?

实现代码

这里写代码片

public static void main(String[] args) {
  Factory factory = new Factory();
  Bird bird = factory.create(Bird.class);
  bird.fly();
}

interface Bird {
  void fly();
}

通过一个Factory 实例调用create方法,传入一个接口的class对象就可以返回一个接口的实例,可以调用接口中的方法fly()。

而在我们的静态代码中并没有一个类去实现了这个Bird接口

这里写代码片

public class DynamicProxy {

    interface Bird {
        void fly();
    }

    public static void main(String[] args) {
        Factory factory = new Factory();
        Bird bird = factory.create(Bird.class);
        bird.fly();
    }

    static class Factory implements InvocationHandler {

        public <T> T create(Class<T> target) {
            return (T) Proxy.newProxyInstance(target.getClassLoader(),
                    new Class[]{target},
                    this);
        }

        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("flying...");
            return null;
        }
    }
}

在调用bird.fly()时,输出结果为flying…,很明显,代码中就如同开始所说的,并不存在一个实现了Bird接口的子类,而bird又实实在在调用了fly()方法。唯一的可能就是bird是接口的实例

这里只是利用到了Java的动态代理,通过Proxy.newProxyInstance()方法生成实现了指定接口的子类,然后返回了这个动态生成类的实例对象。

这个子类在调用接口中的方法时,其实调用的是InvocationHandler的invoke()方法。在此方法中会有对应参数的回调,可以根据这些参数做出合适的拦截/增强等操作。

JDK提供的动态代理,动态生成的子类是继承自Proxy类的,,而Java是不支持多继承的,所以很显然。通过动态代理返回的对象必然是以接口形式来接收的,扩展的只有接口和实现接口的子类,对于一些没有实现接口的类是没有办法进行扩展的。

通过在接口中在方法上的注解,确定一些请求的参数。然后创建代理对象,在方法中拼接处完整的Url

请求网络,并且在响应头中的Status字段打印出来。

这里写代码片

public interface Bird {

    @Protocol()
    @Method()
    @Path("search")
    @Query("q=java")
    @Url("github.com")
    String fly();
}


public class Test {

    public static void main(String[] args) {
        Factory factory = new Factory();
        Bird bird = factory.create(Bird.class);
        String status = bird.fly();

        System.out.println(status);//输出 Status: 200 OK
    }
}

部分代码
public class Factory implements InvocationHandler {
OkHttpClient httpClient = new OkHttpClient();

public <T> T create(Class<T> target) {
    return (T) Proxy.newProxyInstance(target.getClassLoader(),
            new Class[]{target},
            this);
}

public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws Throwable {
    String get = method.getAnnotation(Method.class).method();
    String protocol = method.getAnnotation(Protocol.class).value();
    String url = method.getAnnotation(Url.class).value();
    String path = method.getAnnotation(Path.class).value();
    String query = method.getAnnotation(Query.class).value();

    String entire_url = protocol + "://" + url + "/" + path + "?" + query;


    System.out.println(entire_url);
    Request build = new Request.Builder().url(entire_url).get().build();

    return httpClient.newCall(build).execute().headers().get("Status");
}

}

这里写代码片

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Method {
    String method() default "GET";
}


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Url {
    String value();
}


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Protocol {
    String value() default "https";
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Path {
    String value();
}



@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Query {
    String value() default "";
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值