Motan使用SPI机制来实现模块间的访问,基于接口和name来获取实现类,降低了模块间的耦合。
首先来看一下使用方式:
有两个注解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Spi {
Scope scope() default Scope.PROTOTYPE;
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface SpiMeta {
String name() default "";
}
@Spi用来注解接口,@SpiMeta用来注解接口的实现类
@Spi(scope = Scope.SINGLETON)
public interface ConfigHandler {
......
@SpiMeta(name = MotanConstants.DEFAULT_VALUE)
public class SimpleConfigHandler implements ConfigHandler {
......
Motan Spi机制遵循JDK的spi机制,在META-INF/services/下面配置实现类的描述。
文件:META-INF/services/com.weibo.api.motan.config.handler.ConfigHandler
com.weibo.api.motan.config.handler.SimpleConfigHandler
具体的使用方式是:
ConfigHandler configHandler = ExtensionLoader.getExtensionLoader(ConfigHandler.class).getExtension(MotanConstants.DEFAULT_VALUE);
public enum Scope {
/**
* 单例模式
*/
SINGLETON,
/**
* 多例模式
*/
PROTOTYPE
}