Booting Spring Webapp

Spring Boot is an excellent tool to bootstrap java application. Most of the references mention how to create a standalone java application, optionally with embedded web server (tomcat or jetty). But Spring Boot supports also creating web applications intended to run within servlet container.

Here is example of maven pom.xml file for Spring-Boot-enabled web application:

xml
 1    ...
 2    <packaging>war</packaging>
 3    <properties>
 4        <!-- If web.xml is not used -->
 5        <failOnMissingWebXml>false</failOnMissingWebXml>
 6        <!-- Set Spring-Boot Version -->
 7        <spring-boot.version>1.2.1.RELEASE</spring-boot.version>
 8    </properties>
 9    <dependencyManagement>
10        <dependencies>
11            <dependency>
12                <!-- Import dependency management from Spring Boot -->
13                <groupId>org.springframework.boot</groupId>
14                <artifactId>spring-boot-dependencies</artifactId>
15                <version>${spring-boot.version}</version>
16                <type>pom</type>
17                <scope>import</scope>
18            </dependency>
19        </dependencies>
20    </dependencyManagement>
21    <dependencies>
22        <dependency>
23            <groupId>org.springframework.boot</groupId>
24            <artifactId>spring-boot-starter-web</artifactId>
25            <exclusions>
26                <exclusion>
27                    <groupId>org.springframework.boot</groupId>
28                    <artifactId>spring-boot-starter-tomcat</artifactId>
29                </exclusion>
30            </exclusions>
31        </dependency>
32        <dependency>
33            <!-- Provided in Tomcat -->
34            <groupId>javax.servlet</groupId>
35            <artifactId>javax.servlet-api</artifactId>
36            <scope>provided</scope>
37        </dependency>
38        <dependency>
39            <!-- Provided in Tomcat -->
40            <groupId>javax.el</groupId>
41            <artifactId>javax.el-api</artifactId>
42            <version>2.2.4</version>
43            <scope>provided</scope>
44        </dependency>
45        <!-- Test dependencies -->
46        <dependency>
47            <groupId>org.springframework.boot</groupId>
48            <artifactId>spring-boot-starter-test</artifactId>
49            <scope>test</scope>
50        </dependency>
51        <dependency>
52            <groupId>org.glassfish.web</groupId>
53            <artifactId>javax.el</artifactId>
54            <version>2.2.4</version>
55            <scope>test</scope>
56        </dependency>        
57    </dependencies>
58    ...

You’ll need a SpringBootServletInitializer to configure web application:

java
 1@Configuration
 2@ComponentScan
 3@EnableAutoConfiguration
 4public class Application extends SpringBootServletInitializer {
 5
 6    private static Class<Application> applicationClass = Application.class;
 7
 8    public static void main(String... args) {
 9        SpringApplication.run(applicationClass, args);
10    }
11
12    @Override
13    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
14        return application.sources(applicationClass);
15    }
16}

Tuning Configuration

If you want to have a configuration file hierarchy, e.g.:

  1. Default properties (from classpath)
  2. Environment-specific server properties (from $CATALINA_BASE/conf)
  3. Environment-specific application properties (from $CATALINA_BASE/conf)

You may specify spring.config.location env-entries in web.xml. Spring Boot will read properties from JNDI.

xml
 1<?xml version="1.0" encoding="UTF-8"?>
 2<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
 3         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 4         version="3.0">
 5    <display-na>
 6    <env-entry>
 7        <env-entry-name>spring.config.location</env-entry-name>
 8        <env-entry-type>java.lang.String</env-entry-type>
 9        <env-entry-value>
10            classpath:/default.properties,
11            file:${catalina.base}/conf/common.properties,
12            file:${catalina.base}/conf/my-application.properties
13        </env-entry-value>
14    </env-entry>
15</web-app>

References

Konstantin Pavlov

Konstantin Pavlov

Software Engineer working with Java, Kotlin, Swift, and AI. Focusing on software architecture and building AI-infused apps. Passionate about testing and Open-Source projects.