前言
Activate注解标识一个扩展是否被激活和使用,可以放在定义的类上和方法上,dubbo用它在SPI扩张类定义上,标识这个扩展实现激活的条件和时机,先看下定义:
/**
* Activate. This annotation is useful for automatically activate certain extensions with the given criteria,
* for examples: <code>@Activate</code> can be used to load certain <code>Filter</code> extension when there are
* multiple implementations.
* <ol>
* <li>{@link Activate#group()} specifies group criteria. Framework SPI defines the valid group values.
* <li>{@link Activate#value()} specifies parameter key in {@link URL} criteria.
* </ol>
* SPI provider can call {@link ExtensionLoader#getActivateExtension(URL, String, String)} to find out all activated
* extensions with the given criteria.
*
* @see SPI
* @see URL
* @see ExtensionLoader
*/
@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.
* <p>
* For example, given <code>@Activate("cache, validation")</code>, the current extension will be return only when
* there's either <code>cache</code> or <code>validation</code> key appeared in the URL's parameters.
* </p>
*
* @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;
}
看到这里大家应该明白,Activate
就是定义扩展点实现类激活的条件,当程序运行的参数满足这些条件时,自动激活这些扩展点实现类。调用方法是ExtensionLoader#getActivateExtension
。
接下来我们看看Activate
注解的使用。
使用示例
- 首先定义一个接口,取名为
ActivateExt
,代码如下:
/**
* {@link com.alibaba.dubbo.common.extension.Activate} 使用
*
* @author hui.wang09
* @since 31 January 2019
*/
@SPI
public interface ActivateExt {
String echo(String msg);
}
- 定义五个实现类,如下:
/**
* {@link ActivateExt} impl
*
* @author hui.wang09
* @since 31 January 2019
*/
@Activate(group = {"default_group"})
public class DefaultActivateExtImpl implements ActivateExt{
@Override
public String echo(String msg) {
return msg;
}
}
/**
* {@link ActivateExt} impl
*
* @author hui.wang09
* @since 31 January 2019
*/
@Activate(group = {"group1", "default_group"})
public class GroupActivateExtImpl implements ActivateExt{
@Override
public String echo(String msg) {
return msg;
}
}
/**
* {@link ActivateExt} impl
*
* @author hui.wang09
* @since 31 January 2019
*/
@Activate(order = 2, group = {"order"})
public class OrderActivateExtImpl implements ActivateExt{
@Override
public String echo(String msg) {
return msg;
}
}
/**
* {@link ActivateExt} impl
*
* @author hui.wang09
* @since 31 January 2019
*/
@Activate(order = 1, group = {"order"})
public class OrderActiveExtImplMore implements ActivateExt{
@Override
public String echo(String msg) {
return msg;
}
}
/**
* {@link ActivateExt} impl
*
* @author hui.wang09
* @since 31 January 2019
*/
@Activate(value = {"myKey"}, group = {"value"})
public class ValueActivateExtImpl implements ActivateExt{
@Override
public String echo(String msg) {
return msg;
}
}
- 接着在
META-INF/dubbo
文件夹下创建一个文件,名称为ActivateExt
的全限定名com.hui.wang.dubbo.learn.dubbo.activate.ActivateExt
,内容如下:
group=com.hui.wang.dubbo.learn.dubbo.activate.GroupActivateExtImpl
order1=com.hui.wang.dubbo.learn.dubbo.activate.OrderActiveExtImplMore
order2=com.hui.wang.dubbo.learn.dubbo.activate.OrderActivateExtImpl
value=com.hui.wang.dubbo.learn.dubbo.activate.ValueActivateExtImpl
com.hui.wang.dubbo.learn.dubbo.activate.DefaultActivateExtImpl
到这里,前期的准备完成,下面开始我们的测试使用阶段
使用一: 关于ActivateExt
注解的group
的使用,代码如下:
@Test
public void testGroup() {
ExtensionLoader<ActivateExt> extExtensionLoader = ExtensionLoader.getExtensionLoader(ActivateExt.class);
URL url = URL.valueOf("test://localhost/test");
List<ActivateExt> list = extExtensionLoader.getActivateExtension(url, new String[]{}, "default_group");
list.forEach(o -> System.out.println(o.echo(o.getClass().getName())));
}
打印结果为:
com.hui.wang.dubbo.learn.dubbo.activate.GroupActivateExtImpl
com.hui.wang.dubbo.learn.dubbo.activate.DefaultActivateExtImpl
这里可以看到命中了Activate
注解group
为default_group
的扩展点实例。
使用二: 关于Activate
注解的value
的使用,代码如下:
@Test
public void testValue() {
ExtensionLoader<ActivateExt> extExtensionLoader = ExtensionLoader.getExtensionLoader(ActivateExt.class);
URL url = URL.valueOf("test://localhost/test");
/**
* 对应 @Activate(value = {"myKey"})
*/
url = url.addParameter("myKey", "test");
List<ActivateExt> list = extExtensionLoader.getActivateExtension(url, new String[]{}, "value");
list.forEach(o -> System.out.println(o.echo(o.getClass().getName())));
}
打印结果为:
com.hui.wang.dubbo.learn.dubbo.activate.ValueActivateExtImpl
可以看到命中了Activate
注解group
为value
,且value
为myKey
的扩展点实例。
使用三: 关于Activate
注解order
的使用,代码如下:
@Test
public void test3() {
ExtensionLoader<ActivateExt> extExtensionLoader = ExtensionLoader.getExtensionLoader(ActivateExt.class);
URL url = URL.valueOf("test://localhost/test");
List<ActivateExt> list = extExtensionLoader.getActivateExtension(url, new String[]{}, "order");
list.forEach(o -> System.out.println(o.echo(o.getClass().getName())));
}
打印结果为:
com.hui.wang.dubbo.learn.dubbo.activate.OrderActiveExtImplMore
com.hui.wang.dubbo.learn.dubbo.activate.OrderActivateExtImpl
可以看到命中了Activate
注解group
为order
的扩展点实例,且order
低的优先级高
结尾
关于Activate
注解的基本使用已经讲解完了,在下节我们将具体讲一下Activate
的源码实现。