Annotations in Spring Boot are metadata that provide instructions to the Spring framework at runtime or compile time. Instead of using complex XML configurations, annotations allow developers to configure applications directly in Java code.
- Reduces XML-based configuration
- Improves code readability and maintainability
- Enables auto-configuration
- Simplifies dependency injection
- Speeds up application development
Core Spring Boot Annotations
1. @SpringBootApplication Annotation
- This annotation is used to mark the main class of a Spring Boot application.
- It encapsulates @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan annotations with their default attributes.

Example:
@SpringBootApplication
// Class
public class DemoApplication {
// Main driver method
public static void main(String[] args)
{
SpringApplication.run(DemoApplication.class, args);
}
}
2. @SpringBootConfiguration Annotation
Indicates that a class provides configuration for a Spring Boot application. It is a specialized version of @Configuration and is automatically included in @SpringBootApplication.
@SpringBootConfiguration
public class AppConfig {
}
3. @EnableAutoConfiguration Annotation
Enables Spring Boot’s auto-configuration mechanism, automatically configuring beans based on classpath dependencies and application properties.
@EnableAutoConfiguration
public class AppConfig {
}
4. @ComponentScan Annotation
Tells Spring where to search for components like @Controller, @Service, @Repository, etc.
@ComponentScan("com.example")
public class AppConfig {
}
Spring Bean Annotations
1. @Component
Marks a class as a generic Spring-managed component. Spring automatically detects and registers it during component scanning.
@Component
public class EmailService {
}
2. @Service
Used in the service layer to define business logic and improve code readability.
@Service
public class UserService {
}
3. @Repository
Used in the DAO layer to interact with the database. Automatically translates database-related exceptions into Spring exceptions.
@Repository
public class UserRepository {
}
4. @Configuration
Indicates that a class contains Spring configuration and bean definitions. Acts as a replacement for XML-based configuration.
@Configuration
public class AppConfig {
}
5. @Bean
Used to define a Spring bean explicitly inside a configuration class. Gives full control over bean creation and lifecycle.
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Dependency Injection Annotations
1. @Autowired
Automatically injects required dependencies into a class. Eliminates the need for manual object creation.
@Autowired
private UserService userService;
2. @Qualifier
Specifies which bean to inject when multiple beans of the same type exist.
@Autowired
@Qualifier("emailService")
private NotificationService service;
3. @Primary
Marks a bean as the default choice among multiple candidates. Used when no @Qualifier is explicitly specified.
@Primary
@Component
public class SmsService implements NotificationService {
}
Web and REST API Annotations
1. @RestController
Used to create RESTful web services and automatically returns data in JSON or XML format.
@RestController
public class HelloController {
}
2. @RequestMapping
Maps HTTP requests to controller classes or methods. Defines the base URL path for request handling.
@RequestMapping("/api")
public class ApiController {
}
3. HTTP Method Annotations
Annotation | HTTP Method | Explanation |
|---|---|---|
@GetMapping | GET | Retrieves data from the server |
@PostMapping | POST | Sends data to the server |
@PutMapping | PUT | Updates existing data |
@DeleteMapping | DELETE | Deletes data |
Example
@GetMapping("/users")
public List<User> getUsers() {
return userService.getAllUsers();
}
4. @PathVariable
Extracts values from the URI path and binds them to method parameters.
@GetMapping("/users/{id}")
public User getUser(@PathVariable int id) {
return userService.getUser(id);
}
5. @RequestParam
Reads query parameters from the request URL. Used for optional or filtering inputs.
@GetMapping("/search")
public String search(@RequestParam String keyword) {
return keyword;
}
6. @RequestBody
Binds the HTTP request body to a Java object. Commonly used with POST and PUT requests.
@PostMapping("/users")
public User saveUser(@RequestBody User user) {
return userService.save(user);
}
Configuration and Properties Annotations
1. @Value
Injects individual property values from application.properties or application.yml.
@Value("${server.port}")
private String port;
2. @ConfigurationProperties
Binds a group of related configuration properties to a POJO in a type-safe manner.
@ConfigurationProperties(prefix = "app")
public class AppConfig {
private String name;
}
Validation Annotations
Validation annotations ensure input data correctness.
- @NotNull - Field value must not be null
- @NotBlank - String must contain at least one non-whitespace character
- @Email - Validates email format
- @Size - Restricts field length or size
Example
public class User {
@NotBlank
private String username;
@Email
private String email;
}
Exception Handling Annotations
1. @ExceptionHandler
Handles specific exceptions within a controller. Allows custom error responses for exceptions.
@ExceptionHandler(Exception.class)
public String handleException() {
return "Error occurred";
}
2. @ControllerAdvice
Provides global exception handling across all controllers. Centralizes error-handling logic.
@ControllerAdvice
public class GlobalExceptionHandler {
}
JPA and Database Annotations
Annotation | Purpose |
|---|---|
@Entity | Marks a class as a JPA entity |
@Table | Specifies table mapping |
@Id | Defines primary key |
@GeneratedValue | Auto-generates primary key values |
@Column | Maps class field to table column |