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