Last updated : August 1, 2026

Annotations Tutorials

Web Development

Spring Boot Annotations

1. Introduction

Annotations in Spring Boot are used to configure applications directly in Java code.

They help to:

  • Reduce XML configuration

  • Improve readability

  • Enable auto-configuration

  • Simplify dependency injection

2. Core Spring Boot Annotations

@SpringBootApplication

  • Main annotation for Spring Boot application

  • Combines:

    • @SpringBootConfiguration

    • @EnableAutoConfiguration

    • @ComponentScan

plaintext

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@SpringBootConfiguration

  • Used for configuration class

  • Similar to @Configuration

plaintext

@SpringBootConfiguration
public class AppConfig {
}

@EnableAutoConfiguration

  • Enables auto configuration

  • Automatically configures beans

plaintext

@EnableAutoConfiguration
public class AppConfig {
}

@ComponentScan

  • Scans package for components

plaintext

@ComponentScan("com.example")
public class AppConfig {
}

3. Spring Bean Annotations

@Component

  • Marks class as Spring bean

plaintext

@Component
public class EmailService {
}

@Service

  • Used in service layer

plaintext

@Service
public class UserService {
}

@Repository

  • Used for database layer

plaintext

@Repository
public class UserRepository {
}

@Configuration

  • Used for configuration class

plaintext

@Configuration
public class AppConfig {
}

@Bean

  • Used to create bean manually

plaintext

@Configuration
public class AppConfig {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

4. Dependency Injection Annotations

@Autowired

  • Automatically injects dependency

plaintext

@Autowired
private UserService userService;

@Qualifier

  • Specifies which bean to inject

plaintext

@Autowired
@Qualifier("emailService")
private NotificationService service;

@Primary

  • Default bean when multiple exist

plaintextCopy

@Primary
@Component
public class SmsService implements NotificationService {
}

5. Web and REST API Annotations

@RestController

  • Used to create REST APIs

plaintext

@RestController
public class HelloController {
}

@RequestMapping

  • Maps URL path

plaintext

@RequestMapping("/api")
public class ApiController {
}

HTTP Method Annotations

  • @GetMapping → Fetch data

  • @PostMapping → Create data

  • @PutMapping → Update data

  • @DeleteMapping → Delete data

plaintext

@GetMapping("/users")
public List<User> getUsers() {
    return userService.getAllUsers();
}

@PathVariable

  • Get value from URL

plaintext

@GetMapping("/users/{id}")
public User getUser(@PathVariable int id) {
    return userService.getUser(id);
}

@RequestParam

  • Read query parameters

plaintext

@GetMapping("/search")
public String search(@RequestParam String keyword) {
    return keyword;
}

@RequestBody

  • Read request body

plaintext

@PostMapping("/users")
public User saveUser(@RequestBody User user) {
    return userService.save(user);
}

6. Configuration Annotations

@Value

  • Inject value from properties

plaintext

@Value("${server.port}")
private String port;

@ConfigurationProperties

  • Bind properties to class

plaintext

@ConfigurationProperties(prefix = "app")
public class AppConfig {
    private String name;
}

7. Validation Annotations

  • @NotNull → Cannot be null

  • @NotBlank → Cannot be empty

  • @Email → Valid email

  • @Size → Size limit

plaintext

public class User {

    @NotBlank
    private String username;

    @Email
    private String email;
}

8. Exception Handling Annotations

@ExceptionHandler

  • Handle specific exception

plaintext

@ExceptionHandler(Exception.class)
public String handleException() {
    return "Error occurred";
}

@ControllerAdvice

  • Global exception handling

plaintext

@ControllerAdvice
public class GlobalExceptionHandler {
}

9. JPA Annotations

  • @Entity → Define entity class

  • @Table → Map table

  • @Id → Primary key

  • @GeneratedValue → Auto generate ID

  • @Column → Map column

10. Conclusion

  • Annotations reduce configuration complexity

  • Widely used in Spring Boot applications

  • Improve code readability and maintainability

Job PortalJobs
Spring Boot Architecture Tutorial for Beginners