简体   繁体   English

Spring boot - 可执行战争也可部署到应用程序服务器

[英]Spring boot - executable war also deployable to app server

Let's say I have a spring boot web application - It is runnable via gradle (embedded tomcat).假设我有一个 Spring Boot Web 应用程序 - 它可以通过 gradle(嵌入式 tomcat)运行。 But I need it also to be possible to deploy war in standard way into app server.但我也需要它能够以标准方式将战争部署到应用程序服务器中。 How the app should be configured?应用程序应该如何配置? Standard web.xml along with xml configuration?标准 web.xml 以及 xml 配置?

Currently I have something like:目前我有类似的东西:

@SpringBootApplication

public class MyApplication extends SpringBootServletInitializer {公共类 MyApplication 扩展 SpringBootServletInitializer {

public static void main(String[] args) {
    System.setProperty("spring.profiles.active", "dev");
    SpringApplication.run(MyApplication.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(MyApplication.class);
}

@Configuration
@ConditionalOnWebApplication
public static class WebConfiguration {

    @Bean
    public ServletListenerRegistrationBean<ServletContextListener> registerClientCookieConfigListener () {
        ServletListenerRegistrationBean<ServletContextListener> srb =
                new ServletListenerRegistrationBean<>();
        srb.setListener(new MyConfigListener());
        return srb;
    }

    @Bean
    public ServletListenerRegistrationBean<HttpSessionListener> registerMySessionConfigListener () {
        ServletListenerRegistrationBean<HttpSessionListener> srb =
                new ServletListenerRegistrationBean<>();
        srb.setListener(new MySessionConfigListener());
        return srb;
    }


    @Bean
    public FilterRegistrationBean registerLoginFilter() {
        FilterRegistrationBean filter = new FilterRegistrationBean(new MyFilter());
        filter.setUrlPatterns(Collections.singletonList("/*"));
        return filter;
    }

    @Bean
    public ServletRegistrationBean registerSAMLDispatcherServlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean(
                new DispatcherServlet(), "/test/*");
        bean.setLoadOnStartup(1);
        return bean;
    }
}

} }

which is 1:1 mapping to web.xml.这是 1:1 映射到 web.xml。 Is it even possible to deploy it to app server without web.xml?甚至可以在没有 web.xml 的情况下将其部署到应用程序服务器吗?

You don't need web.xml to deploy spring boot to standalone tomcat server or any other web server.您不需要 web.xml 将 spring boot 部署到独立的 tomcat 服务器或任何其他 web 服务器。

spring boot does not rely on xml configurations, it configures an equivalent to the dispatcher servlet automatically. spring boot 不依赖于 xml 配置,它自动配置了一个相当于调度程序 servlet 的东西。

to deploy a spring boot app to an another server, you need to update your packaging to war in maven要将 Spring Boot 应用程序部署到另一台服务器,您需要在 maven 中将打包更新为 war

 <packaging>war</packaging>

and tell maven that a webserver will be available in the runtime and don't package it with scope provided并告诉 Maven 一个网络服务器将在运行时可用,并且不要使用provided范围对其进行打包

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
   <scope>provided</scope>
</dependency>

few documentations很少的文件

https://www.baeldung.com/spring-boot-war-tomcat-deploy https://www.baeldung.com/spring-boot-war-tomcat-deploy

https://www.mkyong.com/spring-boot/spring-boot-deploy-war-file-to-tomcat/ https://www.mkyong.com/spring-boot/spring-boot-deploy-war-file-to-tomcat/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM