Spring Boot @SpringBootApplication Auto Configuration

Spring Boot Auto Configuration

In this article, We will learn about the Auto Configuration feature of Spring Boot.

If you have not read my introduction article of Spring Boot, I would request you to take a look –  Spring Boot Tutorial.

Spring boot framework is very easy to use and has a lot of features. Auto Configuration is one of the most relevant feature of Spring Boot Application.

Let’s learn about this feature of Spring Boot by creating one simple class with one dependency :

Create Spring boot application with the main class

  1. Create one Maven project in eclipse IDE . You can refer this : Spring Boot Project using Maven.
  2. Add the basic dependency spring-boot-starter-web in your project’s pom.xml.
  3. Create the main application class i.e. the launch class.

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 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.1.8.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>springBootApp</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springBootApp</name>
	<description>Demo project for 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-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

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

</project>

SpringBootAppApplication.java

package com.example.demo;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class SpringBootAppApplication
{
    public static void main(String[] args)
    {
        ApplicationContext context = 
        SpringApplication.run(SpringBootAppApplication.class, args);
    }
}

Explanation of SpringBootAppApplication.java :

We have created the main class of our Spring Boot Application. This class is also known as the launch class of the Spring Boot as it is used to bootstrap the application :

This class has responsibilities as :

  • To create an instance of ApplicationContext of Spring.
  • To enable the functionality of Spring Boot Application to accept the command-line arguments.
  • To load all the Spring beans as provided in the configurations.
  • Any other operations can also be written in this class as per your requirement.

Understanding the @SpringBootApplication Annotation

You can refer this tutorial to get understanding about all the annotations in detail: Spring Boot Annotations.

@SpringBootApplication is same as applying below three annotations :

  1. @SpringBootConfiguration
  2. @EnableAutoConfiguration
  3. @ComponentScan

@SpringBootConfiguration

  • @SpringBootConfiguration is a new annotation introduced in version 2 of Spring boot.
  • In previous versions, we were using @Configuration annotation.
  • Both the annotations are same but @SpringBootConfiguration has the additional feature that it can automatically locates the configurations.
  • This annotation is simply used to define any class as configuration class.

@EnableAutoConfiguration

  • It is used to enable the feature of auto-configuration of the beans which are likely needed.
  • Auto-configuration classes are usually applied on the basis of your defined beans and your application’s classpath.
  • Auto-configuration classes are always loaded after the registration of user-defined beans.

@ComponentScan

This annotation is basically used to enable the Component scanning.

//Specifies the path for scanning of components

@ComponentScan(“com.techblogstation.controllers”)

Running the Application to see logs

Run the application :

Spring Boot Auto Configuration

Logs :

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

2019-09-26 19:50:59.495 INFO 5620 --- [ main] c.example.demo.SpringBootAppApplication : Starting SpringBootAppApplication on DESKTOP-VDV469E with PID 5620 (E:\workspace\springBootApp\target\classes started by Ashish in E:\workspace\springBootApp)
2019-09-26 19:50:59.498 INFO 5620 --- [ main] c.example.demo.SpringBootAppApplication : No active profile set, falling back to default profiles: default
2019-09-26 19:51:00.589 INFO 5620 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-09-26 19:51:00.653 INFO 5620 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-09-26 19:51:00.657 INFO 5620 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-09-26 19:51:00.777 INFO 5620 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-09-26 19:51:00.777 INFO 5620 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1255 ms
2019-09-26 19:51:01.040 INFO 5620 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-26 19:51:01.286 INFO 5620 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-26 19:51:01.299 INFO 5620 --- [ main] c.example.demo.SpringBootAppApplication : Started SpringBootAppApplication in 2.38 seconds (JVM running for 4.097)

Conclusion

That’s all folks! In this article,  we have learnt about the Auto Configuration in Spring Boot and also the basic example of Spring Boot.

Leave a Reply

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