@SpringBootApplication annotation example in Spring Boot

The @SpringBootApplication annotation is a core annotation in the Spring Boot framework. It is typically used to mark the main class of a Spring Boot application. When you apply this annotation to a class, it does the following:

  1. Combines Multiple Annotations: It combines several other annotations into one. These include @Configuration, @EnableAutoConfiguration, and @ComponentScan.
  2. @Configuration: It indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.
  3. @EnableAutoConfiguration: It enables Spring Boot’s auto-configuration feature. Auto-configuration automatically configures your Spring application based on the libraries and dependencies you have in your classpath. This greatly reduces the need for manual configuration.
  4. @ComponentScan: It tells Spring to scan for other Spring components, such as other beans, configurations, and services, in the current package and its sub-packages. This ensures that Spring can find and register all the necessary components.

Here’s a simple example of how to use the @SpringBootApplication annotation:

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

@SpringBootApplication
public class MyApplication {

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

In this example, MyApplication is the main class of the Spring Boot application. The @SpringBootApplication annotation combines the three core annotations mentioned above, and when you run this class, it will start the Spring Boot application and trigger the auto-configuration and component scanning mechanisms.

By using @SpringBootApplication, you can quickly bootstrap a Spring Boot application with sensible defaults and start building your application with minimal configuration.

@SpringBootApplication annotation example in Spring Boot.

SpringMain.java

package springbootexample;

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

@SpringBootApplication
public class SpringMain {
	public static void main(final String[] args) {
		final ConfigurableApplicationContext configurableApplicationContext = SpringApplication
				.run(SpringMain.class, args);	
	}
}

TestContoller.java

package springbootexample;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
	@RequestMapping(value = "/rest")
	public String printHello(){	
		return "hello";
	}
}

The directory structure should look likes below.

@SpringBootApplication annotation example in Spring Boot

/SpringBootAnnotExamp/src/main/resources/application.properties

server.port = 9093

@SpringBootApplication annotation example in Spring Boot

Let’s modify the SpringMain.java. We will use @SpringBootConfiguration, @EnableAutoConfiguration, and @ComponentScan instead of @SpringBootApplication.

SpringMain.java

package springbootexample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
public class SpringMain {
	public static void main(final String[] args) {
		final ConfigurableApplicationContext configurableApplicationContext = SpringApplication
				.run(SpringMain.class, args);	
	}
}

Run this class and hit the rest URI on the browser.

Sure, here are five objective questions related to @SpringBootApplication in Spring Boot, along with four options for each question. The correct answers will be provided at the end.

Question 1: What is the purpose of the @SpringBootApplication annotation in a Spring Boot application?

A) It defines a REST endpoint.
B) It combines multiple annotations such as @Configuration, @EnableAutoConfiguration, and @ComponentScan.
C) It specifies the database connection details.
D) It is used to declare a controller class.

Question 2: Which of the following annotations is NOT combined by the @SpringBootApplication annotation?

A) @Configuration
B) @EnableAutoConfiguration
C) @RestController
D) @ComponentScan

Question 3: What does the @EnableAutoConfiguration annotation do when used in conjunction with @SpringBootApplication?

A) It disables Spring Boot’s auto-configuration.
B) It enables Spring Boot’s auto-configuration, which configures the application based on its classpath and dependencies.
C) It defines a custom configuration class for the application.
D) It scans for components in the current package and sub-packages.

Question 4: In a Spring Boot application, where should you typically place the main class annotated with @SpringBootApplication?

A) In a separate package from the rest of your application components.
B) It doesn’t matter where the main class is located.
C) In the root package (top-level package) of your application.
D) In the src/test directory.

Question 5: Which method is used to start a Spring Boot application with the @SpringBootApplication annotation?

A) run()
B) start()
C) initialize()
D) launch()

Answers:
1) B
2) C
3) B
4) C
5) A

That’s all about @SpringBootApplication annotation example in Spring Boot.

You may like.

Check @SpringBootApplication annotation docs.