Retrofit是一個(gè)在OKhttp的基礎(chǔ)上醉锄,使用注解和動態(tài)代理實(shí)現(xiàn)的網(wǎng)絡(luò)請求框架
官網(wǎng)上給出了非常簡潔的使用示例:
本文不在于說明retrofit怎么使用渠旁,官方文檔寫的很詳細(xì)单料。主要是通過retrofit去了解注解和動態(tài)代理,retrofit是如何通過注解和動態(tài)代理去把復(fù)雜的網(wǎng)絡(luò)請求配置簡化成寥寥幾行代碼的碱璃。
注解
@GET("users/{user}/repos")
Call<List<Repo>> listRepos(@Path("user") String user);
使用過retrofit都知道@GET("users/{user}/repos")
和 @Path("user")
都是具有特殊含義的蚂会。
我們先來看看這兩個(gè)注解類
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface GET {
String value() default "";
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface Path {
String value();
boolean encoded() default false;
}
簡單的介紹一下
@Documented
Documented的英文意思是文檔。它的作用是能夠?qū)⒆⒔庵械脑匕絡(luò)avadoc中去胳螟。
@ Retention
Retention英文意思是保留、保持的意思筹吐,它表示注解存在階段是保留在源碼(編譯器)旺隙,字節(jié)碼(類加載)或者運(yùn)行期(JVM中運(yùn)行)。在@ Retention注解中使用枚舉RetentionPolicy來表示注解保留時(shí)期
- @Retention(RetentionPolicy.SOURCE)骏令,注解僅存在于源碼中,在class字節(jié)碼文件中不包含
- @Retention(RetentionPolicy.CLASS)垄提,默認(rèn)的保留策略榔袋,注解會在class字節(jié)碼文件中保存,但運(yùn)行時(shí)無法獲得
- @Retention(RetentionPolicy.RUNTIME)铡俐,注解會在class字節(jié)碼文件中保存凰兑,在運(yùn)行時(shí)可以通過反射獲取
很顯然上面的兩個(gè)注解都是幫助我們完成網(wǎng)絡(luò)請求配置的,它們使用的都是@Retention(RetentionPolicy.RUNTIME)
@Target
Target的英文意思是目標(biāo)审丘,這個(gè)很容易理解吏够,使用@Target元注解表示我們的注解作用的范圍,可以是類、函數(shù)锅知、函數(shù)參數(shù)變量等
- @Target(ElementType.TYPE) 作用接口播急、類、枚舉售睹、注解
- @Target(ElementType.FIELD) 作用屬性字段桩警、枚舉的常量
- @Target(ElementType.METHOD) 作用方法
- @Target(ElementType.PARAMETER) 作用方法參數(shù)
- @Target(ElementType.CONSTRUCTOR) 作用構(gòu)造函數(shù)
- @Target(ElementType.LOCAL_VARIABLE)作用局部變量
- @Target(ElementType.ANNOTATION_TYPE)作用于注解(@Retention注解中就使用該屬性)
- @Target(ElementType.PACKAGE) 作用于包
- @Target(ElementType.TYPE_PARAMETER) 作用于類型泛型,即泛型方法昌妹、泛型類捶枢、泛型接口 (jdk1.8加入)
- @Target(ElementType.TYPE_USE) 類型使用.可以用于標(biāo)注任意類型除了 class (jdk1.8加入)
GET作用于函數(shù),它使用的是ElementType.METHOD
Path作用于函數(shù)參數(shù)飞崖,它使用的是ElementType.PARAMETER
大致看一下就行烂叔,重點(diǎn)是如何在需要的時(shí)候去獲取這些注解信息
為了說一下如何獲取類上的注解信息,我特地建了一個(gè)Test的注解固歪,作用目標(biāo)是類,有一個(gè)string類的屬性
@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Test {
String value() default "";
}
GitHubService
類是這樣的
@Test("testinterface")
public interface GitHubService {
@GET("users/{user}/repos")
Call<List<Object>> listRepos(@FieldMap @Path("user") String user);
}
下面就貼分別如何獲取類蒜鸡、函數(shù)、參數(shù)上的注解信息的代碼
package retrofittest
import retrofit2.http.FieldMap
import retrofit2.http.GET
import retrofit2.http.Path
/**
* Author:wangling
* Email:wl_0420@163.com
* Date:6/21/21 4:19 PM
*/
fun main() {
// 獲取類上的注解
var java = GitHubService::class.java//獲取類對象
if (java.isAnnotationPresent(Test::class.java)) {//判斷是否包含@Test 注解
java.getAnnotation(Test::class.java)?.let {//獲取 @Test 注解
println("獲取類上的注解的信息:" + it.value)
}
}
// 獲取函數(shù)上的注解
var method = GitHubService::class.java.getDeclaredMethod("listRepos", String::class.java)//從類里獲取名稱為listRepos的函數(shù)的Method
if (method.isAnnotationPresent(GET::class.java)) {//判斷是否包含@GET 注解
method.getAnnotation(GET::class.java)?.let {//獲取@GET 注解
println("獲取函數(shù)上注解的信息:" + it.value)
}
}
//獲取函數(shù)參數(shù)上的注解
//注意 method.parameterAnnotations 是一個(gè)二維數(shù)組 [][] 第一個(gè)坐標(biāo)代表是第幾個(gè)參數(shù) 第二個(gè)坐標(biāo)代表當(dāng)前參數(shù)的第幾個(gè)注解
// 因?yàn)閰?shù)前可以添加多個(gè)注解,,一個(gè)參數(shù)上不可以添加相同的注解,同一個(gè)注解可以加在不同的參數(shù)上
for (ann in method.parameterAnnotations) {
println("ann: $ann")
for (a in ann) {
println("a: $a")
if (FieldMap::class.java.isInstance(a)) {
a as FieldMap
println("獲取參數(shù)上的注解信息:" + a.encoded)
} else if (Path::class.java.isInstance(a)) {
a as Path
println("獲取參數(shù)上的注解信息:" + a.value)
println("獲取參數(shù)上的注解信息:" + a.encoded)
}
}
}
}
打印
獲取類上的注解的信息:testinterface
獲取函數(shù)上注解的信息:users/{user}/repos
ann: [Ljava.lang.annotation.Annotation;@f6f4d33
a: @retrofit2.http.FieldMap(encoded=false)
獲取參數(shù)上的注解信息:false
a: @retrofit2.http.Path(encoded=false, value=user)
獲取參數(shù)上的注解信息:user
獲取參數(shù)上的注解信息:false
那么注解在retrofit中的作用就完成了昼牛,簡潔且明了的提供了網(wǎng)絡(luò)請求必須的參數(shù)
動態(tài)代理
代理模式是23種設(shè)計(jì)模式之一术瓮。
定義:為其它對象提供一種代理以控制對這個(gè)對象的訪問
使用場景:當(dāng)無法或不想直接訪問某個(gè)對象時(shí)可以通過一個(gè)代理對象來間接訪問,為了保證客戶端使用的透明性贰健,委托對象與代理對象必須實(shí)現(xiàn)相同的接口胞四。
下面通過代碼簡單示例一個(gè)靜態(tài)代理
// 定義一個(gè)接口
public interface BuyHouse {
void buyHosue();
}
//創(chuàng)建一個(gè)實(shí)現(xiàn)類,也就是被代理類
public class BuyHouseImpl implements BuyHouse {
@Override
public void buyHosue() {
System.out.println("我要買房");
}
}
//創(chuàng)建一個(gè)代理類
public class BuyHouseProxy implements BuyHouse {
public BuyHouse buyHouse;
public BuyHouseProxy(final BuyHouse buyHouse) {
this.buyHouse = buyHouse;
}
@Override
public void buyHosue() {
System.out.println("買房前準(zhǔn)備");
buyHouse.buyHosue();
System.out.println("買房后裝修");
}
}
代理類BuyHouseProxy持有被代理類BuyHouseImpl的對象伶椿,通過調(diào)用BuyHouseProxy示例的方法就可以間接的調(diào)用BuyHouseImpl的方法辜伟。
在一般情況使用靜態(tài)代理并沒有什么問題,但是在需要代理的類很多時(shí)脊另,每個(gè)被代理類去實(shí)現(xiàn)一個(gè)代理類就會很麻煩导狡,于是動態(tài)代理應(yīng)運(yùn)而生。
//編寫一個(gè)動態(tài)處理期
public class DynamicProxyHandler implements InvocationHandler {
private Object object;
public DynamicProxyHandler(Object object) {
this.object = object;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("買房前準(zhǔn)備");
Object invoke = method.invoke(object, args);
System.out.println("買房后裝修");
return invoke;
}
}
//編寫一個(gè)測試類
public class DynamicProxyTest {
public static void main(String[] args) {
BuyHouse buyHouse = new BuyHouseImpl();
BuyHouse proxyBuyHouse = (BuyHouse) Proxy.newProxyInstance(BuyHouse.class.getClassLoader(), new Class[]{BuyHouse.class}, new DynamicProxyHandler(buyHouse));
proxyBuyHouse.buyHosue();
}
}
要使用動態(tài)代理就是這么簡單偎痛,而動態(tài)代理的原理就不闡述了旱捧,急于想探究新世界的小伙伴可以點(diǎn)擊JAVA動態(tài)代理查看
看到這里可能有小伙伴發(fā)出了質(zhì)疑,上面的動態(tài)代理示例中踩麦,要使用動態(tài)代理必須有一個(gè)實(shí)現(xiàn)類也就是被代理類枚赡,可是我們在使用的過程中只是定義了Service接口,利用不同的注解給方法及參數(shù)設(shè)置了一些配置信息谓谦。那么這個(gè)實(shí)現(xiàn)類該怎么獲取贫橙。
看retrofit的源碼
Retrofit.class
... 關(guān)鍵位置,就是在這里實(shí)現(xiàn)動態(tài)代理
public <T> T create(final Class<T> service) {
Utils.validateServiceInterface(service);
if (this.validateEagerly) {
this.eagerlyValidateMethods(service);
}
return Proxy.newProxyInstance(service.getClassLoader(), new Class[]{service}, new InvocationHandler() {
private final Platform platform = Platform.get();
public Object invoke(Object proxy, Method method, Object... args) throws Throwable {
if (method.getDeclaringClass() == Object.class) {//沒看懂反粥。卢肃。疲迂。
return method.invoke(this, args);
} else if (this.platform.isDefaultMethod(method)) {
return this.platform.invokeDefaultMethod(method, service, proxy, args);
} else {
ServiceMethod serviceMethod = Retrofit.this.loadServiceMethod(method);
OkHttpCall okHttpCall = new OkHttpCall(serviceMethod, args);
return serviceMethod.callAdapter.adapt(okHttpCall);
}
}
});
}
...
重點(diǎn)看
ServiceMethod serviceMethod = Retrofit.this.loadServiceMethod(method);
OkHttpCall okHttpCall = new OkHttpCall(serviceMethod, args);
return serviceMethod.callAdapter.adapt(okHttpCall);
ServiceMethod是一個(gè)負(fù)責(zé)轉(zhuǎn)化(適配)的類,負(fù)責(zé)把一個(gè)接口的抽象方法的執(zhí)行過程的結(jié)果轉(zhuǎn)化(適配)成一個(gè)網(wǎng)絡(luò)請求(HTTP call)莫湘。
ServiceMethod是如何獲得的
ServiceMethod serviceMethod = Retrofit.this.loadServiceMethod(method);
點(diǎn)進(jìn)去查看
ServiceMethod loadServiceMethod(Method method) {
synchronized(this.serviceMethodCache) {
//現(xiàn)在緩存查找
ServiceMethod result = (ServiceMethod)this.serviceMethodCache.get(method);
if (result == null) {
沒有就創(chuàng)建一個(gè)尤蒿,并保存到緩存里
result = (new retrofit2.ServiceMethod.Builder(this, method)).build();
this.serviceMethodCache.put(method, result);
}
return result;
}
}
怎么創(chuàng)建的?
看Builder
public Builder(Retrofit retrofit, Method method) {
this.retrofit = retrofit;
this.method = method;
this.methodAnnotations = method.getAnnotations();
this.parameterTypes = method.getGenericParameterTypes();
this.parameterAnnotationsArray = method.getParameterAnnotations();
}
有沒有發(fā)現(xiàn)是熟悉的地方逊脯。
method.getAnnotations();
獲取方法的所有注解
method.getParameterAnnotations();
獲得方法參數(shù)上的所有注解
有個(gè)這些优质,ServiceMethod是不是就可以獲取到了我們設(shè)置的配置信息了。至此retrofit军洼、動態(tài)代理巩螃、注解就可以串起來了。
當(dāng)然retrofit還有很多設(shè)置匕争,如序列化設(shè)置addConverterFactory
避乏、rxjava支持addCallAdapterFactory
這里就不說了