Chapter 1. Bootstarting Spring
This chapter covers
- How Spring Boot simplifies Spring application development
- The essential features of Spring Boot
- Setting up a Spring Boot workspace
The Spring Framework has been around for over a decade and has found a place as the de facto standard framework for developing Java applications. With such a long and storied history, some might think that Spring has settled, resting on its laurels, and is not doing anything new or exciting. Some might even say that Spring is legacy and that itâs time to look elsewhere for innovation.
Some would be wrong.
There are many exciting new things taking place in the Spring ecosystem, including work in the areas of cloud computing, big data, schema-less data persistence, reactive programming, and client-side application development.
Perhaps the most exciting, most head-turning, most game-changing new thing to come to Spring in the past year or so is Spring Boot. Spring Boot offers a new paradigm for developing Spring applications with minimal friction. With Spring Boot, youâll be able to develop Spring applications with more agility and be able to focus on addressing your applicationâs functionality needs with minimal (or possibly no) thought of configuring Spring itself. In fact, one of the main things that Spring Boot does is to get Spring out of your way so you can get stuff done.
Throughout the chapters in this book, weâll explore various facets of Spring Boot development. But first, letâs take a high-level look at what Spring Boot has to offer.
1.1. Spring rebooted
Spring started as a lightweight alternative to Java Enterprise Edition (JEE, or J2EE as it was known at the time). Rather than develop components as heavyweight Enterprise JavaBeans (EJBs), Spring offered a simpler approach to enterprise Java development, utilizing dependency injection and aspect-oriented programming to achieve the capabilities of EJB with plain old Java objects (POJOs).
But while Spring was lightweight in terms of component code, it was heavyweight in terms of configuration. Initially, Spring was configured with XML (and lots of it). Spring 2.5 introduced annotation-based component-scanning, which eliminated a great deal of explicit XML configuration for an applicationâs own components. And Spring 3.0 introduced a Java-based configuration as a type-safe and refactorable option to XML.
Even so, there was no escape from configuration. Enabling certain Spring features such as transaction management and Spring MVC required explicit configuration, either in XML or Java. Enabling third-party library features such as Thymeleaf-based web views required explicit configuration. Configuring servlets and filters (such as Springâs DispatcherServlet) required explicit configuration in web.xml or in a servlet initializer. Component-scanning reduced configuration and Java configuration made it less awkward, but Spring still required a lot of configuration.
All of that configuration represents development friction. Any time spent writing configuration is time spent not writing application logic. The mental shift required to think about configuring a Spring feature distracts from solving the business problem. Like any framework, Spring does a lot for you, but it demands that you do a lot for it in return.
Moreover, project dependency management is a thankless task. Deciding what libraries need to be part of the project build is tricky enough. But itâs even more challenging to know which versions of those libraries will play well with others.
As important as it is, dependency management is another form of friction. When youâre adding dependencies to your build, youâre not writing application code. Any incompatibilities that come from selecting the wrong versions of those dependencies can be a real productivity killer.
Spring Boot has changed all of that.
1.1.1. Taking a fresh look at Spring
Suppose youâre given the task of developing a very simple Hello World web application with Spring. What would you need to do? I can think of a handful of things youâd need at a bare minimum:
- A project structure, complete with a Maven or Gradle build file including required dependencies. At the very least, youâll need Spring MVC and the Servlet API expressed as dependencies.
- A web.xml file (or a WebApplicationInitializer implementation) that declares Springâs DispatcherServlet.
- A Spring configuration that enables Spring MVC.
- A controller class that will respond to HTTP requests with âHello Worldâ.
- A web application server, such as Tomcat, to deploy the application to.
Whatâs most striking about this list is that only one item is specific to developing the Hello World functionality: the controller. The rest of it is generic boilerplate that youâd need for any web application developed with Spring. But if all Spring web applications need it, why should you have to provide it?
Suppose for a moment that the controller is all you need. As it turns out, the Groovy-based controller class shown in listing 1.1 is a complete (even if simple) Spring application.
Listing 1.1. A complete Groovy-based Spring application
@RestController class HelloController { @RequestMapping("/") def hello() { return "Hello World" } }
Thereâs no configuration. No web.xml. No build specification. Not even an application server. This is the entire application. Spring Boot will handle the logistics of executing the application. You only need to bring the application code.
Assuming that you have Spring Bootâs command-line interface (CLI) installed, you can run HelloController at the command line like this:
$ spring run HelloController.groovy
You may have also noticed that it wasnât even necessary to compile the code. The Spring Boot CLI was able to run it from its uncompiled form.
I chose to write this example controller in Groovy because the simplicity of the Groovy language presents well alongside the simplicity of Spring Boot. But Spring Boot doesnât require that you use Groovy. In fact, much of the code weâll write in this book will be in Java. But thereâll be some Groovy here and there, where appropriate.
Feel free to look ahead to section 1.21 to see how to install the Spring Boot CLI, so that you can try out this little web application. But for now, weâll look at the key pieces of Spring Boot to see how it changes Spring application development.
1.1.2. Examining Spring Boot essentials
Spring Boot brings a great deal of magic to Spring application development. But there are four core tricks that it performs:
- Automatic configurationâ Spring Boot can automatically provide configuration for application functionality common to many Spring applications.
- Starter dependenciesâ You tell Spring Boot what kind of functionality you need, and it will ensure that the libraries needed are added to the build.
- The command-line interfaceâ This optional feature of Spring Boot lets you write complete applications with just application code, but no need for a traditional project build.
- The Actuatorâ Gives you insight into whatâs going on inside of a running Spring Boot application.
Each of these features serves to simplify Spring application development in its own way. Weâll look at how to employ them to their fullest throughout this book. But for now, letâs take a quick look at what each offers.
Auto-configuration
In any given Spring applicationâs source code, youâll find either Java configuration or XML configuration (or both) that enables certain supporting features and functionality for the application. For example, if youâve ever written an application that accesses a relational database with JDBC, youâve probably configured Springâs JdbcTemplate as a bean in the Spring application context. Iâll bet the configuration looked a lot like this:
@Bean public JdbcTemplate jdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); }
This very simple bean declaration creates an instance of JdbcTemplate, injecting it with its one dependency, a DataSource. Of course, that means that youâll also need to configure a DataSource bean so that the dependency will be met. To complete this configuration scenario, suppose that you were to configure an embedded H2 database as the DataSource bea...