The Spring Boot revolution

Spring is a popular framework for building web and enterprise applications.
It does not focus on one aspect of the development - like other projects - but has many projects that tries to answer the business needs (ie. Spring Integration, Spring Data, Spring MVC, etc.).

If you worked with Spring in the past, you noticed the complexity to configure your application each time you needed to create one, even if you had the option to use XML files, annotations or JavaConfig.
That's why the Spring team brought to us (in the version 4.0 of the Spring Framework) Spring Boot: to alleviate the complexity of configuration. Well, that's one of the reasons.

Spring Boot also lets you create applications easily and quickly with an embedded server (no need to deploy war files) - something you will surely need towards a Microservices architecture. But that architecture is a good subject for a new post.

Let's start with a very simple example using Maven.
You need to add this your pom file:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Your app needs to have the spring-boot-starter-parent  as a parent. There are more ways to start Spring-Boot, but let's stick with this one.

Now, all you need to do to run our example is to create a controller

package com.example.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class HelloController {


    @RequestMapping("/")
    @ResponseBody    
    public String home(){
        return "How I wish you were here";
    }


    public static void main(String[] args) throws Exception {
        SpringApplication.run(HelloController.class);
    }

}

As you can see we have some annotations worth mentioning. The @Controller annotation tells Spring this class will handle incoming web requests.
The second class-level annotation  @EnableAutoConfiguration tells Spring-Boot to "guess" how you want to configure your application based on the jar dependencies you have added.

Because we have added spring-boot-starter-web  as a dependency in our pom file,  and since this dependency comes already with Tomcat and Spring MVC embedded, the auto-configuration assumes we are developing a web application and setup Spring accordingly.

The @RequestMapping  provides routing information. It tells Spring to map the "/" to the home method (this is a Spring MVC configuration).

Finally we need to add to our main  method Spring Boot's SpringApplication class and call the run method.

SpringApplication will bootstrap our application, starting Spring which in turns will start the embedded Tomcat server.
We will add  HelloController.class  as a parameter to the run method- this is the way to tell Spring which is our primary component.

After that, all you need to do is run this Maven command on your command line (in the directory where your pom file resides).

$ mvn spring-boot:run

Now open your browser and go to localhost:8080 . You should see...

How I wish you were here

Pretty simple, isn't it? How long did it take to create this app ?

On our next instalments we will see how to build Spring RESTful web services and how to add iBatis (or as it is called today "MyBatis"), a persistence layer framework. Stay tuned !




Comments

Popular Posts