Spring Boot Non-web Application Configuration

Spring boot applications are not limited to creating REST APIs or cloud services. There are many more non-web application types that Spring boot can help build. Learn to use WebApplicationType configuration to disable the web application nature of a Spring boot application.

1. Introduction

One of the main advantages of Spring Boot is the quick development time. Spring Boot has an embedded Web Server which is Tomcat by default and can be switched to Jetty or UnderTow.

There are many use cases like simple console applications in which we may not need the Web Server. For example, thick-client UI applications, job scheduler apps, batch processing applications, serverless applications, etc. In such cases, we may need to configure the Spring Boot app to not use the embedded Web Server.

Generally, the starting point of such non-web applications is a CommandLineRunner implementation class.

2. Exclude Starter Web Dependency

The simplest way to exclude the web server environment is to not include spring-boot-starter-web dependency in our application.

But it may happen that some other dependency has transitive dependencies on Tomcat or any other server. In such cases, Spring Boot will auto-detect the server from the classpath. So, we will have to configure our application to disable it.

3. Using Java Configuration

We can disable the embedded web server in our application using org.springframework.boot.builder.SpringApplicationBuilder by specifying the org.springframework.boot.WebApplicationType.

@SpringBootApplication
public class NonWebServerApplication {

	public static void main(String[] args) {
	
		new SpringApplicationBuilder(NonWebServerApplication.class)
			.web(WebApplicationType.NONE)
			.run(args);
	}
}

This configuration specifies that the application should not run as a web application and should not start an embedded web server.

We can also disable the embedded server for specific profiles. For example, if we want to disable it specifically for 'prod‘ profile, we can pass the spring.profiles.active parameter as Java startup argument, system properties or even configure in a properties file.

@SpringBootApplication
public class NonWebServerApplication {

	public static void main(String[] args) throws IOException {

		Resource resource = new ClassPathResource("application.properties");
		Properties props = PropertiesLoaderUtils.loadProperties(resource);
		String activeProfile = props.getProperty("spring.profiles.active");    //We can read from runtime arguments as well

		SpringApplicationBuilder builder = new SpringApplicationBuilder(NonWebServerApplication.class);
		if ("prod".equalsIgnoreCase(activeProfile)) {
			builder.web(WebApplicationType.NONE);
		}
		builder.run(args);
	}

}

Here we first get the active profile set and disable the embedded server only if it’s production.

4. Using Properties Configuration

We can do the same configuration in the application properties file. Its advantage over Java configuration is that we can disable web environments for specific profiles/environments in a clean and easier manner.

Spring Boot allows grouping the configuration parameters for different environments into the different application.properties configuration files. Spring Boot will automatically pick up the right configuration file depending on the activated profile and load the configuration properties from that file. We can activate a profile using various methods described in this Spring Profiles article.

If we want to disable the embedded web server only for “prod” and keep it enabled for other profiles, we can set the following in the application-prod.properties.

spring.main.web-application-type=none

4. Conclusion

This article explored various ways to configure a Spring Boot App without a Web server and disable the web application-specific features from the application.

Happy Learning!!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.