简体   繁体   English

如何在 Spring 引导嵌入式服务器(tomcat)中部署现有的 web 应用程序(战争)

[英]How I can deploy existing web applications (wars) in the Spring Boot embedded server (tomcat)

I have an application which allows to dynamically generate web applications (wars) and I would like to deploy these applications in a server to test them and I think of putting them in the same embedded server of spring, here is how I solved the problem with a simple main java.我有一个允许动态生成 web 应用程序(战争)的应用程序,我想将这些应用程序部署在服务器中以测试它们,我想将它们放在 spring 的同一嵌入式服务器中,这就是我解决问题的方法一个简单的主 java。

public class Main {
    private final static Logger logger = LoggerFactory.getLogger(Main.class);
    private final static File catalinaHome = new File(
            "C:\\Users\\Dev\\Desktop\\demo\\userstory-2\\compiler\\patternHost");
    private static Tomcat tomcat = null;
    public static void main(String[] args) {
        tomcat = new Tomcat();
        tomcat.setPort(8080);
        tomcat.setBaseDir(catalinaHome.getAbsolutePath());
        tomcat.getHost().setAutoDeploy(true);
        tomcat.getHost().setDeployOnStartup(true);
        tomcat.getServer().addLifecycleListener(new VersionLoggerListener());
        tomcat.getHost().addLifecycleListener(new HostConfig());
        try {
            tomcat.start();
        } catch (LifecycleException e) {
            logger.error("Tomcat could not be started.");
            e.printStackTrace();
        }
        logger.info("Tomcat started on " + tomcat.getHost());
        tomcat.getServer().await();
    }
}

How can I do the same with spring boot.我怎样才能对 spring 启动做同样的事情。 ? ?

I have converted a non-spring app to spring boot in this way and it worked for me.我以这种方式将非弹簧应用程序转换为 spring 启动,它对我有用。 I was able to run it with spring boot embedded tomcat.我能够使用 spring 引导嵌入式 tomcat 运行它。 Hope this helps.希望这可以帮助。

Spring boot is all about speed, it comes with embedded-tomcat server(provided you use spring-boot-starter-web dependency) and now all you need is java to run your standalone spring boot application. Spring 启动完全与速度有关,它带有嵌入式 tomcat 服务器(前提是您使用 spring-boot-starter-web 依赖项),现在您只需要 java 即可运行您的独立 Z2A2D595E6ED9A0B24F027F4B 应用程序。 It reduces the manual steps of copying war file to tomcat's webapp folder and then starting it.它减少了将war文件复制到tomcat的webapp文件夹然后启动它的手动步骤。

Try the approach which suits your app.尝试适合您的应用的方法。

If your old app is spring based:如果您的旧应用基于 spring:

  1. Create a new spring boot starter web project and copy your old source code to this new project.创建一个新的 spring 引导启动器 web 项目并将您的旧源代码复制到这个新项目。 Modify application.properties, resources folder, add all required dependencies in pom.xml file and change package as war .修改 application.properties、resources 文件夹,在 pom.xml 文件中添加所有需要的依赖项并将 package 更改为war
  2. Do a mvn clean install it will generate a war file (with embedded tomcat) in target folder of your project's root directory.执行mvn clean install它将在项目根目录的目标文件夹中生成一个 war 文件(带有嵌入式 tomcat)。 Now to run it all you need to do is, in your target folder open terminal and run java -jar your_warFileName.war it will start the application.现在要运行它,您需要做的就是在目标文件夹中打开终端并运行java -jar your_warFileName.war它将启动应用程序。

If your old app is not a spring based:如果您的旧应用程序不是基于 spring 的:

  1. Again start with new spring boot starter web project and copy your source code but then to use your old code with spring-boot, first you have to do clean-up stuff like adding @RestController to controller classes, declaring beans by marking classes with @Service or @Component and autowiring beans in the appropriate places. Again start with new spring boot starter web project and copy your source code but then to use your old code with spring-boot, first you have to do clean-up stuff like adding @RestController to controller classes, declaring beans by marking classes with @Service or @Component 并在适当的地方自动装配 bean。 Once your code compiles fine then to run it your can use step 2 as above.一旦您的代码编译良好,然后运行它,您可以使用上面的步骤 2。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何注册可以被多个WAR共享的bean(Spring boot + Tomcat) - How to register beans that can be shared by multiple WARs (Spring boot + Tomcat) 如何在 tomcat 服务器上部署 spring 启动 web 应用程序 - How to deploy spring boot web application on tomcat server 在具有相同端口的外部 tomcat 上部署不同的 Spring Boot War - deploy different spring boot wars on external tomcat with same port 如何启动和停止部署在 tomcat 嵌入式服务器中的应用程序 - How I can start and stop applications deployed in a tomcat embedded server 如何自定义spring boot embedded tomcat线程池? - How can I customize spring boot embedded tomcat thread pool? 如何使用嵌入式 tomcat 减少 Spring Boot 应用程序的内存使用量? - How to reduce Spring boot applications memory usage with embedded tomcat? 如何在同一个 Tomcat 上部署多个带有外部配置的 Spring Boot 应用程序? - How to deploy multiple Spring boot applications with external configurations on the same Tomcat? 如何为嵌入在我的 spring-boot 应用程序中的 Web 服务器(Tomcat)配置低级套接字属性? - How to configure low-level socket properties for a web server (Tomcat) embedded in my spring-boot application? spring boot的嵌入式tomcat服务器如何加载外部“war”文件 - How load a external "war" file to the embedded tomcat server of spring boot 如何在外部 tomcat 服务器上部署 spring 启动应用程序? - How to deploy spring boot app on external tomcat server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM