前言
Activate注解標(biāo)識一個擴(kuò)展是否被激活和使用鼻弧,可以放在定義的類上和方法上习霹,dubbo用它在SPI擴(kuò)張類定義上呜投,標(biāo)識這個擴(kuò)展實現(xiàn)激活的條件和時機(jī)加匈,先看下定義:
/**
* 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;
}
看到這里大家應(yīng)該明白,Activate
就是定義擴(kuò)展點(diǎn)實現(xiàn)類激活的條件仑荐,當(dāng)程序運(yùn)行的參數(shù)滿足這些條件時雕拼,自動激活這些擴(kuò)展點(diǎn)實現(xiàn)類。調(diào)用方法是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);
}
- 定義五個實現(xiàn)類辑甜,如下:
/**
* {@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
文件夾下創(chuàng)建一個文件,名稱為ActivateExt
的全限定名com.hui.wang.dubbo.learn.dubbo.activate.ActivateExt
逊笆,內(nèi)容如下:
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
到這里栈戳,前期的準(zhǔn)備完成,下面開始我們的測試使用階段
使用一: 關(guān)于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())));
}
打印結(jié)果為:
com.hui.wang.dubbo.learn.dubbo.activate.GroupActivateExtImpl
com.hui.wang.dubbo.learn.dubbo.activate.DefaultActivateExtImpl
這里可以看到命中了Activate
注解group
為default_group
的擴(kuò)展點(diǎn)實例子檀。
使用二: 關(guān)于Activate
注解的value
的使用,代碼如下:
@Test
public void testValue() {
ExtensionLoader<ActivateExt> extExtensionLoader = ExtensionLoader.getExtensionLoader(ActivateExt.class);
URL url = URL.valueOf("test://localhost/test");
/**
* 對應(yīng) @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())));
}
打印結(jié)果為:
com.hui.wang.dubbo.learn.dubbo.activate.ValueActivateExtImpl
可以看到命中了Activate
注解group
為value
乃戈,且value
為myKey
的擴(kuò)展點(diǎn)實例褂痰。
使用三: 關(guān)于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())));
}
打印結(jié)果為:
com.hui.wang.dubbo.learn.dubbo.activate.OrderActiveExtImplMore
com.hui.wang.dubbo.learn.dubbo.activate.OrderActivateExtImpl
可以看到命中了Activate
注解group
為order
的擴(kuò)展點(diǎn)實例症虑,且order
低的優(yōu)先級高
結(jié)尾
關(guān)于Activate
注解的基本使用已經(jīng)講解完了缩歪,在下節(jié)我們將具體講一下Activate
的源碼實現(xiàn)。