org.springframework.boot.loader.JarLauncher源码实例Demo

类org.springframework.boot.loader.JarLauncher源码实例Demo

下面列出了org.springframework.boot.loader.JarLauncher 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: pivotal-bank-demo   文件: ExecJarRule.java
public ExecJarRule() {
  this.execJar = JarLauncher.class.getProtectionDomain().getCodeSource().getLocation().getFile();
}
 
源代码2 项目: liiklus   文件: ApplicationRunner.java
@SneakyThrows
public ConfigurableApplicationContext run() {
    System.setProperty("plugins.dir", findPluginsDir().getAbsolutePath());
    System.setProperty("plugins.pathMatcher", "*/build/libs/*.jar");

    var tempFile = Files.createTempFile("app", ".jar");
    tempFile.toFile().deleteOnExit();
    try (var appJarStream = getClass().getClassLoader().getResourceAsStream("app-boot.jar")) {
        Files.copy(appJarStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
    }

    var launcher = new JarLauncher(new JarFileArchive(tempFile.toFile(), tempFile.toUri().toURL())) {

        ClassLoader createClassLoader() throws Exception {
            return super.createClassLoader(getClassPathArchives());
        }

        @Override
        protected ClassLoader createClassLoader(URL[] urls) throws Exception {
            var systemClassLoader = ClassLoader.getSystemClassLoader();
            return new LaunchedURLClassLoader(urls, systemClassLoader.getParent()) {

                @Override
                protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
                    var classFile = findResource(name.replace(".", "/") + ".class");
                    if (classFile != null) {
                        // If exists in the app.jar, load it from the system classloader instead
                        log.debug("Loading class '{}' from the system ClassLoader instead", name);
                        return systemClassLoader.loadClass(name);
                    }
                    return super.loadClass(name, resolve);
                }
            };
        }
    };

    var currentClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        var appClassLoader = launcher.createClassLoader();
        Thread.currentThread().setContextClassLoader(appClassLoader);

        var applicationClass = appClassLoader.loadClass("com.github.bsideup.liiklus.Application");

        var createSpringApplicationMethod = applicationClass.getDeclaredMethod("createSpringApplication", String[].class);

        var application = (SpringApplication) createSpringApplicationMethod.invoke(null, (Object) new String[0]);
        application.setDefaultProperties(properties);
        return application.run();
    } finally {
        Thread.currentThread().setContextClassLoader(currentClassLoader);
    }
}
 
如果文章对您有帮助,请点击页面广告。以鼓励作者做出的努力。
 类所在包
 类方法
 同包方法