Spring Boot

This post describes basic idea about spring boot, why to use it and a simple example about it.

Spring is very popular java framework for building java based web application as well enterprise application. It is popular because of following strong features it provides:

  • Spring dependency injection – This is the main feature to which spring was introduced to think something in object oriented way so that java code can be loose coupled.
  • Spring Web – A powerful module to create web application in MVC pattern and get rid of many configuration which we do in Servlet based application.
  •  Provides simplify integration with other frameworks like hibernate, JPA etc.
  • Spring Data – Simplifies data access from No SQL and and relational DB.
  • Spring Integration – powerful module to make integration connectors with third party system and make a complete ESB messaging system to perform integration.

There are many other modules and features which is provided by spring. For more information about spring projects https://spring.io/projects.

To make spring based application there is a lot of configurations (click here to check simple program with spring mvc) required like

  • Add many dependencies in class path
  • Create xml files to configure spring environment for initiating beans, database properties, hibernate properties if we are using hibernate etc.
  • Configure data source to perform database operation, view resolvers to resolve views etc.

There are lot of configuration which we have to do if want to create spring based application and this is too much work and take so much time to get up spring based application.

Same work we have to do again and again if we want to create another spring based application.

Don’t you think that there should be some automated way to configure spring configurations and get spring based application up and running quickly.

Here comes spring boot in picture. It provides you the automated way to configure spring configuration automatically and get ready spring based application quickly.

Following example will explain how spring boot make life easy for developers.

Let’s create a maven project and add following dependencies in pom.xml file.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>springboot</groupId>
	<artifactId>application</artifactId>
	<packaging>jar</packaging>
	<version>0.0.1</version>
	<name>Application Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
	</parent>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
	<build>
		<finalName>application</finalName>
	</build>
</project>

As you can see that this is much smaller pom.xml file to configure spring web module and there is no worry about the version as well.

It will add following dependencies in the class path:

maven_dep

Here, it also add tomcat-embed server in the class path so there is no need to add external server to run the application.

Now, create Controller class having one request mapping in it as below:

package com.threadminiions.controllers;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GeneralController {

	@RequestMapping("/value")
	public String getValue()
	{
		return "Welcome to spring boot.";
	}
}

Now, create a configuration class to initiate spring application as below:

package com.threadminions;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication //Combination of @Configuration and @ComponentScan
public class SpringBootConfig {

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

This is all about configuration. Now its time to run what we created and test whether spring web based application is ready or not.

Run it as java application and it will show following output:

sprin_boot

As you can see in output that tomcat is started at 8080 port so hit http://localhost:8080

url in order to access request mapping and you will see the following output in your browser:

spring_boot

So As you can see how easy spring boot makes for developers to make spring based application with minimum configurations and in very less time.

This is all about the basic idea of spring boot.

3 thoughts on “Spring Boot

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s