After reading Dubbo's ExtensionLoader, I felt that it is very convenient to load various components and replace components in this way, so I implemented it myself.
Instructions:
public static void main(String[] args) {
// 调用 key 为 post 的http组件
ExtensionLoader.getExtensionLoader(Http.class).getAdaptiveExtension("post").http();
// 这个 直接使用注解配置的 key
ExtensionLoader.getExtensionLoader(Http.class).getAdaptiveExtension().http();
}
//输出结果
>>这是PostHttp 组件的 调用
>>这是GetHttp 组件的 调用
The configuration file is in src/META-INF/services/com.hadluo.dubbo.test.Http:
com.hadluo.dubbo.test.Http is the full path of the client interface
post=com.hadluo.dubbo.test.PostHttp
get=com.hadluo.dubbo.test.GetHttp
Http interface
package com.hadluo.dubbo.test;
import com.hadluo.dubbo.extension.Adaptive;
import com.hadluo.dubbo.extension.SPI;
@SPI
public interface Http {
@Adaptive("get")
public void http();
}
Two implementation classes
public class GetHttp implements Http{
@Override
public void http() {
System.err.println("这是GetHttp 组件的 调用");
}
}
public class PostHttp implements Http {
@Override
public void http() {
System.err.println("这是PostHttp 组件的 调用");
}
}
The source code structure is as follows
If you want the source code, you can add me qq 657455400 for free, and you can download it with points on csdn. Also give a download link:
https://download.csdn.net/download/luozheng4698729/10371705