2015-08-04

Right now, Spring is the de-facto standard framework for Java-based development, especially in corporate environments. Usually when we talk about Spring, we’re talking about web development using Spring MVC. While Spring is still hugely popular, in recent years we’ve seen a number of lighter, simpler frameworks *ahem* spring up that can make simple web development much quicker and easier. So, is Spring on its way out?

While shiny new frameworks are exciting (I’ve been enjoying testing out some micro frameworks lately) – and can have a real effect on productivity for many projects – Spring still has an extensive existing user base, tons of library integrations and is a very powerful framework. Spring is extremely configurable and customizable with a lot of useful functionality like dependency injection, simplified data access, security and the aforementioned MVC framework for web development. And of course, sometimes we use Spring because it’s not always possible to be in complete control of the tools we use on the job.

Unfortunately, along with Spring’s flexibility comes a fair amount of complexity. Spring tends to need quite a bit of configuration just to get to a workable state (and this used to be stored primarily in *shudder* XML), so the task of starting a new Spring application from scratch (and then maintaining it) can seem daunting. Spring has made some strides over the years as far as ease of development, including adding annotations which replaced more-and-more XML config over time. For a while now, it’s even been possible to create a Spring application without any XML at all. But, even at a basic level, you’re probably going to want a web app with a templating framework for your HTML pages, a database for persistence and some security. With just these components – and likely more to configure – you may end up a couple days into a project with nothing to show for it except a shell of an application that hopefully starts without crashing.

This is where Spring Boot comes in. Spring Boot can take almost all of the hassle out of this normally arduous process. It essentially takes Spring and sprinkles in some opinions about how things should be done. With Spring Boot, there are a plethora of pre-configured components with sane defaults that can be added without breaking a sweat.

For example, using Gradle, we’ll start a project by adding the spring-boot plugin and a dependency on spring-boot-starter-web to our build config.



Then we’ll just add a one-liner main method to kick things off.



Then, … wait, that’s it? Pretty much! But let’s go ahead and add a controller to actually make something happen.



There, we’ve got ourselves a working REST application!

But I need all the things! If you want to use a fancy templating library like Velocity or Thymeleaf, just add a single dependency and Spring Boot will set it up with a sensible default configuration. How about persistence? Again, a single dependency will enhance your project, this time with a pre-configured HSQL implementation. Don’t want to use HSQL in production? Add a dependency on the database of your choice and a couple of application properties to define your data source. You can even automatically stand up Spring security with a default configuration offering simple password authentication.

Just a couple of change are required to add all of that.

The cherry on top is that running applications is also easier with Spring Boot. It will compile all your dependencies into one runnable jar, complete with an embedded tomcat servlet container. Of course, you can easily specify a different servlet container by adding it as a dependency, or you can just package your app into a war file for deployment in a more traditional manner.

Spring Boot gives you a super easy way to get started with some default settings, akin to many of the newer frameworks out there, but also affords you all the same configurability you would have in any other Spring project. Whether you’re doing something as simple as changing your template filename pattern from *.html to *.mustache, or as crazy as implementing a brand new security paradigm, Spring Boot will let you make the changes you want, and then intelligently fill in the gaps – but only where you haven’t specified your own configuration. This is what really makes Spring Boot a welcome upgrade. It can be as simple or as custom as you want it to be. It’s not always the right tool for the job, but it’s definitely worth consideration when planning new projects.

Show more