Spring Boot Application annotation
The `@SpringBootApplication` annotation is a convenience annotation that combines three crucial Spring annotations to enable the auto-configuration of a Spring Boot application. Internally, it is equivalent to using the following three annotations:
1. @EnableAutoConfiguration:
- Purpose: Enables Spring Boot’s auto-configuration mechanism, which attempts to automatically configure your Spring application based on the dependencies that are present on the classpath.
- How It Works: It scans the classpath for dependencies and configures the beans accordingly.
2. @ComponentScan:
- Purpose: Enables component scanning, which automatically detects and registers beans annotated with `@Component`, `@Service`, `@Repository`, and `@Controller` in the specified package.
- How It Works: It scans the base package and its sub-packages for these annotations and registers them as beans in the Spring context.
3. @Configuration:
- Purpose: Indicates that the class can be used by the Spring IoC container as a source of bean definitions.
- How It Works: It allows defining beans using `@Bean` methods.
Table of Contents
Internal Breakdown of @SpringBootApplication
Example
```java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class))
public @interface SpringBootApplication {
// Optional attributes and methods
}
```
@SpringBootConfiguration
1. @SpringBootConfiguration
```java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}
```
Purpose: Indicates that this is a configuration class.
2. @EnableAutoConfiguration
2. @EnableAutoConfiguration
```java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
// Optional attributes and methods
}
```
Purpose: Enables auto-configuration by importing AutoConfigurationImportSelector
, which scans for META-INF/spring.factories
to load configuration classes.
Example
3. @ComponentScan
```java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
// Optional attributes and methods
}
```
- Purpose: Scans for components to be registered as beans.
Example
MainApplication.java
```java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
@Bean
public String exampleBean() {
return "This is a bean";
}
}
```
ExampleComponent.java
```java
import org.springframework.stereotype.Component;
@Component
public class ExampleComponent {
public String getMessage() {
return "Hello from ExampleComponent";
}
}
```
ExampleController.java
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ExampleController {
@Autowired
private ExampleComponent exampleComponent;
@GetMapping("/message")
public String getMessage() {
return exampleComponent.getMessage();
}
}
```
Explanation of Example
- MainApplication:
- The
@SpringBootApplication
annotation enables auto-configuration, component scanning, and configuration. - The
exampleBean
method defines a bean that will be managed by the Spring container.
- ExampleComponent:
- This class is annotated with
@Component
, making it a candidate for component scanning and bean registration.
- ExampleController:
- This class is annotated with
@RestController
, making it a web controller that handles HTTP requests. - It uses
ExampleComponent
to return a message when the/message
endpoint is accessed.
Summary
- @SpringBootApplication: Combines
@EnableAutoConfiguration
,@ComponentScan
, and@Configuration
to simplify the configuration of Spring Boot applications. - Example: Demonstrates the use of
@SpringBootApplication
to create and run a Spring Boot application with a simple REST controller and a component.