Is Spring Boot rebooting Spring’s Ease of Use?

Spring started out as a ‘small’ Dependency Injection container, which gave the Java world a whole new direction. Especially on the area of Java Enterprise, it offered innovations which allowed for faster and easier development of new Java applications.

In the last few years, Spring has grown enormously as a framework. The basics are still relatively simple, but due to the large number of different extensions it can be hard to start a new project. The people at Spring are familiar with this situation and are trying to remedy it with its newest addition: Spring Boot.

Liever Nederlands lezen? Ga naar JavaTopper.nl

Spring Roo? Spring Boot!

A few years ago, the people behind Spring released a tool to increase developer productivity: Spring Roo. It is a commandline tool which takes over the control of your project’s structure and makes sure dependencies are well managed and classes reside in their rightful places. Even though it was a promising tool and really did provide a way to quickly get started, it never really caught on. This is mainly due to the fact that Roo will take over your project and it is difficult to get it back out.

The newest addition to the Spring family promises to make the initial stages of a project more like child’s play. Spring Boot will allow you to kickstart a new project and have a running application in only a few minutes. How do they do it? Convention over Configuration, one of Spring’s foundations (although it might not feel that way when you’re working with it).

Spring Boot looks at your classpath and tries to find any available configuration. Based on the classes and annotations it finds, it will make some clever inductions about what you are trying to do. On the Spring website, they are showing an example where you have JPA and a PostgreSQL driver on your classpath. If that is the case, then Boot will configure a persistenceunit with a PostgreSQL datasource.

A Web-app in record time

Alright, enough talking. How does it work? Well, it really is simple: you make a new Maven project Pom and configure it as follows (instructions for Gradle):

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.0.0.RC3</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

To be honest, this is all it takes. You just created a project which contains an embedded Tomcat server, Spring MVC and Jackson. You can now start building your Web application:

@RestController
@EnableAutoConfiguration
public class HelloWorldController {

  @RequestMapping("/hello")
  public HashMap<String, Object> bla(@RequestParam(defaultValue = "World") String name) {
    HashMap<String, Object> result = new HashMap<String, Object>();

    result.put("Name", name);
    result.put("Message", "Hello " + name);

    return result;
  }

  public static void main(String[] args) {
    SpringApplication.run(HelloWorldController.class, args);
  }
}

This class is all you need to make a small Hello World web application. After compiling, you can just execute this class any way you like (e.g. from your IDE) and you will have a running Tomcat server which will give you the following result when you browse to http://localhost:8080/hello?name=JHK:

{
"Message": "Hello JHK",
"Name": "JHK"
}

Is Spring Boot going to make it?

I think so, yes. Seeing how Spring Boot is still an incubator-project, it already contains a ton of starters
and howtos to get you going. It configures some things automatically when needed, but everything can be overruled manually. Ideal if you want to try something, or just need to quickly setup some micro service. If you don’t agree with the defaults, you can define your own Webserver / Database driver / JPA provider / Template engine.

Just give it a try, take a look at the collection of sample projects or read the blogpost by Steve Perkins.

–JH

Posted in Java, Spring, Tools and tagged , , , .