0% found this document useful (0 votes)
6 views2 pages

03 SpringBoot Annotations

The document provides a comprehensive guide to core annotations in Spring Boot, categorized into stereotype annotations, dependency injection annotations, Spring Boot specific annotations, transaction and scheduling annotations, and validation annotations. Each category includes a list of annotations, their usage, and purpose within a Spring application. Key annotations include @Component, @Autowired, @SpringBootApplication, @Transactional, and various validation constraints.

Uploaded by

jacad86072
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

03 SpringBoot Annotations

The document provides a comprehensive guide to core annotations in Spring Boot, categorized into stereotype annotations, dependency injection annotations, Spring Boot specific annotations, transaction and scheduling annotations, and validation annotations. Each category includes a list of annotations, their usage, and purpose within a Spring application. Key annotations include @Component, @Autowired, @SpringBootApplication, @Transactional, and various validation constraints.

Uploaded by

jacad86072
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Spring Boot – Core Annotations Guide

1. Stereotype Annotations
Stereotype annotations mark classes with their role in the Spring application. Spring scans for
these and registers them as beans in the application context.

Annotation Used On Purpose


@Component Any class Generic Spring-managed bean
@Service Service layer Business logic
@Repository DAO layer Database access, exception translation
@Controller Web layer Handles HTTP requests (returns views)
@RestController Web layer @Controller + @ResponseBody

2. Dependency Injection Annotations


Annotation Description
@Autowired Injects dependency by type
@Qualifier Specifies which bean to inject when multiple exist
@Primary Marks preferred bean when multiple candidates exist
@Value Injects values from properties file
@Bean Declares a bean inside @Configuration class
@Configuration Marks class as source of bean definitions

@Service
public class OrderService {

@Autowired
private OrderRepository orderRepository;

@Value("${[Link]-orders:100}")
private int maxOrders;
}

3. Spring Boot Specific Annotations


Annotation Description
@SpringBootApplication Entry point: combines 3 annotations
@EnableAutoConfiguration Enables Spring Boot auto-config
@ComponentScan Scans packages for components
@ConditionalOnProperty Activates bean based on property value
@Profile Activates bean only for specific profiles
@ConfigurationProperties Binds properties file to a POJO

4. Transaction & Scheduling


@Transactional

@Transactional
public void transferMoney(Long from, Long to, double amount) {
[Link](from, amount);
[Link](to, amount);
}

@Scheduled

@Scheduled(cron = "0 0 9 * * MON-FRI")


public void sendDailyReport() {
[Link]();
}

5. Validation Annotations
Annotation Constraint
@NotNull Field must not be null
@NotBlank String must not be blank
@Size(min,max) String/collection size range
@Min / @Max Numeric min/max value
@Email Must be valid email format
@Pattern(regexp) Must match regex pattern

You might also like