Spring Boot Annotations: A Complete Guide with Examples
This document provides a comprehensive guide to Spring Boot annotations, including definitions,
explanations, examples, and use cases. It is organized to help developers understand why, where,
when, and how each annotation is used in a Spring Boot application.
1. Basic Annotations
@SpringBootApplication
Marks the main application class and combines @Configuration, @EnableAutoConfiguration, and
@ComponentScan for simplified setup.
Example:
Example:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
@Component, @Service, @Repository
@Component is a general-purpose bean annotation, while @Service and @Repository specify
service and repository layers, respectively.
Example:
Example:
import org.springframework.stereotype.Service;
@Service
public class UserService {
public String getUser() {
return "User details";
}
2. Configuration Annotations
@Configuration
Marks a class as a source of bean definitions. Used with @Bean to define Spring beans.
Example:
Example:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public UserService userService() {
return new UserService();
@Bean
Indicates a method that returns a Spring bean, usually within a @Configuration class.
Example:
Example:
@Bean
public UserService userService() {
return new UserService();
3. Web Layer Annotations
@RestController
Indicates that a class handles RESTful requests, automatically serializing responses to JSON.
Example:
Example:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@GetMapping("/user")
public String getUser() {
return "User details";
@RequestMapping, @GetMapping, @PostMapping
Map HTTP requests to controller methods. @RequestMapping is general, while others specify
HTTP methods.
Example:
Example:
@GetMapping("/hello")
public String sayHello() {
return "Hello, world!";
4. Data Layer Annotations
@Entity
Defines a JPA entity, mapping the class to a database table.
Example:
Example:
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class User {
@Id
private Long id;
private String name;
@Id
Marks a field as the primary key in a JPA entity.
Example:
Example:
@Id
private Long id;
5. Transactional and Security Annotations
@Transactional
Manages transactions, ensuring data consistency across multiple operations.
Example:
Example:
import org.springframework.transaction.annotation.Transactional;
@Transactional
public void saveUser(User user) {
// logic to save user
@PreAuthorize, @Secured
Used for role-based access control in methods.
Example:
Example:
import org.springframework.security.access.prepost.PreAuthorize;
@PreAuthorize("hasRole('ROLE_ADMIN')")
public void adminOnlyTask() {
// admin task
6. Testing Annotations
@SpringBootTest
Loads the Spring application context for integration testing.
Example:
Example:
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.Test;
@SpringBootTest
public class ApplicationTests {
@Test
public void contextLoads() {
@MockBean
Creates a mock bean for testing, replacing the actual bean in the application context.
Example:
Example:
import org.springframework.boot.test.mock.mockito.MockBean;
@MockBean
private UserService userService;
7. Advanced Annotations
@EnableAutoConfiguration
Enables automatic configuration, allowing Spring Boot to set up beans based on classpath settings.
Example:
Example:
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@EnableAutoConfiguration
public class MyAppConfig {}
@ConditionalOnProperty
Defines conditions for bean creation based on properties.
Example:
Example:
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@Bean
@ConditionalOnProperty(name = "feature.enabled", havingValue = "true")
public FeatureService featureService() {
return new FeatureService();