Spring Boot - Introduction

Why Spring boot

You must have heard about the Spring framework that is one of the great frameworks to develop the enterprise-grade application. Before Spring framework, these kinds of the application had been developed in JEE platform using the EJB and Java Application Servers like JBoss, Weblogic, WebSphere etc. Spring framework is the integration framework which gives us the facility to write the business solution using its core modules where spring framework manages your objects and their dependencies and you need to just focus on writing the business logic. 

Why did Spring framework come into existence?

J2EE Platform had been able to give the enterprise solutions since the beginning, J2EE provided the facility to handle the life cycle of the java objects with their required dependency, and developer only needs to focus on writing the business logic. This feature which is called dependency injection and this architectural pattern which is called Inversion of Control, somehow being was provided by the J2EE platform. But the J2EE platform has been complex in terms of implementation and there has been a significant running cost to keep the application running. Even if you want to use the DI feature, you were needed to run your project in java application server.

During this period of time, Rod Johnson released the framework with the publication of his book "Expert One-on_one J2EE Design and Development in Oct 2002. This framework was able to manage the java objects life cycle and their dependencies similar to J2EE and the project developed in this framework could run without J2EE server. That is why most of the enterprise solution developer started to love this framework and it becomes popular within a few years.

Why Spring Boot?

Spring framework becomes the large ecosystem to develop the solution, it has many other modules/projects under its umbrella to support the solutions being developed using this framework. Now, again the same story implies here. Spring framework provides so many features which its module and configuration. To write a simple hello work project in Spring MVC, you need add various dependencies, you need to write something in web.xml, you need to create another configuration file which is called dispatcher servlet config, you need load the application config file using the context listener and then, this project requires a java based server like tomcat or jetty.

If you are going to write the large application like ERP or CRM, it is worthiness. but if you want to write a small application like to accept the data from a contact-us form, if you have to do all the steps I explained before. This basic or foundation cost has always been there no matter what size of application you are going to build using spring framework.

To solve this, Spring team introduces another platform called Spring boot, which reduces the complexity and configuration in comparison to the Spring framework for small size projects, but it solves the purpose of the developer. it allows you to develop self-running and production-grade application without must configurations. It manages most of the configuration, libraries, dependencies, third-party libraries automatically so that you can get started within 5 minutes. 

 

Spring boot Hello World Example

Step 1: Create the maven project by writing you own maven pom file or download the pom file from https://start.spring.io/

Here is the pom.xml file content:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>Spring-Boot-Demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>Spring Boot Demo</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</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>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Step 2: Write the Application class(HelloApplication.java) which has the following code with main method.

package com.example.demo1;

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

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

 

Step 3: Write the simple Rest Controller(HelloController.java) code

package com.example.demo1;

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

@RestController
public class HelloController {
    @RequestMapping("/")
    public String greeting() {
        return "Hello from spring boot example";
    }
}

 

That's it.

You will see the following output in browser when you run the program.

Spring Boot Hello Example

Comments

Submitted by Williamawaws (not verified) on Thu, 10/28/2021 - 07:40

Permalink

Comment

I have been surfing on-line greater than 3 hours nowadays, yet I never discovered any fascinating article like yours. It is pretty value sufficient for me. In my view, if all site owners and bloggers made good content as you did, the web can be a lot more useful than ever before.