Refine SpringApplication source types
Update `SpringApplication` so that the `run` methods and constructors now require `Class<?>` arguments, rather than `Objects`. String based sources can still be loaded, but must now be set on the `getSources()` collections. `Package` and `Resource` types are no longer directly supported. This change should help IDEs offer better content assist, and will help integrations with alternative languages such as Ceylon. Users currently passing in Class references or using the `spring.main.sources` property should not be affected by this change. If an XML resource is being used, some refactoring may be required (see the changes to `SampleSpringXmlApplication` in this commit). Fixes gh-9170
This commit is contained in:
@@ -56,12 +56,12 @@ public class SpringApplicationLauncher {
|
||||
* @return The application's {@code ApplicationContext}
|
||||
* @throws Exception if the launch fails
|
||||
*/
|
||||
public Object launch(Object[] sources, String[] args) throws Exception {
|
||||
public Object launch(Class<?>[] sources, String[] args) throws Exception {
|
||||
Map<String, Object> defaultProperties = new HashMap<>();
|
||||
defaultProperties.put("spring.groovy.template.check-template-location", "false");
|
||||
Class<?> applicationClass = this.classLoader
|
||||
.loadClass(getSpringApplicationClassName());
|
||||
Constructor<?> constructor = applicationClass.getConstructor(Object[].class);
|
||||
Constructor<?> constructor = applicationClass.getConstructor(Class[].class);
|
||||
Object application = constructor.newInstance((Object) sources);
|
||||
applicationClass.getMethod("setDefaultProperties", Map.class).invoke(application,
|
||||
defaultProperties);
|
||||
|
||||
@@ -51,7 +51,7 @@ public final class PackagedSpringApplicationLauncher {
|
||||
new SpringApplicationLauncher(classLoader).launch(getSources(classLoader), args);
|
||||
}
|
||||
|
||||
private Object[] getSources(URLClassLoader classLoader) throws Exception {
|
||||
private Class<?>[] getSources(URLClassLoader classLoader) throws Exception {
|
||||
Enumeration<URL> urls = classLoader.getResources("META-INF/MANIFEST.MF");
|
||||
while (urls.hasMoreElements()) {
|
||||
URL url = urls.nextElement();
|
||||
|
||||
@@ -98,7 +98,7 @@ public class SpringApplicationRunner {
|
||||
synchronized (this.monitor) {
|
||||
try {
|
||||
stop();
|
||||
Object[] compiledSources = compile();
|
||||
Class<?>[] compiledSources = compile();
|
||||
monitorForChanges();
|
||||
// Run in new thread to ensure that the context classloader is setup
|
||||
this.runThread = new RunThread(compiledSources);
|
||||
@@ -125,8 +125,8 @@ public class SpringApplicationRunner {
|
||||
}
|
||||
}
|
||||
|
||||
private Object[] compile() throws IOException {
|
||||
Object[] compiledSources = this.compiler.compile(this.sources);
|
||||
private Class<?>[] compile() throws IOException {
|
||||
Class<?>[] compiledSources = this.compiler.compile(this.sources);
|
||||
if (compiledSources.length == 0) {
|
||||
throw new RuntimeException(
|
||||
"No classes found in '" + Arrays.toString(this.sources) + "'");
|
||||
@@ -148,7 +148,7 @@ public class SpringApplicationRunner {
|
||||
|
||||
private final Object monitor = new Object();
|
||||
|
||||
private final Object[] compiledSources;
|
||||
private final Class<?>[] compiledSources;
|
||||
|
||||
private Object applicationContext;
|
||||
|
||||
@@ -156,10 +156,10 @@ public class SpringApplicationRunner {
|
||||
* Create a new {@link RunThread} instance.
|
||||
* @param compiledSources the sources to launch
|
||||
*/
|
||||
RunThread(Object... compiledSources) {
|
||||
RunThread(Class<?>... compiledSources) {
|
||||
super("runner-" + (runnerCounter++));
|
||||
this.compiledSources = compiledSources;
|
||||
if (compiledSources.length != 0 && compiledSources[0] instanceof Class) {
|
||||
if (compiledSources.length != 0) {
|
||||
setContextClassLoader(((Class<?>) compiledSources[0]).getClassLoader());
|
||||
}
|
||||
setDaemon(true);
|
||||
|
||||
@@ -73,7 +73,7 @@ public class SpringApplicationLauncherTests {
|
||||
public void sourcesDefaultPropertiesAndArgsAreUsedToLaunch() throws Exception {
|
||||
System.setProperty("spring.application.class.name",
|
||||
TestSpringApplication.class.getName());
|
||||
Object[] sources = new Object[0];
|
||||
Class<?>[] sources = new Class<?>[0];
|
||||
String[] args = new String[0];
|
||||
new SpringApplicationLauncher(getClass().getClassLoader()).launch(sources, args);
|
||||
|
||||
@@ -88,7 +88,7 @@ public class SpringApplicationLauncherTests {
|
||||
private Set<String> launch() {
|
||||
TestClassLoader classLoader = new TestClassLoader(getClass().getClassLoader());
|
||||
try {
|
||||
new TestSpringApplicationLauncher(classLoader).launch(new Object[0],
|
||||
new TestSpringApplicationLauncher(classLoader).launch(new Class<?>[0],
|
||||
new String[0]);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
@@ -129,7 +129,7 @@ public class SpringApplicationLauncherTests {
|
||||
|
||||
private static String[] args;
|
||||
|
||||
public TestSpringApplication(Object[] sources) {
|
||||
public TestSpringApplication(Class<?>[] sources) {
|
||||
TestSpringApplication.sources = sources;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user