spring boot create a no-web application

1. Problem

Not all applications must be Spring web application (or web service). If you want to execute some code in the main method, but the underlying facilities need to start a Spring application to the desired setting, then use the Spring Boot SpringApplication characteristics can be easily achieved.

spring boot for the vast majority of web applications, but sometimes we just want to start by spring boot container, use some of its features, the single did not want to start a web service, how to do?

2. solve

SpringApplication depending on whether it needs a web application to change its ApplicationContext class, the first thing you need to do is to remove the servlet API dependent, if you can not do that, then you can explicitly call SpringApplication.setWebEnvironment(false)can be.

Examples are as follows:
Here Insert Picture Description

3. HTTP port random unassigned

Gets want to scan HTTP port (local port uses the operating system in case of conflict) can set an unusedserver.port=0

4. References

  1. Create a no-web application
Published 418 original articles · won praise 745 · Views 1.26 million +

Guess you like

Origin blog.csdn.net/u013467442/article/details/90757989