Booting Spring Webapp


Feb 19, 2015 2 minutes read

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:

    ...
    <packaging>war</packaging>
    <properties>
        <!-- If web.xml is not used -->
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <!-- Set Spring-Boot Version -->
        <spring-boot.version>1.2.1.RELEASE</spring-boot.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <!-- Provided in Tomcat -->
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <!-- Provided in Tomcat -->
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>2.2.4</version>
            <scope>provided</scope>
        </dependency>
        <!-- Test dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.el</artifactId>
            <version>2.2.4</version>
            <scope>test</scope>
        </dependency>        
    </dependencies>
    ...

You’ll need a SpringBootServletInitializer to configure web application:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {

    private static Class<Application> applicationClass = Application.class;

    public static void main(String... args) {
        SpringApplication.run(applicationClass, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(applicationClass);
    }
}

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 version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <display-na>
    <env-entry>
        <env-entry-name>spring.config.location</env-entry-name>
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>
            classpath:/default.properties,
            file:${catalina.base}/conf/common.properties,
            file:${catalina.base}/conf/my-application.properties
        </env-entry-value>
    </env-entry>
</web-app>

References

See Also

Webapp Inter-Communication in Tomcat

Sometimes you need to access services deployed in one web application from another web application within same application server. For Tomcat there is not so many options to do it. In this article we’ll discuss you how to use JMX for communication between web applications.

Conditional Java Configurations in Spring Framework

Spring Framework offers very flexible means for binding application components. Externalizable properties, composite configuration, nested application contexts and profiles.

Sometimes, it is necessary to control whether particular beans or @Configuration will be loaded or not. Spring Framework v.4.1.x does not provide that feature out of the box. But, hopefully, Spring allows conditional bean initialization (see @Profile implementation and @Configurable). So, I created the annotation @Enabled which allows me to control bean instantiation via properties.

@Enabled indicates that a component is eligible for registration when evaluated expression is true. This annotation should be used in conjunction with Configuration and Bean annotations.

JAX-WS with Custom SSLSocketFactory

It’s very easy to configure custom SSLSocketFactory for JAX-WS web-service: just specify custom property referring to SSLSocketFactory bean. But there is a nuance…

logo   Never miss a story, subscribe to our newsletter