For reference: This use case is documented in the Spring Boot Reference Guide:
Not all Spring applications have to be web applications (or web services). If you want to execute some code in a
mainmethod, but also bootstrap a Spring application to set up the infrastructure to use, then it’s easy with theSpringApplicationfeatures of Spring Boot. ASpringApplicationchanges itsApplicationContextclass depending on whether it thinks it needs a web application or not. The first thing you can do to help it is to just leave the servlet API dependencies off the classpath. If you can’t do that (e.g. you are running 2 applications from the same code base) then you can explicitly callSpringApplication.setWebEnvironment(false), or set theapplicationContextClassproperty (through the Java API or with external properties). Application code that you want to run as your business logic can be implemented as aCommandLineRunnerand dropped into the context as a@Beandefinition.
application.properties:
spring.main.web-environment=false #webEnvironment property