@SPI public interface ActivateExt { String echo(String msg); }
@Activate(group = {"default"}) public class ActivateExtImpl1 implements ActivateExt { @Override public String echo(String msg) { return msg; } } @Activate(group = {"group1", "group2"}) public class GroupActivateExtImpl implements ActivateExt { @Override public String echo(String msg) { return msg; } }
@Activate(order = 2, group = {"order"}) public class OrderActivateExtImpl1 implements ActivateExt { @Override public String echo(String msg) { return msg; } } @Activate(order = 1, group = {"order"}) public class OrderActivateExtImpl2 implements ActivateExt { @Override public String echo(String msg) { return msg; } }
@Activate(value = {"value"}, group = {"group"}) public class ValueActivateExtImpl implements ActivateExt { @Override public String echo(String msg) { return msg; } }
在resources下新建META-INF/dubbo/internal文件夾,新建com.alibaba.dubbo.demo.provider.activate.ActivateExt,即接口的全限定名
,文件內容爲:segmentfault
group=com.alibaba.dubbo.demo.provider.activate.impl.GroupActivateExtImpl key=com.alibaba.dubbo.demo.provider.activate.impl.ValueActivateExtImpl order1=com.alibaba.dubbo.demo.provider.activate.impl.OrderActivateExtImpl1 order2=com.alibaba.dubbo.demo.provider.activate.impl.OrderActivateExtImpl2 com.alibaba.dubbo.demo.provider.activate.impl.ActivateExtImpl1
下面是幾個單元測試及其測試結果,觀察測試結果咱們能夠得出如下結論:
1) 若是loader.getActivateExtension(URL url, String[] values, String group)中的group參數可以匹配到,就是咱們要選擇的,test1中根據group=default匹配到了ActivateExtImpl1,它也是在此條件下須要激活的
2) @Activate中的value是第二層校驗參數,第一層是group,在group校驗經過的狀況下,若是URL中的key參數與類@Activate註解中的value參數一致且key參數對應的值不爲空
,則纔會被選中
3) order越小,優先級越高ide
/** * 1 * class com.alibaba.dubbo.demo.provider.activate.impl.ActivateExtImpl1 */ @Test public void test1(){ ExtensionLoader<ActivateExt> loader = ExtensionLoader.getExtensionLoader(ActivateExt.class); URL url = URL.valueOf("test://localhost/test"); List<ActivateExt> list = loader.getActivateExtension(url, new String[]{}, "default"); System.out.println(list.size()); list.forEach(item -> System.out.println(item.getClass())); }
/** * 1 * class com.alibaba.dubbo.demo.provider.activate.impl.GroupActivateExtImpl */ @Test public void test2(){ ExtensionLoader<ActivateExt> loader = ExtensionLoader.getExtensionLoader(ActivateExt.class); URL url = URL.valueOf("test://localhost/test"); List<ActivateExt> list = loader.getActivateExtension(url, new String[]{}, "group1"); System.out.println(list.size()); list.forEach(item -> System.out.println(item.getClass())); }
/** * 2 * class com.alibaba.dubbo.demo.provider.activate.impl.OrderActivateExtImpl1 * class com.alibaba.dubbo.demo.provider.activate.impl.ValueActivateExtImpl */ @Test public void test3(){ ExtensionLoader<ActivateExt> loader = ExtensionLoader.getExtensionLoader(ActivateExt.class); URL url = URL.valueOf("test://localhost/test"); // 注意這裏要使用url接收,不能直接url.addParameter() url = url.addParameter("value", "test"); List<ActivateExt> list = loader.getActivateExtension(url, new String[]{"order1", "default"}, "group"); System.out.println(list.size()); list.forEach(item -> System.out.println(item.getClass())); }
/** * 2 * class com.alibaba.dubbo.demo.provider.activate.impl.OrderActivateExtImpl2 * class com.alibaba.dubbo.demo.provider.activate.impl.OrderActivateExtImpl1 */ @Test public void test4(){ ExtensionLoader<ActivateExt> loader = ExtensionLoader.getExtensionLoader(ActivateExt.class); URL url = URL.valueOf("test://localhost/test"); List<ActivateExt> list = loader.getActivateExtension(url, new String[]{}, "order"); System.out.println(list.size()); list.forEach(item -> System.out.println(item.getClass())); }
public void test3(){ ExtensionLoader<ActivateExt> loader = ExtensionLoader.getExtensionLoader(ActivateExt.class); URL url = URL.valueOf("test://localhost/test"); // 注意這裏要使用url接收,不能直接url.addParameter() url = url.addParameter("value", "test"); List<ActivateExt> list = loader.getActivateExtension(url, new String[]{"order1", "default"}, "group"); System.out.println(list.size()); list.forEach(item -> System.out.println(item.getClass())); }
public List<T> getActivateExtension(URL url, String[] values, String group) { List<T> exts = new ArrayList<T>(); // 將傳遞進來的values包裝成List類型的names List<String> names = values == null ? new ArrayList<String>(0) : Arrays.asList(values); // 包裝好的數據中不含"-default" if (!names.contains(Constants.REMOVE_VALUE_PREFIX + Constants.DEFAULT_KEY)) { // 獲取這個類型的數據的全部擴展信息 getExtensionClasses(); for (Map.Entry<String, Activate> entry : cachedActivates.entrySet()) { // 獲取拓展的名稱,在配置文件中配置的key=value中的key值 String name = entry.getKey(); // 獲取註解 Activate activate = entry.getValue();
// 判斷group是否匹配,group是程序傳進來的值,若是沒有設定則返回true表示匹配, // 若是設定了須要進行比較匹配 if (isMatchGroup(group, activate.group())) { // 獲取key值即name對應的實例 T ext = getExtension(name); if (!names.contains(name) && !names.contains(Constants.REMOVE_VALUE_PREFIX + name) && isActive(activate, url)) { exts.add(ext); } } } // 排序 Collections.sort(exts, ActivateComparator.COMPARATOR); }
List<T> usrs = new ArrayList<T>(); for (int i = 0; i < names.size(); i++) { String name = names.get(i); if (!name.startsWith(Constants.REMOVE_VALUE_PREFIX) && !names.contains(Constants.REMOVE_VALUE_PREFIX + name)) { if (Constants.DEFAULT_KEY.equals(name)) { // 若是name是default,就將usrs加入exts頭部並清空usrs if (!usrs.isEmpty()) { exts.addAll(0, usrs); usrs.clear(); } } else { // 獲取name對應的拓展並加入usrs T ext = getExtension(name); usrs.add(ext); } } } if (!usrs.isEmpty()) { exts.addAll(usrs); } return exts; }
上述代碼中用到的cachedActivates是在getExtensionClasses()方法中賦值的,具體能夠參考:Dubbo的SPI機制分析1-SPI加載class源碼分析
Activate activate = clazz.getAnnotation(Activate.class); if (activate != null) { cachedActivates.put(names[0], activate); }
// 判斷group是否匹配,很好理解 private boolean isMatchGroup(String group, String[] groups) { if (group == null || group.length() == 0) { return true; } if (groups != null && groups.length > 0) { for (String g : groups) { if (group.equals(g)) { return true; } } } return false; }
// 將url中的鍵key與Activate註解中的value值作匹配,而且key所對應的value值不能爲空 private boolean isActive(Activate activate, URL url) { String[] keys = activate.value(); if (keys.length == 0) { return true; } for (String key : keys) { for (Map.Entry<String, String> entry : url.getParameters().entrySet()) { String k = entry.getKey(); String v = entry.getValue(); if ((k.equals(key) || k.endsWith("." + key)) && ConfigUtils.isNotEmpty(v)) { return true; } } } return false; }
注意,若是是如下這種形式,傳入的values中有-default,則全部的匹配能夠經過的@Activate都不會被激活,只有values中指定的能夠被激活。若是傳入了「-」符合開頭的擴展類名,則該擴展點也不會被激活。單元測試
// 輸出class com.alibaba.dubbo.demo.provider.activate.impl.OrderActivateExtImpl1 ExtensionLoader<ActivateExt> loader = ExtensionLoader.getExtensionLoader(ActivateExt.class); URL url = URL.valueOf("test://localhost/test"); // 注意這裏要使用url接收,不能直接url.addParameter() url = url.addParameter("value", "test"); List<ActivateExt> list = loader.getActivateExtension(url, new String[]{"order1", "-default"}, "group"); System.out.println(list.size()); list.forEach(item -> System.out.println(item.getClass()));