Annotations
Annotations
Annotations
Annotation on Bean
import It is a method-level annotation. It is
@Bean org.springframework an alternative of XML tag. It tells the
method to produce a bean to be
.context.annotation.Bean; managed by Spring Container.
Annotation on Controller Class
Annotation on Object
import It is used to autowire spring bean
on setter methods, instance
org.springframework.beans variable, and constructor. When
@Autowired .factory.annotation.Autowired; we use @Autowired annotation,
the spring container autowires
the bean by matching data-type.
CRUD Operation Annotation
import It maps the HTTP POST requests on the
@PostMapping org.springframework.
specific handler method. It is used to
create a web service endpoint that
(value= "/uripath”) web.bind.annotation. creates resource. It is used instead of
using: @RequestMapping(method =
PostMapping; RequestMethod.POST)
import It maps the HTTP GET requests on the
@GetMapping org.springframework.
specific handler method. It is used to
create a web service endpoint that
(value="/uriPath”) web.bind.annotation. fetches resource. It is used instead of
using: @RequestMapping(method =
GetMapping; RequestMethod.GET)
import It maps the HTTP PUT requests on the
@PutMapping org.springframework.
specific handler method. It is used to
create a web service endpoint that
(value="/uripath”) web.bind.annotation. creates or updates resource. It is used
instead of using:
PutMapping; @RequestMapping(method =
RequestMethod.PUT)
import It maps the HTTP DELETE requests on
@DeleteMapping org.springframework.
the specific handler method. It is used
to create a web service endpoint that
(value="/uripath”) web.bind.annotation. deletes a resource. It is used instead of
using: @RequestMapping(method =
DeleteMapping; RequestMethod.DELETE)
Annotation on REST request
import It is used to bind HTTP
@RequestBody org.springframework.web request with an object in a
method parameter.
.bind.annotation.RequestBody;
import used to extract values from
@PathVariable org.springframework.web.bind. the URI path and use them as
method parameters.
("email") annotation.PathVariable;
import Annotation which indicates
@RequestParam org.springframework.web.bind that a method parameter
should be bound to a
(”email”) .annotation.RequestParam; webrequest parameter.
@Pattern(regexp = "[az]" import javax.validation These annotations are used to
validate incoming data before
, message = "abc”) .constraints.*; processing it in your
@NotNull, @Min, @Max application.
import jakarta.validation.Valid; If you have a REST endpoint
@Valid then use @Valid with a DTO
class as a method parameter.
Annotation on DTO
@OneToOne
import javax.persistence to be used foreign/reference
@JoinColumn key.
.JoinColumn;
(name="abc")
import Marks a field as not persistent,
@Transient meaning it won't be mapped to a
javax.persistence.Transient; database column.
Stereotype Annotation
import This annotation is a generic
stereotype for any Spring-managed
org.springframework component. It tells Spring to scan
@Component .stereotype.Component; the classpath for classes annotated
with @Component and create
beans from them.
import This annotation is used to mark a
@Controller class as a Spring MVC controller. It is
org.springframework typically used in web applications to
.stereotype.Controller; handle HTTP requests.
Transactional Annotation
import If an exception is thrown
org.springframework.transacti within the method, the
transaction can be rolled back,
@Transactional on.annotation.Transactional;
ensuring data consistency. You
can annotate a specific
method or class.
Exception Annotation
This annotation is used to define
a global exception handler that
import can be applied to multiple
org.springframework controllers. You can create a
class annotated with
.web.bind.annotation @RestControllerAdvice and
@RestControllerAdvice .RestControllerAdvice; define methods within it
annotated with
@ExceptionHandler to handle
specific exceptions.
You can use the
@ExceptionHandler annotation
@ExceptionHandler import on methods within your
yInternException.class)
MethodArgumentNotValidException
import exception is typically thrown when
@ExceptionHandler({ org.springframework.we validating the request parameters,
request body, or method arguments
MethodArgumentNot b.bind.MethodArgument of a controller endpoint using
NotValidException; annotation-based validation (e.g.,
ValidException.class, @Valid and validation annotations
like @NotNull, @Size, etc.).
ConstraintViolation import ConstraintViolationException
exception is more closely associated
Exception.class}) javax.validation.Constrai with Bean Validation and is thrown
when there are validation
ntViolationException; constraints violated on JavaBeans.
These constraints are typically
defined using annotations like
@NotNull, @Size, @Email, etc.
import This annotation can be used to
@ResponseStatus(Http specify the HTTP status code to
Status.NOT_FOUND) org.springframework return when a specific exception
.web.bind.annotation occurs. You can use it in
combination with
.ResponseStatus; @ExceptionHandler.
Aspects Annotation
Aspects in Spring Boot are
commonly used for tasks like
import logging, security, transaction
@Aspect org.aspectj.lang management, and
performance monitoring.
.annotation.Aspect; They allow you to keep these
cross-cutting concerns
separate from your core
business logic, making your
codebase more modular and
maintainable.
You define pointcut
@AfterThrowing expressions using annotations
import like @Before, @After,
@Before, org.aspectj.lang @Around, etc. These
expressions determine where
@After, .annotation.*; in your codebase the advice
methods should be applied.
@Around