Version Info

Spring Boot 1.3.2
JDK 1.7

Overview - Why Spring boot ?

Did you ever felt like :

"Ok I want to build spring application from scratch but .. Ahhhh!! too many dependencies to be added in pom.xml for a simple spring application to be created, such a pain."
"Oh no !! what else I have missed in this spring based web application that request is not served."
"Okay, so to use mysql using spring jbdc, what all dependencies are to be added... Hummm!! lets check google."


Spring boot solves these problems completely. Lets try out an example before getting to know the features, then you get to understand the real power of spring boot.

<!-- [1] -->  
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mnkartik.springboot</groupId>
  <artifactId>sboot1</artifactId>
  <version>1.0</version>
  <packaging>jar</packaging>

From Listing-1 this defines the configuration of the artifact sboot1 which is of jar type.

This is usual process of defining an artifact configuration in maven ecosystem where the present artifact is `sboot1`. This is very similar to any other maven based artifact configuration. `..`
<!-- [2] -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.RELEASE</version>
  </parent>

From Listing-2, this configuration is very similar to the parent child maven configuration, which is used to import the dependencies from the parent pom without specifically mentioning the version. In Spring boot, its called as starter POM.

Starter POM

By the name you can understand that a starter pom consists of pre-defined set of dependencies, required for a specific type of spring based application to kick off, which can be of spring jpa or web or standalone or batch or any other type of spring application. For a specific type of starter pom, required dependencies are included so that you dont need to find those and configure each and every one manually.

Lets dig deep into the For example check out the dependencies in the `spring-boot-starter-parent` [pom.xml](https://github.com/spring-projects/spring-boot/blob/master/spring-boot-starters/spring-boot-starter-parent/pom.xml). Go to the dependency `spring-boot-dependencies` which is a parent of the `spring-boot-starter-parent` [pom.xml](https://github.com/spring-projects/spring-boot/blob/master/spring-boot-dependencies/pom.xml). In this file you will see lots of pre-defined dependencies under the `..` tag, which is what is available by default when you define the above parent tag. If you see the list in details you will get to know that all the required dependencies sufficient enough for most of the spring based standalone projects. If you want to define a specific version of the spring dependency then, you could override the same with specific version which is a usual maven behaviour. Apart from the ones which are configured as defaults, if any more are required which are not present in the starter, mention the same in the present pom.xml which is also a usual way of doing things in maven.

Standard pre-defined starter pom’s

Below table lists out the default spring boot application starters

spring-boot-starter The core Spring Boot starter, including auto-configuration support, logging and YAML
spring-boot-starter-jdbc Support for JDBC databases.
spring-boot-starter-security Support for spring-security
spring-boot-starter-logging Import Spring Boot’s default logging framework (Logback).

For more information Check out each of the starters pom.xml, there is is huge list of starter pom’s present, so as to get clear picture of how and what dependencies are imported by default.

<!-- [3] -->

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>      
    </dependency>
  </dependencies>

From Listing-3, this starter dependency is sufficient enough to start of the standalone application. Check out the dependencies whcih are mentioned the above spring-boot-starter pom.xml for more info.

   
  <build>
    <plugins>
      
      <!-- [4] -->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>  

From Listing-4, this is configuration lets the artifact to be bundled as a executable artifact ( here executable jar ), so that you can use java -jar command to run the end package artifact.

Try out: Check out by commenting this section and you will notice that the jar is not executable any more and will get error.

@EnableAutoConfiguration
@Component
@ComponentScan
public class Main {
  // ..
}

@EnableAutoConfiguration

This annotation tells spring boot to perform required configuration based on the starter dependencies configured. For now we have configured the spring-boot-starter which defaults to standalone application.

ConfigurableApplicationContext ctx = SpringApplication.run(Main.class, args);

Main mainObj = ctx.getBean(Main.class);

mainObj.init();

SpringApplication.run() takes the class which loads the spring configuration ( here we have used Main.java as the spring, java based configuration ). Using the ConfigurableApplicationContext you get the bean reference as shown above, where reference to the Main object is returned.


Run application

mvn package

Before running the application, package first by issuing the above command

mvn spring-boot:run 

This command uses the maven tag to run the spring application. Further we will see this same command is used to run the web application too in later topics. Unlike configuring the maven runnable jar plugins specifically, just the starter build plugin added in the listing reduces lot of time.

or

java -jar target\sboot1-1.0.jar

Even this works, since as per above description, the application is packaged as runnable jar or executable jar and hence can be run in the above manner too. This is not a usual jar, but it contains dependencies too inside single jar. Also called as uber jar.

Go to the target folder and run the command `jar -xfv sboot1-1.0.jar` which lists out all spring related jars inclusive of the present package classes. Hence this is called the uber jar or fat jar.


Output

Below is the output when the above command is run.( Some of the logs are not displayed for brevity). The output shows that the spring boot runs.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.0.RELEASE)

...
...

inside init method
Application exited

..
..


Features

Now that we have seen the example, below are the features of using spring-boot:

  • faster kick off of the spring based projects by reducing the time significantly to mention each and every spring dependency in pom.xml, by using spring-boot-starter-xxxxx dependencies. Later you are going to see how fast web applicatons can be developed.
  • provide features like embedded servers, security, metrics, health checks, externalized configuration and many more.
  • easy support the distributed application development

..any many more which we will see one by one in upcoming topics


Download Source Code Browse Source Code