Spring Boot Annotations: Cheat sheet

Spring Boot Annotations: Cheat sheet

What are these annotations

In Java, annotations are a form of metadata that provides additional information about program elements, such as classes, methods, fields, or parameters. They allow developers to add instructions, markers, or descriptors to code without changing its behavior. Annotations are denoted by the @ symbol followed by the annotation name.

Annotations in Spring Boot help in simplifying configuration by providing a concise and declarative approach. Rather than relying on extensive XML configurations, developers can use annotations to define the desired behavior of their application components.

Advantages of using annotations:

  • Annotations like @Autowired in Spring Boot facilitate dependency injection, where dependencies are automatically resolved and injected into the components that require them.

  • Spring Boot integrates with Aspect-Oriented Programming using annotations like @Aspect and @Around. AOP allows developers to separate concerns, such as logging, caching, or security, from the core business logic, leading to cleaner and more maintainable code.

  • Annotations in Spring Boot allow developers to extend the framework's capabilities. For example, annotations like @EnableCaching and @EnableScheduling enable caching and scheduling functionalities, respectively, within the application without requiring additional configuration.

  • Spring Boot follows the principle of convention over configuration. By using specific annotations, such as @RestController or @Service, developers can adopt pre-configured defaults and conventions, reducing the need for explicit configuration.

Cheat Sheet

  • @SpringBootApplication -> marks the main class of the spring boot application. It combines '@Configuration', '@EnableAutoConfiguration' and '@ComponentScan' into a single convenient annotation.

  • @EnableAutoConfiguration -> It auto-configures the bean that is present in the class-path and configures it to run the methods.

  • @ComponentScan -> it is used when we want to scan a package of beans. It is used with the annotation @Configuration. We can also specify the base packages to scan for Spring Components.

  • @Component -> Marks a class as a generic spring component. It serves as a general-purpose stereotype annotation for any spring-managed component.

  • @Configuration -> indicates that a class declares Spring bean definitions and should be processed by the spring container. It is used in conjunction with '@Bean' methods.

  • @Bean -> Indicates that a method produces a bean to be managed by spring container. It is typically used in '@Configuration' class to define custom bean instances.

  • @Required -> It applies to the bean setter method. It indicates that the annotated bean must be populated at the configuration time with the required property or else it will throw an BeanInitializationException.

  • @Controller -> Marks a class as a controller in the MVC architecture. It handles HTTP requests and generates HTTP responses.

  • @RestController -> Combines '@Controller' and '@ResponseBody'. It is used to develop RESTful web services, automatically serializing the return objects into JSON/XML response bodies.

  • @GetMapping -> It maps the HTTP GET requests on the specific handler method. It is used to create a web service endpoint that fetches.

  • @PostMapping -> It maps the HTTP POST requests on the specific handler method. It is used to create a web service endpoint that creates.

  • @PutMapping -> It maps the HTTP PUT requests on the specific handler method. It is used to create a web service endpoint that creates or updates.

  • @DeleteMapping -> It maps the HTTP DELETE requests on the specific handler method. It is used to create a web service endpoint that deletes a resource.

  • @PatchMapping -> It maps the HTTP PATCH requests on the specific handler method.

  • @Autowired -> Injects dependencies into beans. It enables the automatic wiring of beans, reducing manual configuration and promoting loose coupling.

  • @Service -> Marks a class as a service logic in the application's business logic layer. It represents the service layer of the applications and encapsulates the business logic.

  • @Repository -> Marks the class as a repository layer in the data access layer. It provides database access and performs CRUD operations.

  • @Value -> Injects value from property files, environment variables or command-line arguments into bean fields or constructor parameters

  • @PostConstruct -> Specifes a method to be executed after bean initialization. It can be used for any initialization logic that needs to be performed after dependency injection.

  • @PreDestroy -> Specifies a method to be executed before a bean is destroyed. It can be used for any cleanup or resource release task.\

  • @Transactional -> Defines a transactional behaviour of a method or a class. it ensures that the annotated methods or the methods within the annotated class are executed within a transaction.

  • @Valid -> Enables validation of method parameters, request bodies or form objects using JSR-303 bean validation or custom validation logic.

  • @ExceptionHandler -> Defines method that handles specific exceptions thrown within a controller. It allows custom error handling and generation oh appropriate HTTP responses.

  • @PathVariable -> It is used to extract the values from the URL. It is most suitable for the RESTful web service, where the URL contains a path variable. We can define multiple @PathVariable in a method.

These annotations represent a subset of the commonly used annotations in Spring Boot. They provide essential functionality and simplify the development of Spring applications. Understanding and utilizing these annotations effectively can greatly enhance your productivity and make your code more maintainable and robust.

So, dive into the world of annotations, experiment with their capabilities, and unlock the full potential of Spring Boot. Happy coding!