How setup Spring Boot application using Spring Initializr in 5 min?

In this artilce we are creating our first Spring Boot application using Spring Initializr.

Technology Used:

Java 8
Maven 3
Spring Boot 2.3.2

Step 1: Create New Project

We are using https://start.spring.io/ for creating new Spring Boot application. Spring Initializr provides defualt configuration for a quick start of the application. In our case we are using Maven 3, Java 8, Spring Boot 2.3.2.  Maven is one of the most popular build automation tool. In our case GroupId is com.codesakha and ArtifactId is SpringBootExample. GroupId presents package name and ArtifactId represents project name.

Step 2: pom.xml

After click on GENERATE button three main files created pom.xml, SpringBootExampleApplication.java, ServletInitializer.java. pom.xml file contains dependencies, plugins, version of Java, artifactid, groupid and many more. If you concentrate on pom.xml then you get embedded tomcat also, this is one of the advantage of Spring Boot.

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.codesakha</groupId>
	<artifactId>SpringBootExample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>SpringBootExample</name>
	<description>Hello World in Spring Boot</description>

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

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
                                        <artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

Step 3: void main()

We don’t need to disuss anything about main function here. SpringApplication provides a way that start Spring application from main() method. SpringApplication.run is a static method as you can see.

package com.codesakha.SpringBootExample;

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

@SpringBootApplication
public class SpringBootExampleApplication {

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

}

Step 4: SpringApplicationBuilder

It enables auto-configuration and component scanning.

package com.codesakha.SpringBootExample;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(SpringBootExampleApplication.class);
	}

}

Step 5: mvn spring-boot:run

Now time came when we can install our project using mvn clean install. It will download all dependencies related to project. We can import this project in eclipse using mvn eclipse:eclipse. The last steps is come when we can run our project using mvn spring-boot:run. After run project console looks like this.

Please feel free to write any suggestion related to this article.

Leave a Reply

Your email address will not be published. Required fields are marked *