DelegatingClassLoader extends ClassLoader { @Override public URL getResource(String name) { for (ClassLoader delegate : delegates) { URL url = delegate.getResource(name); if (url != null) { return url; } if (log.isTraceEnabled()) { log.trace("URL " + name + " not found with classloader: " + delegate + ". Trying other delegates"); } } return null; } DelegatingClassLoader(ClassLoader... delegates); @Override InputStream getResourceAsStream(String name); @Override URL getResource(String name); @Override Enumeration<URL> getResources(String name);  }
@Test public void testGetResource() throws IOException { ClassLoader cl = new DelegatingClassLoader(clOne, clTwo, clThree); URL propsUrl = cl.getResource("test.properties"); String val = loadProperty(propsUrl, "key"); Assert.assertEquals("one", val); propsUrl = cl.getResource("nonexisting"); Assert.assertNull("Looking for non-existing resource must return null", propsUrl); propsUrl = cl.getResource("test.conf"); val = loadProperty(propsUrl, "key"); Assert.assertEquals("test.conf", val); propsUrl = cl.getResource("sub/test.properties"); val = loadProperty(propsUrl, "key"); Assert.assertEquals("sub one", val); cl = new DelegatingClassLoader(clTwo, clThree, clOne); propsUrl = cl.getResource("test.properties"); val = loadProperty(propsUrl, "key"); Assert.assertEquals("two", val); propsUrl = cl.getResource("sub/test.properties"); val = loadProperty(propsUrl, "key"); Assert.assertEquals("sub one", val); }