ClassLoaderFilter implements Filter { @Override public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { ClassLoader ocl = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(invoker.getInterface().getClassLoader()); try { return invoker.invoke(invocation); } finally { Thread.currentThread().setContextClassLoader(ocl); } }  @Override Result invoke(Invoker<?> invoker, Invocation invocation);  }
@Test public void testInvoke() throws Exception { URL url = URL.valueOf("test: String path = DemoService.class.getResource("/").getPath(); final URLClassLoader cl = new URLClassLoader(new java.net.URL[]{new java.net.URL("file:" + path)}) { @Override public Class<?> loadClass(String name) throws ClassNotFoundException { try { return findClass(name); } catch (ClassNotFoundException e) { return super.loadClass(name); } } }; final Class<?> clazz = cl.loadClass(DemoService.class.getCanonicalName()); Invoker invoker = new MyInvoker(url) { @Override public Class getInterface() { return clazz; } @Override public Result invoke(Invocation invocation) throws RpcException { Assertions.assertEquals(cl, Thread.currentThread().getContextClassLoader()); return null; } }; Invocation invocation = Mockito.mock(Invocation.class); classLoaderFilter.invoke(invoker, invocation); }