This repository contains examples and explanations of various Spring Boot annotations. Spring Boot is a powerful framework for building Java applications with minimal configuration. Annotations play a crucial role in configuring and customizing Spring Boot applications.
- Introduction
- Getting Started
- Annotations
- 3.1 @RestController
- 3.2 @RequestMapping
- 3.3 @Autowired
- 3.4 @Service
- 3.5 @Repository
- 3.6 @Component
- Usage
- Contributing
Spring Boot is an opinionated framework that aims to simplify the development of production-ready applications using the Spring ecosystem. One of the core features of Spring Boot is its extensive use of annotations. These annotations help configure the behavior of various components, simplify dependency injection, and enable features like web service endpoints.
This repository serves as a reference for developers who want to understand the different annotations provided by Spring Boot and how they can be used in their projects.
To get started, you need to have Java and Maven installed on your system. Clone this repository to your local machine and import it into your preferred Java IDE.
git clone https://github.com/Shubh2-0/SpringBoot_Annotations.git
This section covers some of the essential annotations used in Spring Boot applications.
The @RestController
annotation combines @Controller
and @ResponseBody
. It is used to define RESTful web service endpoints that directly return JSON or XML responses.
@RestController
public class ExampleController {
// Controller methods...
}
The @RequestMapping
annotation maps HTTP requests to specific controller methods. It allows you to define the URL paths and HTTP methods that trigger the corresponding methods.
@RestController
@RequestMapping("/api")
public class ExampleController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String sayHello() {
return "Hello, World!";
}
}
The @Autowired
annotation is used for automatic dependency injection. It allows Spring to automatically wire beans into dependent objects.
@Service
public class ExampleService {
// Service methods...
}
@RestController
public class ExampleController {
private final ExampleService exampleService;
@Autowired
public ExampleController(ExampleService exampleService) {
this.exampleService = exampleService;
}
// Controller methods using exampleService...
}
The @Service
annotation is used to mark a class as a service bean in Spring. Service beans typically hold business logic and are used to perform various operations.
@Service
public class ExampleService {
// Service methods...
}
The @Repository
annotation is used to indicate that a class is a repository (data access) bean. Repositories are responsible for database operations.
@Repository
public class ExampleRepository {
// Repository methods...
}
The @Component
annotation is a generic stereotype for any Spring-managed component. It indicates that a class is a Spring component and should be auto-detected during classpath scanning.
@Component
public class ExampleComponent {
// Component methods...
}
To use this repository, clone it to your local machine and explore the examples provided for each annotation. You can run the Spring Boot application using Maven:
cd SpringBoot_Annotations
mvn spring-boot:run
Feel free to modify the examples or add your own annotations to experiment with Spring Boot's capabilities.
Contributions are welcome! If you find any issues or want to add more examples, feel free to open a pull request.
If you want to contact me, you can reach me through below handles.