I have a Spring Boot application which was generated using jhipster, and it works fine. However, I also need to create a second application for some back-office batch jobs, and this application uses most of the spring services of the first application. What I did is create a second main class, which starts a spring boot application. The problem is this also starts the embedded web-server and all the services that are only useful for the web app. I only need the services, persistence and other classes that are not specifically tied to the GUI.
Here are my two main classes (simplified)
The normal spring-boot app:
@ComponentScan
@AutoConfigure
class Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class)
app.run(args)
}
}
The back-office app:
@ComponentScan
@AutoConfigure
class BackOfficeApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(BackOfficeApplication.class)
app.run(args)
}
}
What works: My back office application has access to everything that I need. Spring services, beans, etc. The problem: The back office app starts the GUI, preventing me to launch it twice at the same time.
Is there a way to deactivate the launching of the embedded tomcat server? Otherwise, is there a way to load the spring application context in another way that wouldn't start the embedded server ?
Some details: * I don't start the app by using mvn spring-boot:run. I launch the class directly with java (or using eclipse
So if you want to have spring boot without the web server just don't use the dependencies spring-boot-starter-jersey or spring-boot-starter-web since if you build your application as a jar file there is no reason to have those dependencies and not have an embedded server delivered.
If we want to exclude tomcat from spring boot, we don't need to do much, we just need to add one additional block(<exclusions>) to the Spring Boot dependency. <exclusions> tag is used to make us sure that given server/artifactId is being removed at the time of build.
The easiest way to prevent a Spring Boot application from starting an embedded web server is to not include the web server starter in our dependencies. This means not including the spring-boot-starter-web dependency in either the Maven POM or Gradle build file.
The Spring Boot framework provides the default embedded server (Tomcat) to run the Spring Boot application. It runs on port 8080. It is possible to change the port in Spring Boot.
SpringApplication
has a property webEnvironment
. It defaults to true if Tomcat is on the classpath but you can set it to false (programmatically or with spring.main.webEnvironment
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With