Maintain classpath order in PropertiesLauncher
I think this is safe, judging by the integration tests, but I'm not putting it in 1.2.x until we've had some feedback on it. The integration tests actually had a bug that was masking this problem because they were merging Properties from the whole classpath instead of picking the first available resource (which is generally what we do in Spring Boot applications for application.properties for instance). Fixes gh-3048
This commit is contained in:
@@ -17,12 +17,13 @@
|
||||
package org.springframework.boot.load.it.props;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
* Spring configuration.
|
||||
@@ -37,7 +38,9 @@ public class SpringConfiguration {
|
||||
|
||||
@PostConstruct
|
||||
public void init() throws IOException {
|
||||
String value = PropertiesLoaderUtils.loadAllProperties("application.properties").getProperty("message");
|
||||
Properties props = new Properties();
|
||||
props.load(new ClassPathResource("application.properties").getInputStream());
|
||||
String value = props.getProperty("message");
|
||||
if (value!=null) {
|
||||
this.message = value;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ assert out.contains('Hello Embedded Foo!'),
|
||||
'With loader.path=.,lib should use the application.properties from the local filesystem\n' + out
|
||||
|
||||
new File("${basedir}/target/application.properties").withWriter { it -> it << "message: Spam" }
|
||||
|
||||
out = exec("java -Dloader.path=target,.,lib -jar ${jarfile}")
|
||||
assert out.contains('Hello Embedded Spam!'),
|
||||
'With loader.path=target,.,lib should use the application.properties from the target directory\n' + out
|
||||
@@ -75,8 +75,6 @@ public abstract class Launcher {
|
||||
protected ClassLoader createClassLoader(List<Archive> archives) throws Exception {
|
||||
List<URL> urls = new ArrayList<URL>(archives.size());
|
||||
for (Archive archive : archives) {
|
||||
// Add the current archive at end (it will be reversed and end up taking
|
||||
// precedence)
|
||||
urls.add(archive.getUrl());
|
||||
}
|
||||
return createClassLoader(urls.toArray(new URL[urls.size()]));
|
||||
|
||||
@@ -448,8 +448,6 @@ public class PropertiesLauncher extends Launcher {
|
||||
}
|
||||
}
|
||||
addParentClassLoaderEntries(lib);
|
||||
// Entries are reversed when added to the actual classpath
|
||||
Collections.reverse(lib);
|
||||
return lib;
|
||||
}
|
||||
|
||||
|
||||
@@ -134,6 +134,17 @@ public class PropertiesLauncherTests {
|
||||
waitFor("Hello World");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUserSpecifiedClassPathOrder() throws Exception {
|
||||
System.setProperty("loader.path", "more-jars/app.jar,jars/app.jar");
|
||||
System.setProperty("loader.classLoader", URLClassLoader.class.getName());
|
||||
PropertiesLauncher launcher = new PropertiesLauncher();
|
||||
assertEquals("[more-jars/app.jar, jars/app.jar]",
|
||||
ReflectionTestUtils.getField(launcher, "paths").toString());
|
||||
launcher.launch(new String[0]);
|
||||
waitFor("Hello Other World");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomClassLoaderCreation() throws Exception {
|
||||
System.setProperty("loader.classLoader", TestLoader.class.getName());
|
||||
|
||||
Reference in New Issue
Block a user