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:
- Combines Multiple Annotations: It combines several other annotations into one. These include
@Configuration
,@EnableAutoConfiguration
, and@ComponentScan
. - @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. - @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.
- @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.
/SpringBootAnnotExamp/src/main/resources/application.properties
server.port = 9093
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.
- @RestController and @Controller annotation example in Spring Boot.
- @RequestMapping annotation example In Spring Boot.
- @RequestBody and @ResponseBody annotation example in Spring Boot.
- @PathVariable and @RequestParam annotations in Spring Boot.
- @RequestHeader annotation example by using Spring Boot.
- @Component, @Controller, @Service and @Repository annotations example using Spring Boot.
- @Configuration annotation example using spring boot.
- @ComponentScan example in spring boot.
- Content negotiation example using Spring Boot.
- Filter example in Spring Boot.
- Spring Boot interceptor example.
- Spring restful web services example using spring boot.
- Deploy multiple war files in JBoss to different port.
- Spring boot datasource configuration using tomcat.
- Jboss 7 EPA datasource configuration using oracle and spring boot.
- @ControllerAdvice Global Error Handling Example in Spring Boot
- @ExceptionHandler Example in Spring Boot.
- Spring Data JPA example using spring boot.
- Spring batch basic.
- Spring batch task scheduling example using spring boot.
- Spring transaction management basic.
- Spring transaction management example using spring boot.
- Spring security default authorization example using spring boot.
- Spring security inMemoryAuthentication and authorization example using spring boot.
- How to deploy multiple war files in Jboss in same port.
- @Transactional noRollbackForClassName example using spring boot
- @Transactional rollbackForClassName example using spring boot
- @Transactional readonly true example in spring boot
- @Transactional noRollbackFor example using spring boot.
- @Transactional rollbackFor example using spring boot.
Check @SpringBootApplication annotation docs.