1.Activate的作用
Activate:可以被框架中自动激活加载扩展,此Annotation用于配置扩展被自动激活加载条件。
以上定义有2个关键词,自动激活和加载条件
可以参考此篇文章:
解释:用户通过group和value配置激活条件,被activate注解的扩展点在满足某种条件时会被激活
2.Activate注解的功能定义
我们先看看其注解的定义,主要有3个功能定义,可以简化理解为筛选和排序
分组(筛选条件)
key值(筛选条件)
排序
代码示例1
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Activate {
/**
* Activate the current extension when one of the groups matches. The group passed into
* {@link ExtensionLoader#getActivateExtension(URL, String, String)} will be used for matching.
*
* @return group names to match
* @see ExtensionLoader#getActivateExtension(URL, String, String)
*/
String[] group() default {};
/**
* Activate the current extension when the specified keys appear in the URL's parameters.
*
* For example, given @Activate("cache, validation")
, the current extension will be return only when
* there's either cache
or validation
key appeared in the URL's parameters.
*
*
* @return URL parameter keys
* @see ExtensionLoader#getActivateExtension(URL, String)
* @see ExtensionLoader#getActivateExtension(URL, String, String)
*/
String[] value() default {};
/**
* Relative ordering info, optional
*
* @return extension list which should be put before the current one
*/
String[] before() default {};
/**
* Relative ordering info, optional
*
* @return extension list which should be put after the current one
*/
String[] after() default {};
/**
* Absolute ordering info, optional
*
* @return absolute ordering info
*/
int order() default 0;
}
使用场景:
jdk spi中如果接口有多版本实现的话,会以迭代列表形式返回.
dubbo在此基础上添加了上面定义的3个概念.
3.代码示例
以Filter为例
代码示例2
public class Test1Filter implements Filter {
@Override
public Result invoke(Invoker> invoker, Invocation invocation) throws RpcException {
return null;
}
}
@Activate(group = Constants.PROVIDER,order = 2)
public class Test2Filter implements Filter {
@Override
public Result invoke(Invoker> invoker, Invocation invocation) throws RpcException {
return null;
}
}
@Activate(group = Constants.CONSUMER,order = 3)
public class Test3Filter implements Filter {
@Override
public Result invoke(Invoker> invoker, Invocation invocation) throws RpcException {
return null;
}
}
@Activate(group = {Constants.PROVIDER,Constants.CONSUMER},order = 4)
public class Test4Filter implements Filter {
@Override
public Result invoke(Invoker> invoker, Invocation invocation) throws RpcException {
return null;
}
}
@Activate(group = {Constants.PROVIDER,Constants.CONSUMER},order = 5,value = "test5")
public class Test5Filter implements Filter {
@Override
public Result invoke(Invoker> invoker, Invocation invocation) throws RpcException {
return null;
}
}
services的配置文件
image
以上定义了5个自定义Filter
Test1Filter没有自动激活,所以不会自动加载激活
Test2Filter的group为Constants.PROVIDER,添加order以保证有序执行
Test3Filter的group为Constants.CONSUMER
Test4Filter的group为Constants.PROVIDER和Constants.CONSUMER
Test5Filter的value为test5
下面我们再来看示例代码
代码示例3
public static void main(String[] args){
ExtensionLoader extensionLoader = ExtensionLoader.getExtensionLoader(Filter.class);
URL url=new URL("","",3);
url=url.addParameter("filterValue", "test5");
List list=extensionLoader.getActivateExtension(url,"",Constants.PROVIDER);
System.out.println(list);
list=extensionLoader.getActivateExtension(url,"",Constants.CONSUMER);
System.out.println(list);
list=extensionLoader.getActivateExtension(url,"filterValue",Constants.CONSUMER);
System.out.println(list);
}
除了返回内置的Filter之外,下面图片还返回了我们自己定义的Filter(在一定条件下自动激活)
图1 返回了Test2Filter和Test4Filter
image
图2 Test3Filter和Test4Filter
image
图3 Consumer的Filter和额外相关key的Filter
image
以Activate注解自动激活的方式可以方便地全局扩展dubbo Filter功能