Spring Boot:如何将另一个WAR文件添加到嵌入式tomcat? [英] Spring Boot: How to add another WAR files to the embedded tomcat?

查看:175
本文介绍了Spring Boot:如何将另一个WAR文件添加到嵌入式tomcat?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Boot的嵌入式tomcat非常便于开发和部署.

Spring Boot's embedded tomcat is very handy, for both development and deploy.

但是,如果应该添加另一个(第三方)WAR文件(例如GeoServer)怎么办?

But what if an another (3rd-party) WAR file (for example, GeoServer) should be added?

以下可能是正常过程:

  1. 安装普通的Tomcat服务器.
  2. 将Spring Boot应用程序构建为WAR文件,并将其添加到Tomcat的webapps文件夹中.
  3. 还将另一个(第三方)WAR文件添加到webapps文件夹中.

但是,如果可以进行以下配置,那就太好了.

But it would be nice if the following configuration were possible.

  1. 将Spring引导应用程序构建为一个独立的Jar,其中包括嵌入式Tomcat.
  2. 部署Spring引导应用程序Jar.
  3. 将另一个(第三方)WAR文件添加到嵌入式Tomcat可以识别的文件夹中.
  4. 使用嵌入式Tomcat同时提供Spring Boot应用程序的内容和另一个WAR的内容.

怎么办?

更新

当spring boot应用程序由胖jar(=可执行jar)组成时,答案中的代码是不够的.修改后的内容如下:

When the spring boot application is made of fat jar(=executable jar), the code in the answer is not enough. The revised one is as follows:

@Bean
public EmbeddedServletContainerFactory servletContainerFactory() {
    return new TomcatEmbeddedServletContainerFactory() {

        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                Tomcat tomcat) {
            try {
                Context context = tomcat.addWebapp("/foo", "/path/to/foo.war");
                WebappLoader loader =
                    new WebappLoader(Thread.currentThread().getContextClassLoader());
                context.setLoader(loader);
            } catch (ServletException ex) {
                throw new IllegalStateException("Failed to add webapp", ex);
            }
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }

    };
}

由于胖jar中的jar文件无法由系统类加载器加载,因此必须指定一个显式的父类加载器.否则,附加的WAR无法将库jar加载到添加了WAR的Spring Boot应用程序的胖jar中.

Since the jar files in a fat jar cannot be loaded by the system classloader, an explicit parent classloader must be specified. Otherwise, the additional WAR cannot load the library jars in the fat jar of the spring boot application that added the WAR.

推荐答案

您可以使用Tomcat.addWebapp将war文件添加到嵌入式Tomcat.就像它的javadoc所说的那样,它是相当于将Web应用程序添加到Tomcat的Web应用程序目录中".要在Spring Boot中使用此API,您需要使用自定义TomcatEmbeddedServletContainerFactory子类:

You can add a war file to embedded Tomcat using Tomcat.addWebapp. As its javadoc says, it's the "equivalent to adding a web application to Tomcat's web apps directory". To use this API in Spring Boot, you need to use a custom TomcatEmbeddedServletContainerFactory subclass:

@Bean
public EmbeddedServletContainerFactory servletContainerFactory() {
    return new TomcatEmbeddedServletContainerFactory() {

        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                Tomcat tomcat) {
            // Ensure that the webapps directory exists
            new File(tomcat.getServer().getCatalinaBase(), "webapps").mkdirs();

            try {
                Context context = tomcat.addWebapp("/foo", "/path/to/foo.war");
                // Allow the webapp to load classes from your fat jar
                context.setParentClassLoader(getClass().getClassLoader());
            } catch (ServletException ex) {
                throw new IllegalStateException("Failed to add webapp", ex);
            }
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }

    };
}

这篇关于Spring Boot:如何将另一个WAR文件添加到嵌入式tomcat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆