Spring Framework Annotations (original) (raw)

Last Updated : 05 Mar, 2025

Spring framework is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. Spring framework mainly focuses on providing various ways to help you manage your business objects.

Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. They do not have a direct effect on the operation of the code they annotate, nor do they change the action of the compiled program. In this article, we will discuss the main types of annotations available in the Spring Framework with examples.

**Types of Spring Framework Annotations

The below diagram demonstrates the types of Spring Framework Annotations

Spring-Framework-Annotations

**1. Spring Core Annotations

Spring annotations are present in the **org.springframework.beans.factory.annotation and **org.springframework.context.annotation packages. These annotations can be divided into two categories

Spring-Core-Annotations_

****@Autowired:** This annotation is applied to fields, setter methods, and constructors. It injects object dependency implicitly. The Spring container injects the dependency when it finds this annotation.

**Field Injection Example:

class Student {

@Autowired

Address address;

}

**Constructor Injection Example:

class Student {

Address address;

@Autowired

Student(Address address) {

this.address = address;

}

}

**Setter Injection Example:

class Student {

Address address;

@Autowired

void setAddress(Address address) {

this.address = address;

}

}

Context Configuration Annotations

****@Profile:** This annotation is used to indicate that a ****@Component** class or a ****@Bean** method should only be used when a specific profile is active.

**Example:

@Component
@Profile("developer")
public class Employee {}

2. Spring Web Annotations

Spring Web annotations are present in the **org.springframework.web.bind.annotation package. Some of the commonly used annotations in this category are:

****@Controller:** This annotation marks a class as a Spring MVC controller. It is used with @RequestMapping to handle web requests.

**Example:

Java `

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;

@Controller public class DemoController { @RequestMapping("/hello") @ResponseBody public String helloGFG() { return "Hello GeeksForGeeks"; } }

`

3. Spring Boot Annotations

Spring Boot annotations are present in the org.springframework.boot.autoconfigure and **org.springframework.boot.autoconfigure.condition packages. Some of the commonly used annotations in this category are:

****@SpringBootApplication:** This annotation is used to mark the main class of a Spring Boot application. It encapsulates @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations with their default attributes.

**Example:

Java `

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

`

4. Spring Scheduling Annotations

Spring Scheduling annotations are present in the **org.springframework.scheduling.annotation package. Some of the commonly used annotations in this category are:

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:

Java `

class Student { @Id Long id; }

`

6. Spring Bean Annotations

Spring Bean annotations are used to configure beans in a Spring container. Some of the commonly used annotations in this category are:

**Example:

Java `

@Service public class UserService { // business logic }

@Repository public class UserRepository { // CRUD operations }

@Controller public class UserController { // handle user requests }

`

Types of @Stereotype Annotation

The below diagram demonstrates the types of Stereotype annotation

_Types-of-Stereotype-annotations