Spring Framework Annotations

Last Updated : 24 Oct, 2025

The Spring Framework is a lightweight, open-source framework for building enterprise-level Java applications. It simplifies dependency injection, data access, web development, and configuration through annotations, reducing XML configuration.

Spring annotations serve as metadata that provide additional information about classes, methods, or fields. They help Spring automatically detect, configure, and manage application components.

Types of Spring Framework Annotations

  • Spring Core Annotations
  • Spring Web Annotations
  • Spring Boot Annotations
  • Spring Scheduling Annotations
  • Spring Data Annotations
  • Spring Bean Annotations
Spring-Framework-Annotations
Spring Framework annotations

1. Spring Core Annotations 

Located in org.springframework.beans.factory.annotation and org.springframework.context.annotation.

Spring-Core-Annotations_
Spring core annotations

1. Dependency Injection(DI)-Related Annotations

  • @Autowired: Injects dependencies automatically via field, constructor, or setter.
Java
class Student {
    @Autowired
    private Address address;
}
  • @Qualifier: Specifies which bean to inject when multiple beans of the same type exist.
  • @Primary: Marks a bean as the default when multiple candidates exist.
  • @Bean: Defines a bean explicitly in a configuration class.
  • @Scope: Defines bean scope (singleton, prototype, etc.).
  • @Value: Injects values from property files.
  • @Lazy: Defers bean initialization until first use.
  • @Required: Marks a property as mandatory for dependency injection.
  • @Lookup: Used for method injection to return prototype-scoped beans.

2. Context Configuration Annotations

  • @Profile: Activates a bean or component only under a specific profile.
Java
@Component
@Profile("dev")
class DevConfig {}
  • @Import: Imports additional configuration classes.
  • @ImportResource: Loads XML configuration files.
  • @PropertySource: Specifies property file locations.

2. Spring Web Annotations

  • @Controller : Marks a class as an MVC controller
  • @RequestMapping : Maps HTTP requests to handler methods
  • @RequestBody : Binds request body to a Java object
  • @ResponseBody : Sends method return value as HTTP response
  • @PathVariable : Extracts values from the URI
  • @RequestParam : Extracts query parameters from URL
  • @RestController : Combines @Controller and @ResponseBody for REST APIs
  • @ExceptionHandler : Handles exceptions in controller methods
  • @ResponseStatus : Defines HTTP status for responses
  • @ModelAttribute : Binds method parameters or return values to the model
  • @CrossOrigin : Enables CORS for controllers or specific endpoints

Example:

Java
@RestController
@RequestMapping("/api")
class DemoController {
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello GeeksForGeeks";
    }
}

3. Spring Boot Annotations

Located in org.springframework.boot.autoconfigure and related packages.

  • @SpringBootApplication: Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan.
Java
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  • @EnableAutoConfiguration: Enables automatic configuration based on dependencies.
  • @ConditionalOnClass / @ConditionalOnMissingBean: Conditional bean registration.
  • @ConfigurationProperties: Maps external configuration properties to POJOs.

4. Spring Scheduling Annotations

Located in org.springframework.scheduling.annotation.

  • @EnableScheduling: Enables scheduling.
  • @Scheduled: Schedules a task execution.
  • @EnableAsync: Enables asynchronous method execution.
  • @Async: Executes methods asynchronously.

Example:

Java
@EnableScheduling
class SchedulerConfig {
    @Scheduled(fixedRate = 5000)
    void printTime() {
        System.out.println(LocalTime.now());
    }
}

5. Spring Data Annotations

Spring Data annotations provide an abstraction over data storage technologies. Some of the commonly used annotations in this category are:

@Transactional: This annotation marks a method or class as transactional.

Example:

Java
@Transactional
void performPayment() {}


@Id: This annotation marks a field in a model class as the primary key.

Example:

6. Spring Bean Annotations

Used for persistence and transaction management.

  • @Transactional: Defines transactional boundaries.
  • @Id: Marks a field as the primary key.
  • @Query: Defines custom queries for repositories.
  • @Procedure: Calls stored procedures.
  • @Modifying: Marks queries that modify data.
  • @Lock: Specifies locking behavior.
  • @EnableJpaRepositories: Enables JPA repositories.

MongoDB-specific:

  • @Document: Marks a class as a MongoDB document.
  • @Field: Maps document fields to class fields.

Example:

Java
@Entity
class Student {
    @Id
    private Long id;
    private String name;
}

6. Spring Bean & Stereotype Annotations

Used for component scanning and bean definition.

_Types-of-Stereotype-annotations
Stereotype annotations
  • @Component: Generic annotation for any Spring-managed bean.
  • @Service: Indicates a service-layer class.
  • @Repository: Marks DAO or repository classes.
  • @Controller: Marks presentation-layer controllers.
  • @Configuration: Indicates a configuration class.
  • @ComponentScan: Defines base packages for scanning annotated components.

Example:

Java
@Service
public class UserService {}

@Repository
public class UserRepository {}

@Controller
public class UserController {}
Comment

Explore