spring

Spring @SpringBootApplication Annotation Example

Welcome readers, in this tutorial, we will explore one of the most important annotations of the spring framework.

1. Introduction

  • Spring Boot is a module that provides rapid application development feature to the spring framework including auto-configuration, standalone-code, and production-ready code
  • It creates applications that are packaged as jar and are directly started using embedded servlet container (such as Tomcat, Jetty or Undertow). Thus, no need to deploy the war files
  • It simplifies the maven configuration by providing the starter template and helps to resolve the dependency conflicts. It automatically identifies the required dependencies and imports them in the application
  • It helps in removing the boilerplate code, extra annotations, and xml configurations
  • It provides a powerful batch processing and manages the rest endpoints
  • It provides an efficient jpa-starter library to effectively connect the application with the relational databases

1.1 @SpringBootApplication annotation in Spring

  • @SpringBootApplication annotation was introduced in Spring Boot 1.2.0 and it enables the auto-configuration feature
  • This annotation encapsulates the working of three different annotations i.e.
    • @Configuration: Allows the developers to explicitly register the beans
    • @ComponentScan: Enables the component-scanning so that the controller class and other components will be automatically discovered and registered as beans in spring’s application context
    • @EnableAutoConfiguration: Enables the auto-configuration feature of spring boot
  • This annotation takes up the following optional parameters:
    • exclude: Excludes the list of classes from the auto-configuration
    • excludeNames: Excludes the list of fully qualified class names from the auto configuration
    • scanBasePackage: Provides the list of packages which must be applied for scanning
    • scanBasePackageClasses: Provides the list of classes in the other package which must be applied for scanning

Now, open the eclipse ide and let us see how to implement this annotation in the spring boot module.

2. Spring @SpringBootApplication Annotation Example

Here is a systematic guide for implementing this tutorial.

2.1 Tools Used

We are using Eclipse Kepler SR2, JDK 8, and Maven.

2.2 Project Structure

In case you are confused about where you should create the corresponding files or folder, let us review the project structure of the spring boot application.

Spring @SpringBootApplication Annotation - Application Structure
Fig. 1: Application Structure

2.3 Project Creation

This section will demonstrate how to create a Java-based Maven project with Eclipse. In Eclipse IDE, go to File -> New -> Maven Project.

Spring @SpringBootApplication Annotation - Maven Project
Fig. 2: Create a Maven Project

In the New Maven Project window, it will ask you to select a project location. By default, ‘Use default workspace location’ will be selected. Select the ‘Create a simple project (skip archetype selection)’ checkbox and just click on the next button to proceed.

Spring @SpringBootApplication Annotation - Project Details
Fig. 3: Project Details

It will ask you to ‘Enter the group and the artifact id for the project’. We will input the details as shown in the below image. The version number will be by default: 0.0.1-SNAPSHOT.

Spring @SpringBootApplication Annotation - Archetype Parameters
Fig. 4: Archetype Parameters

Click on Finish and the creation of a maven project is completed. If you observe, it has downloaded the maven dependencies and a pom.xml file will be created. It will have the following code:

pom.xml

<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.springboot</groupId>
	<artifactId>Myfirstspringbootapplication</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</project>

We can start adding the dependency that developers want like Spring Boot etc. Let us start building the application!

3. Creating a Spring Boot application

Below are the steps involved in developing the application.

3.1 Maven Dependency

Here, we specify the dependency for the Spring Boot. Maven will automatically resolve the other dependencies. The updated file will have the following code.

pom.xml

<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.springboot</groupId>
	<artifactId>Myfirstspringbootapplication</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>My first spring boot application example</name>
	<description>An example to explain the @SpringBootApplication annotation.</description>

	<!-- spring boot parent dependency jar -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
	</parent>
	<dependencies>
		<!-- spring boot jar -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
	</dependencies>
	<build>
		<finalName>Myfirstspringbootapplication</finalName>
	</build>
</project>

3.2 Implementation/Main Class

Let us write the implementation/main class involved in this application. This class is the entry point of the spring boot application containing the @SpringBootApplication annotation and the static main method.

Myapplication.java

package com.springboot;

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

// This annotation enables the auto-configuration feature of the spring boot module (i.e. java-based configuration and component scanning)
@SpringBootApplication

// The main class serves two purpose in a spring boot application: Configuration and bootstrapping.
public class Myapplication {

	public static void main(String[] args) {

		// The "run()" method returns the "ConfigurableApplicationContext" instance which can be further used by the spring application.
		SpringApplication.run(Myapplication.class, args);

	}
}

4. Run the Application

To execute the application, right click on the Myapplication.java class, Run As -> Java Application.

Spring @SpringBootApplication Annotation - Run the Application
Fig. 5: Run the Application

Developers can debug the example and see what happens after every step. Enjoy!

5. Conclusion

In this section, developers learned how to create a simple spring boot application. That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!

Developers can download the sample application as an Eclipse project in the Downloads section.

6. Download the Eclipse Project

This was an example of @SpringBootApplication annotation of the spring framework.

Download
You can download the full source code of this example here: Spring @SpringBootApplication Annotation Example

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button