SpringBoot 如何在应用服务器上工作 [英] How SpringBoot works on application servers

查看:44
本文介绍了SpringBoot 如何在应用服务器上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正致力于将我们的 Spring REST 应用程序迁移到 Spring Boot 微服务.我有点怀疑:

I am working on migrating our Spring REST applications to Spring Boot microservices. I have some doubt:

据我所知,spring boot 有一个 main(),它调用 SpringApplication 中存在的静态 run().因此,当我们在 Eclipse IDE 中将它作为Java 应用程序"运行时,这个 main() 被调用并且 @SpringBootApplication 注释发挥了神奇的作用.但是当我在 Websphere Application Servers 上部署这个应用程序时,它是如何工作的,因为现在 main() 不会被调用.那么如何在不调用 main() 的情况下加载所有 bean.

As I know spring boot has a main() and it calls static run() which is present in SpringApplication. So, when we run it as "Java Applicaton" in Eclipse IDE, this main() gets called and @SpringBootApplication annotation do the magic. But when I deploy this application on Websphere Application Servers, then how is this working because now the main() will not gets called. Then how all the beans getting loaded without calling the main().

推荐答案

Spring Boot 隐式启动嵌入式服务器,它包含在 spring-boot-starter-tomcat 依赖项中.这就是 main() 方法在引导环境中工作的原因.

Spring Boot implicitely starts an embedded server, which is included in spring-boot-starter-tomcat dependency. That's why main() method works in boot environment.

典型的解决方案是创建两个配置文件 - 一个用于启动开发,另一个用于部署 - 那么您可能有几个起点.

Typical solution is to create two profiles - one for boot development and the other for deployment - then you may have several starting points.

开发配置:

@Configuration
@Profile(Profiles.DEV)
@Import(AppConfig.class)
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class)
            .profiles(Profiles.DEV)
            .run(args);
    }
}

部署配置(适用于 WAS、tomcat、...):

Deploy config (for WAS, tomcat, ...):

@Configuration
@Profile(Profiles.DEPLOY)
@Import(AppConfig.class)
public class ApplicationTomcat extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application
            .profiles(Profiles.DEPLOY)
            .sources(ApplicationTomcat.class);
    }
}

个人资料:

public class Profiles {

    public final static String DEV = "dev";

    public final static String DEPLOY = "deploy";
}

在您的部署配置文件中,不要忘记排除 spring-boot-starter-tomcat 依赖项并使其成为 war 包.

In your deployment profile, don't forget to exclude spring-boot-starter-tomcat dependency and make it war bundle.

通过这种方式,您可以以标准方式在 WAS(或 tomcat,...)上部署应用程序,并使用 main() 方法在本地启动您的应用程序.

This way you can deploy application on WAS (or tomcat, ...) in standard way and start your application localy also with main() method.

这篇关于SpringBoot 如何在应用服务器上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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