✅ 4) Spring Boot Annotations (Core)
✅ 4) Spring Boot Annotations (Core)
🔹 Definition:
Spring Boot provides a set of annotations to simplify configuration, auto-wiring, and REST API creation.
Annotation What It Does Where Used @SpringBootApplicationCombines @Configuration, @EnableAutoConfiguration, @ComponentScan Main class @ComponentScanScans packages for beans/components Main class or config class @EnableAutoConfigurationEnables automatic configuration based on dependencies Main class @RestControllerCombines @Controller and @ResponseBody REST controller @RequestMappingMaps HTTP requests On methods or classes @AutowiredInjects dependencies Fields, constructors @ValueInjects properties from application.properties Fields @BeanRegisters a method’s return as a Spring bean Inside @Configuration @ConfigurationDeclares a configuration class Any config class
Spring Boot provides a set of annotations to simplify configuration, auto-wiring, and REST API creation.
| Annotation | What It Does | Where Used |
|---|---|---|
@SpringBootApplication | Combines @Configuration, @EnableAutoConfiguration, @ComponentScan | Main class |
@ComponentScan | Scans packages for beans/components | Main class or config class |
@EnableAutoConfiguration | Enables automatic configuration based on dependencies | Main class |
@RestController | Combines @Controller and @ResponseBody | REST controller |
@RequestMapping | Maps HTTP requests | On methods or classes |
@Autowired | Injects dependencies | Fields, constructors |
@Value | Injects properties from application.properties | Fields |
@Bean | Registers a method’s return as a Spring bean | Inside @Configuration |
@Configuration | Declares a configuration class | Any config class |
1.
@SpringBootApplication
Marks the main class of a Spring Boot application.
It combines
This annotation triggers auto-configuration, component scanning, and Java-based configuration.
It is typically placed on the entry-point class containing the
It combines
@Configuration, @EnableAutoConfiguration, and @ComponentScan.This annotation triggers auto-configuration, component scanning, and Java-based configuration.
It is typically placed on the entry-point class containing the
main() method.2. @Configuration
Indicates that the class declares one or more Spring beans.Used to define beans using Java configuration instead of XML.Each method annotated with @Bean inside this class returns an object to be managed by Spring.Commonly used for custom configuration classes.
Indicates that the class declares one or more Spring beans.
Used to define beans using Java configuration instead of XML.
Each method annotated with @Bean inside this class returns an object to be managed by Spring.
Commonly used for custom configuration classes.
3. @ComponentScan
Tells Spring where to look for annotated components (like
By default, it scans the package where the main class is located and its subpackages.
Used when you want to explicitly define the base packages to scan.
It helps Spring find and register beans automatically.
@Component, @Service, etc.).By default, it scans the package where the main class is located and its subpackages.
Used when you want to explicitly define the base packages to scan.
It helps Spring find and register beans automatically.
4. @EnableAutoConfiguration
Enables Spring Boot’s auto-configuration feature.
Based on classpath dependencies, it auto-configures beans and settings.
Part of @SpringBootApplication, but can be used separately.
Saves effort in manual configuration of common components like DataSource, MVC, etc.
5. @Bean
Part of
@SpringBootApplication, but can be used separately.Saves effort in manual configuration of common components like DataSource, MVC, etc.
@BeanDeclares a bean manually inside a @Configuration class.
Returned object is managed by the Spring container.
Useful for third-party libraries or custom instantiations.
Provides full control over bean instantiation.
@Bean
public MyService myService() {
return new MyServiceImpl();
}
Declares a bean manually inside a @Configuration class.
Returned object is managed by the Spring container.
Useful for third-party libraries or custom instantiations.
Provides full control over bean instantiation.
@Bean
public MyService myService() {
return new MyServiceImpl();
}
✅ Spring Boot Feature Enabling Annotations
✅ Spring Boot Feature Enabling Annotations
Annotation Purpose
@EnableCachingEnables caching mechanism @EnableSchedulingEnables scheduled tasks via @Scheduled @EnableAsyncEnables asynchronous execution with @Async @EnableJpaRepositoriesEnables Spring Data JPA repositories @EnableTransactionManagement Enables transaction support
| Annotation | Purpose |
|---|---|
| Enables caching mechanism |
@EnableScheduling | Enables scheduled tasks via @Scheduled |
@EnableAsync | Enables asynchronous execution with @Async |
@EnableJpaRepositories | Enables Spring Data JPA repositories |
@EnableTransactionManagement | Enables transaction support |
No comments:
Post a Comment