background

In my previous SpringBoot development projects, either internal Tomcat was used as an executable JAR or external Tomcat was published as a WAR. I have not encountered a scenario where only the SpringBoot framework was used, but no Web was used.

One was developed using SpringBoot technology, and Netty did not need the built-in Tomcat and its Web capabilities to provide Web services.

How do you strip out Tomcat and use SpringBoot and Bean dependency management? The framework already provides this capability. There are two ways to turn a SpringBoot project into a non-Web application from Tomcat.

Preparations: Remove the Web dependency configuration

Tomcat is no longer required for the project, so the relevant JAR packages need to be removed from pom.xml.

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <! > > <groupId> org.springFramework. boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>Copy the code

Or simply delete the spring-boot-starter-Web dependency.

Method 1: The main method specifies non-Web applications

In the SpringBoot boot class, use SpringApplicationBuilder to create non-web applications.

new SpringApplicationBuilder(MyApplication.class)
	    .web(WebApplicationType.NONE)
	    .run(args);
Copy the code

Method 2: Configure specified non-Web applications

Leave the boot class unchanged and add the following configuration to the SpringBoot configuration file:

spring:
  main:
    web-application-type: none
Copy the code

Delete the useless server.tomcat configuration.

Non-web application startup logs

Regular built-in Tomcat startup applications print Tomcat version information and startup port:

Initializing ProtocolHandler [" HTTP-niO-XXX "] Starting service [Tomcat] Starting Servlet Engine: [Apache Tomcat/9.0.21]Copy the code

After the first two operations, the application is a normal Java project.

[restartedMain] xxx.myAppliction: Started MyAppliction in 71.436 seconds (JVM running for 76.232)Copy the code

The revelation of

When I first received this requirement, I felt that it would be too much trouble to implement this feature, so I had an instinctive resistance.

It is surprisingly easy to find SpringBoot Web services for RestController, a servlet-based Web service, in Netty.

To quote a passage from “Cognitive Awakening” :

The real difficulties are always smaller than they seem. The root cause of procrastination, struggle, fear, and fear is often not how difficult the task itself is, but how vague it becomes inside. As soon as the blurred boundary is extended, the subconscious mind comes into play. Even simple little things are difficult for the subconscious mind to solve.

Some people say: Life is full of “chicken soup”, I have become immune.

However, we still need to keep the important truth in our eyes, hearts and minds.

Because, the truth is understood, but people are really easy to succumb to short-term temptation!