You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2018/09/14 08:40:59 UTC

[GitHub] jiangyunpeng opened a new issue #2507: Dubbo 's ExtensionLoader may be can't find custom Extension

jiangyunpeng opened a new issue #2507: Dubbo 's ExtensionLoader may be can't find custom Extension  
URL: https://github.com/apache/incubator-dubbo/issues/2507
 
 
   
   ### Environment
   
   * Dubbo version: 2.6.3
   * Operating System version: mac OS
   * Java version: 1.8
   
   ### Steps to reproduce this issue
   
   Dubbo通过 ExtensionLoader 加载各种SPI的实现,但是findClassLoader可能无法发现自定义SPI扩展点。
   
   dubbo ExtensionLoader 代码:
   
       private void loadFile(Map<String, Class<?>> extensionClasses, String dir) {
           String fileName = dir + type.getName();
           try {
               Enumeration<java.net.URL> urls;
               ClassLoader classLoader = findClassLoader();
               if (classLoader != null) {
                   urls = classLoader.getResources(fileName);
               } else {
                   urls = ClassLoader.getSystemResources(fileName);
               }
             ......
        }
   
       private static ClassLoader findClassLoader() {
           return ExtensionLoader.class.getClassLoader();
       }
   
   
   可以看到 ExtensionLoader 使用的是本身class所在的ClassLoader load 文件,当我们自定义的SPI扩展点和dubbo不在同一个classloader就会导致无法被load。
   
   
   建议修改为  org.springframework.util.ClassUtils 中更合理的实现方式:
   
   	public static ClassLoader getDefaultClassLoader() {
   		ClassLoader cl = null;
   		try {
   			cl = Thread.currentThread().getContextClassLoader();
   		}
   		catch (Throwable ex) {
   			// Cannot access thread context ClassLoader - falling back...
   		}
   		if (cl == null) {
   			// No thread context class loader -> use class loader of this class.
   			cl = ClassUtils.class.getClassLoader();
   			if (cl == null) {
   				// getClassLoader() returning null indicates the bootstrap ClassLoader
   				try {
   					cl = ClassLoader.getSystemClassLoader();
   				}
   				catch (Throwable ex) {
   					// Cannot access system ClassLoader - oh well, maybe the caller can live with null...
   				}
   			}
   		}
   		return cl;
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org