Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need minimal Spring configuration.
Spring Boot has a number of uses that do not require a web server: console applications, job scheduling, batch or stream processing, serverless applications, etc.
Table of Content :
- Introduction
- Not Using Web Dependencies
- Change Spring Application Type
- Using CommandLineRunner
- Using Property file
- Summary
There are many ways to run Spring boot Applications without the Web Server.
1). Not Using Web Dependencies
In this approach, we need to remove or not include the spring-boot-starter-web dependency in either the Maven POM or Gradle build file.
org.springframework.boot spring-boot-starter-web 2.3.4.RELEASE
2). Change Spring Application Type
if you want to run spring boot without a servlet container, but with one on the classpath (e.g. for tests), use the following, as described in the spring boot documentation:
@Configuration
@EnableAutoConfiguration
public class MySpringApp {
public static void main(String[] args) throws JAXBException {
SpringApplication app = new SpringApplication(MySpringApp.class);
app.setWebEnvironment(false); //this line we have to add
ConfigurableApplicationContext ctx = app.run(args);
}
}
or
new SpringApplicationBuilder(MySpringApp.class)
.web(WebApplicationType.NONE)
.run(args);
or
SpringApplication application = new SpringApplication(MySpringApp.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);
Where WebApplicationType:
- None - NoThe application should not run as a web application and should not start an embedded web server.
- REACTIVE - The application should run as a reactive web application and should start an embedded reactive web server
- SERVLET - The application should run as a servlet-based web application and should start an embedded servlet web server.
3). Using CommandLineRunner
Command-line runners are a useful functionality to execute the various types of code that only have to be run once, right after application startup.
Spring Batch relies on these runners in order to trigger the execution of the jobs.
To run the Spring boot Application without a server, We can use CommandLineRunner.We need to make changes in our Spring Application as given below.
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(false).run(args);
}
}
And
@Component
public class CommandLiner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// Put your logic here
}
}
4). Using Property file
In this approach, we need to add a statement in the application.properties file as.
spring.main.web-application-type=none
In the case of the YAML file, we have
spring:
main:
web-application-type: none
Questions/Articles related to Run Spring Boot App
Without Web a Server
Spring Boot Rest API example Spring Boot command line runner Example
What is Difference Between @ComponentScan and @EnableAutoConfiguration in Spring Boot
Spring Cloud AWS – S3 upload/download Examples
Spring Boot Custom Banner
In this article, we have seen Different ways to Run Spring Boot without a Web Server.
0 Comments
Post a Comment