Spring Boot

spring boot

The sessions at this year’s SpringOne conference covered a wide range of topics, everything from developer tooling to testing to reactive programming to core Spring. However, permeating the keynote and nearly all sessions was Spring Boot, Pivotal’s newest addition to the Spring ecosystem. In fact, one session was titled ‘Building Bootiful Web Applications’.

Simplifying the pom.xml

Spring Boot is aimed at simplifying Spring projects, especially the configuration. The simplification has several layers. The first layer is the pom.xml. To create a Spring Boot project (or migrate an existing one), you first need to specify the Spring Boot parent pom:

    org.springframework.boot
    spring-boot-starter-parent
    1.1.6

The parent pom has harmonized versions of various dependencies, including non-Spring libraries such as Hibernate. This allows the convention of omitting the <version> tag from all <dependency> tags. For example, to pull in Spring MVC use this:

    org.springframework.boot
    spring-boot-starter-web

Of course, you can override this harmonized version if desired by explicitly declaring a <version> tag (per Maven convention).

Additionally, as will be mentioned in the next section, web application projects can easily be packaged as jar and executed as a command line application hat uses an embedded Tomcat. Purely optional, and you could even use it strictly for development, but a nice option nonetheless.

Simplifying Spring Configuration

Spring 3.1 introduced annotation-driven configuration, which was great. Spring Boot takes that to the next level with a new annotation, @EnableAutoConfiguration.

At first glance this annotation seems to offer small conveniences, such as automatically scanning for settings in application.properties and application-profile.properties for all active profiles. But on closer glance it’s doing a lot more than that. This annotation uses classpath scanning to automatically provide library implementations based on the Spring dependencies you have specified in the pom.xml. For example, if your pom.xml declares ‘spring-boot-starter-web’ (Spring MVC), then at runtime an embedded server will be started. By default, this is a Tomcat server, but you can specify another (eg, Jetty) if preferred. Similarly, if you have specified ‘spring-boot-starter-jdbc’ you will get an embedded hsql server by default unless you have declared another database driver.

Synergies

Spring Boot offers a lot of synergies for the Spring modules. The SpringBootWebSecurityConfiguration provides basic common sense security configuration. You can customize it of course, but this lets you get a new project up and running quickly.

The ultimate synergy is the actuator. To use it, add this dependency to your project:

    org.springframework.boot
    spring-boot-starter-actuator

This creates several REST endpoints that expose all sorts of debug information. For example, the ‘/beans’ endpoint shows a complete listing of all Spring beans in the application. If you’re using Spring Security in your project, these endpoints are automatically locked down, allowing you to configure who can access them (eg, admin role).

The remote shell is an alternative to the actuator. Instead of exposing REST endpoints, the remote shell exposes an ssh port (default 2000) that you can log into and use commands to query the system. For example, the ‘beans’ command shows a complete listing of all Spring beans in the application.

Summary

I was able to convert an existing Spring project to Spring Boot in a matter of hours. If that’s too much of an investment, then I urge you to use Spring Boot for your next new project. Pivotal has created Spring Initializr, a WYSIWYG wizard for creating a custom sample web application that contains all the components you need:
http://start.spring.io/

Alternatively, the SpringSource Eclipse IDE allows the creation of a new project using these templates. Right-click on Package Explorer and select New > Import Spring Getting Started Content. This should help you get up and running with your next ‘Bootiful’ application!