From b5294a48b2f1836262421c75153d1b67e6b3466c Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 7 Sep 2016 10:00:34 +0100 Subject: [PATCH] Use context class loader instead of one-off for command location This works, and feels like the right thing to do, since there is no guarantee that extensions won't in turn use ServiceLoader for things that we haven't yet anticipated. Fixes gh-6829. Cc @wilkinsona in case he has an opinion. --- .../main/java/org/springframework/boot/cli/SpringCli.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java index 81c5c198bb..7fd33b8922 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java @@ -31,6 +31,7 @@ import org.springframework.boot.cli.command.core.HintCommand; import org.springframework.boot.cli.command.core.VersionCommand; import org.springframework.boot.cli.command.shell.ShellCommand; import org.springframework.boot.loader.tools.LogbackInitializer; +import org.springframework.util.ClassUtils; import org.springframework.util.SystemPropertyUtils; /** @@ -51,6 +52,7 @@ public final class SpringCli { LogbackInitializer.initialize(); CommandRunner runner = new CommandRunner("spring"); + ClassUtils.overrideThreadContextClassLoader(createExtendedClassLoader(runner)); runner.addCommand(new HelpCommand(runner)); addServiceLoaderCommands(runner); runner.addCommand(new ShellCommand()); @@ -66,14 +68,14 @@ public final class SpringCli { } private static void addServiceLoaderCommands(CommandRunner runner) { - ServiceLoader factories = ServiceLoader.load(CommandFactory.class, - createCommandClassLoader(runner)); + ServiceLoader factories = ServiceLoader + .load(CommandFactory.class); for (CommandFactory factory : factories) { runner.addCommands(factory.getCommands()); } } - private static URLClassLoader createCommandClassLoader(CommandRunner runner) { + private static URLClassLoader createExtendedClassLoader(CommandRunner runner) { return new URLClassLoader(getExtensionURLs(), runner.getClass().getClassLoader()); }