@SpringBootApplication annotation in Spring Boot





Hey guys in this post, we will discuss about @SpringBootApplication annotation in detail. To understand this annotation, first create a spring boot project.

Read More:

Create spring boot project


There are many different ways to create a spring boot application, you can follow the below articles to create one –

>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA

Add maven dependencies


Open pom.xml and add the following dependencies –

<?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 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.5.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>in.bushansirgur</groupId>
	<artifactId>springbootdemo</artifactId>
	<version>1.0.0</version>
	<name>springbootdemo</name>
	<description>Spring boot demo</description>
	<properties>
		<java.version>11</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>

Open the main class


Open the base class that contains main(), the class should look like the one below –

package in.bushansirgur.springbootdemo;

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

@SpringBootApplication
public class SpringbootdemoApplication {

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

}
  • When we create a spring boot application, it also creates a base class that contains the main(), this is the starting point to the spring boot application
  • This class is annotated with @SpringBootApplication, which represents that spring boot application
  • This annotation intern uses multiple annotations
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {}

Important annotations that we need to understand here is @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan. Let’s discuss these annotations one by one.

@SpringBootConfiguration


  • This annotation also uses one more annotation internally which is @Configuration annotation
  • If you already worked in Spring and Spring core then probably you already heard about this annotation
  • @Configuration is an analog for xml file. Such classes are sources of bean definitions by defining methods with the @Bean annotation.

@Configuration is:

  • not required, if you already pass the annotated class in the sources parameter when calling then SpringApplication.run() method;
  • required, when you don’t pass the annotated class explicitly, but it’s in the package that’s specified in the @ComponentScan annotation of your main configuration class.




For readability, classes that are even explicitly passed as sources may anyway be annotated with @Configuration – just to show the intentions more clearly.

Our current class is not really source of bean definitions, because it doesn’t have any, but if you had @Bean annotated methods, Spring would see them.

@ComponentScan


This annotation is clear that, it will scan all the components in our application.

  • The classes that are annotated with @Component, @Service, @Repository, @Controller these are spring components
  • Spring will scan for all these classes under the base package in.bushansirgur.springbootdemo and add it to the spring container
  • If these classes are present in the different package then we need to explicitly specify the package names inside @ComponentScan

@EnableAutoConfiguration


This annotation also clears that, it enables auto configure the application based on the dependencies that are added to the classpath.

If you remember, we have added the Web dependency for the above example, this one dependency downloads all the dependencies that are required to build web app, REST API, Spring MVC and uses the embedded tomcat.

02

When we run the application,

  • If Spring finds spring-webmvc dependency inside the classpath then it will configure the DispatcherServlet
  • If Spring finds jackson-databind then it will automatically convert Java object to JSON and JSON to Java object
  • If Spring finds log4j then it will configure the Logging framework
  • If Spring finds tomcat-embed-core then will start the tomcat server on port localhost:8080

Let’s prove this

Run the app


Run the application using the below maven command –

mvn spring-boot:run

Check the console logs

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

2021-08-26 16:43:56.425  INFO 20448 --- [           main] i.b.s.SpringbootdemoApplication          : Starting SpringbootdemoApplication using Java 11.0.3 on LTIN319745 with PID 20448 (C:\Users\910875\Documents\workspace-sts-3.9.10.RELEASE\springbootdemo\target\classes started by 910875 in C:\Users\910875\Documents\workspace-sts-3.9.10.RELEASE\springbootdemo)
2021-08-26 16:43:56.427  INFO 20448 --- [           main] i.b.s.SpringbootdemoApplication          : No active profile set, falling back to default profiles: default
2021-08-26 16:43:57.172  INFO 20448 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-08-26 16:43:57.180  INFO 20448 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-08-26 16:43:57.180  INFO 20448 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.52]
2021-08-26 16:43:57.249  INFO 20448 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-08-26 16:43:57.249  INFO 20448 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 775 ms
2021-08-26 16:43:57.552  INFO 20448 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-08-26 16:43:57.564  INFO 20448 --- [           main] i.b.s.SpringbootdemoApplication          : Started SpringbootdemoApplication in 1.445 seconds (JVM running for 2.069)

You can see the message Tomcat started on port(s):8080. We have not configured any server details, but the application starts tomcat server on port 8080. This is all about @EnableAutoConfigure


That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.



About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

View all posts

Leave a Reply

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