DEV Community

Oloruntobi Ajayi
Oloruntobi Ajayi

Posted on

Spring Boot Annotations

@SpringBootApplication:

Purpose: Marks the main class of a Spring Boot application. It combines three annotations:

@Configuration: Indicates that the class can be used by the Spring IoC container as a source of bean definitions.

@ComponentScan: Instructs Spring to scan and discover other components like controllers, services, and repositories in the package and its sub-packages.

@EnableAutoConfiguration: Enables Spring Boot's auto-configuration mechanism, allowing automatic configuration of beans based on dependencies and classpath.
Use Cases:
Main class of a Spring Boot application.
Example:

java
Copy code
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@RestController:

Purpose: Marks a class as a RESTful controller. It automatically serializes return objects into JSON/XML responses.
Use Cases:
Building RESTful web services.
Example:
java
Copy code
@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
@Autowired:

Purpose: Injects dependencies automatically. It's typically used with constructor injection, field injection, or setter injection.
Use Cases:
Dependency injection in Spring-managed beans.
Example:

java
Copy code
@Service
public class MyService {
private final MyRepository repository;

@Autowired
public MyService(MyRepository repository) {
    this.repository = repository;
}
Enter fullscreen mode Exit fullscreen mode

}
@Service:

Purpose: Marks a class as a service component. It's a specialization of @Component and is used to indicate that the class performs some business logic or service tasks.
Use Cases:
Service layer in a Spring application, which contains business logic.
Example:

java
Copy code
@Service
public class MyService {
public String doSomething() {
return "Service processing";
}
}
@Repository:

Purpose: Marks a class as a data access component. It's a specialization of @Component and is used to indicate that the class interacts with a database or other external data source.
Use Cases:
Data access layer in a Spring application, responsible for database operations.
Example:

java
Copy code
@Repository
public class MyRepository {
public String getData() {
return "Data from repository";
}
}
@RequestMapping:

Purpose: Maps HTTP requests to handler methods. It's used at class and method levels to define the URL mappings.
Use Cases:
Defining endpoints for controllers.

Example:
java
Copy code
@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
Summary:

@SpringBootApplication: Main class of a Spring Boot application, enabling auto-configuration, component scanning, and acting as a configuration source.

@RestController: Marks a class as a RESTful controller, automatically serializing return objects into JSON/XML responses.

@Autowired: Injects dependencies automatically into Spring-managed beans.

@Service: Marks a class as a service component, typically containing business logic.

@Repository: Marks a class as a data access component, typically used for database operations.

@RequestMapping: Maps HTTP requests to handler methods, used for defining endpoints in controllers.

Top comments (0)