AWS_Developer
AWS_Developer

Reputation: 856

How SpringBoot works on application servers

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

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().

Upvotes: 3

Views: 6877

Answers (1)

Michal Stepan
Michal Stepan

Reputation: 649

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.

Dev config:

@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);
    }
}

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);
    }
}

Profiles:

public class Profiles {

    public final static String DEV = "dev";

    public final static String DEPLOY = "deploy";
}

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

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

Upvotes: 4

Related Questions