VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/spring-core-annotations/

⇱ Spring Core Annotations - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring Core Annotations

Last Updated : 8 Nov, 2025

Spring Annotations are metadata in Java used to provide configuration and behavior information to the Spring Framework. They eliminate the need for XML-based configuration, making Spring applications more concise, readable, and easier to maintain.

In essence, annotations tell the Spring Container how to create, configure, and manage application components (beans). They are widely used for dependency injection, bean configuration, web components, and context management.

Types of Spring Framework Annotations

πŸ‘ spring_framework_annotations
Spring framework annotations
  • Spring Core Annotations: Dependency injection, bean lifecycle, and context configuration.
  • Spring Web Annotations: Used for building web and RESTful services.
  • Spring Boot Annotations: Simplify auto-configuration and bootstrapping.
  • Spring Scheduling Annotations: For task scheduling and asynchronous execution.
  • Spring Data Annotations: For JPA, repositories, and data persistence.
  • Spring Bean Annotations: For defining and managing beans.

Spring Core Annotations

Annotations in the org.springframework.beans.factory.annotation and org.springframework.context.annotation packages are collectively called Spring Core Annotations.

They can be divided into two main categories:

  • Dependency Injection (DI)-Related Annotations
  • Context Configuration Annotations
πŸ‘ spring_core_annotations_
Spring core annotations

DI-Related Annotations

These annotations are used to perform dependency injection and define how beans are created and wired together in the Spring container.

1. @Autowired

Automatically injects dependencies by type. It can be used on constructors, fields, or setter methods.

Example (Field Injection):

Example (Constructor Injection):

Example (Setter Injection):

2. @Qualifier

Used with @Autowired to resolve conflicts when multiple beans of the same type exist. It specifies the exact bean name to be injected.

3. @Primary

Specifies a default bean when multiple beans of the same type are available for autowiring.

4. @Bean

Indicates that a method produces a Spring-managed bean. Used inside @Configuration classes.

5. @Lazy

Delays the initialization of a bean until it is first requested.

6. @Value

Injects values from properties files, environment variables, or expression language.

7. @Scope

Defines the scope of a Spring bean. Common scopes:

  • singleton (default)
  • prototype
  • request
  • session
  • application

8. @Lookup

Used for method injection, it allows a method to return a new instance of a prototype-scoped bean each time it’s called.

9. @Required (Deprecated)

Previously used to indicate that a property must be injected, now deprecated as of Spring 5.1.

Context Configuration Annotations

These annotations are used to define how Spring discovers, configures, and loads beans into the ApplicationContext.

1. @Configuration

Marks a class as a source of bean definitions. Beans within this class are defined using @Bean methods.

2. @ComponentScan

Specifies the base packages to scan for Spring-managed components like @Component, @Service, or @Repository.

3. @Import

Used to import additional @Configuration classes into another configuration class.

4. @ImportResource

Imports legacy XML-based configurations into a Java-based configuration class.

5. @PropertySource

Imports legacy XML-based configurations into a Java-based configuration class.

6. @Profile

Specifies that a bean should only be registered when a particular Spring profile is active.

7. @PropertySource

Loads a .properties file into the Spring environment so that property values can be injected using @Value.

8. @Conditional

Conditionally registers a bean based on a custom condition class.

Comment
Article Tags:

Explore