In Spring Boot, annotations play a crucial role in configuring various aspects of the application. Here are some common annotations used in Spring Boot along with code examples:
- @SpringBootApplication: This annotation serves as the entry point of the Spring Boot application. It is a combination of three other annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan. @Configuration indicates that the class contains configuration beans, @EnableAutoConfiguration enables Spring Boot’s auto-configuration feature, and @ComponentScan tells Spring to scan and automatically discover other components and configurations within the package and its sub-packages.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
- @RestController: Used to define RESTful web services. It indicates that the class provides RESTful endpoints, and methods annotated with @GetMapping, @PostMapping, etc., are mapped to HTTP requests.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyRestController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
- @Service: This annotation is used to mark a class as a service component in the Spring framework. Service components typically contain the business logic of the application and are often used to perform tasks such as data manipulation, validation, and interaction with repositories.
import org.springframework.stereotype.Service;
@Service
public class MyService {
public void doSomething() {
// Service logic
}
}
- @Repository: Marks a class as a repository component in Spring, typically used for database operations. Repository components are responsible for data access and manipulation, such as saving, updating, deleting, and querying data from a database.
import org.springframework.stereotype.Repository;
@Repository
public class MyRepository {
public void save(Object obj) {
// Save object to the database
}
}
- @Autowired: This annotation is used for automatic dependency injection. It injects dependencies into the class constructor, fields, or methods, eliminating the need for manual bean wiring. Spring resolves the dependencies at runtime and injects the appropriate beans.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class MyController {
private final MyService myService;
@Autowired
public MyController(MyService myService) {
this.myService = myService;
}
// Controller methods
}
- @Configuration: Indicates that the class contains one or more bean definitions. Beans defined in a configuration class can be injected into other components using @Autowired or retrieved from the Spring application context.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfiguration {
@Bean
public MyBean myBean() {
return new MyBean();
}
}
These annotations are fundamental in Spring Boot applications for configuring components, defining RESTful endpoints, managing services and repositories, injecting dependencies, and organizing the application’s architecture.