Friday, 5 April 2013

Spring Boot Core Annotations



✅ 4) Spring Boot Annotations (Core)

🔹 Definition:

Spring Boot provides a set of annotations to simplify configuration, auto-wiring, and REST API creation.

AnnotationWhat It DoesWhere Used
@SpringBootApplicationCombines @Configuration@EnableAutoConfiguration@ComponentScanMain class
@ComponentScanScans packages for beans/componentsMain class or config class
@EnableAutoConfigurationEnables automatic configuration based on dependenciesMain class
@RestControllerCombines @Controller and @ResponseBodyREST controller
@RequestMappingMaps HTTP requestsOn methods or classes
@AutowiredInjects dependenciesFields, constructors
@ValueInjects properties from application.propertiesFields
@BeanRegisters a method’s return as a Spring beanInside @Configuration
@ConfigurationDeclares a configuration classAny config class

1. @SpringBootApplication

Marks the main class of a Spring Boot application.
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.

3. @ComponentScan

Tells Spring where to look for annotated components (like @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

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

AnnotationPurpose

@EnableCaching
Enables caching mechanism
@EnableSchedulingEnables scheduled tasks via @Scheduled
@EnableAsyncEnables asynchronous execution with @Async
@EnableJpaRepositoriesEnables Spring Data JPA repositories
@EnableTransactionManagement    Enables transaction support

No comments:

Post a Comment