Spring Annotations
Spring Annotations
𝙐𝙨𝙚𝙙
𝘼𝙣𝙣𝙤𝙩𝙖𝙩𝙞𝙤𝙣𝙨
𝙞𝙣 𝙎𝙥𝙧𝙞𝙣𝙜
Component Scanning & Dependency
Injection
• @Component : Marks a class as a Spring-managed
component. Used for generic components that don't fit
into @Service, @Repository, or @Controller categories.
• @Controller : Specialized @Component, used in Spring
MVC to define a web controller.
• @RestController : Combines @Controller and
@ResponseBody to simplify RESTful API development.
• @Service : Specialized @Component, used for
business logic and service layer components.
• @Repository : Specialized @Component, used for data
access objects (DAO) to indicate storage operations.
• @Autowired : Automatically injects dependencies into
Spring beans, reducing manual configuration.
• @Qualifier("beanName") : Specifies which bean to
inject when multiple candidates exist.
• @Primary : Marks a bean as the default choice when
multiple candidates exist.
• @Value("${property.key}") : Injects values from
properties f iles into Spring beans.
1
Configuration & Bean Management
• @Configuration : Marks a class as a source of bean
definitions for the Spring container.
• @Bean : Declares a method that produces a Spring-
managed bean.
• @Import({ConfigClass.class}) : Imports another
configuration class.
• @ImportResource("classpath:config.xml") : Loads an
XML configuration file.
• @DependsOn("beanName") : Ensures a bean is
initialized only after specific dependencies are available.
• @Lazy : Postpones bean initialization until it is first
accessed.
• @Scope("prototype") : Defines bean scope (singleton,
prototype, etc.).
• @PropertySource("classpath:app.properties") : Loads
external property files.
• @EnableAspectJAutoProxy : Enables support for
Aspect-Oriented Programming (AOP) in Spring.
2
Spring AOP (Aspect Oriented Programming)
• @Aspect : Defines an aspect, a modularization of cross-
cutting concerns like logging.
• @Before("execution(* package.Class.method(..))") :
Executes advice before a matched method executes.
• @After("execution(* package.Class.method(..))") :
Executes advice after method execution (regardless of
outcome).
• @AfterReturning("execution(* package.Class.method
(..))") : Runs only when the method successfully returns.
• @AfterThrowing("execution(* package.Class.method
(..))") : Runs if the method throws an exception.
• @Around("execution(* package.Class.method(..))") :
Wraps method execution for pre- and post-processing.
• @Pointcut("execution(* package..*(..))") : Defines
reusable AOP expressions.
3
Spring MVC & REST
Request Handling
• @RequestMapping("/endpoint") : Maps HTTP requests
to handler methods.
• @GetMapping("/endpoint") : Handles HTTP GET
requests.
• @PostMapping("/endpoint") : Handles HTTP POST
requests.
• @PutMapping("/endpoint") : Handles HTTP PUT
requests.
• @DeleteMapping("/endpoint") : Handles HTTP DELETE
requests.
• @PatchMapping("/endpoint") : Handles HTTP PATCH
requests.
• @RequestParam("param") : Binds query parameters.
• @PathVariable("id") : Extracts values from URLs.
• @RequestBody : Converts request body to Java objects.
• @ResponseBody : Converts Java objects to JSON/XML
responses.
• @ModelAttribute("model") : Binds request data to a
model.
• @SessionAttributes("user") : Stores attributes in HTTP
session.
• @CrossOrigin("*") : Enables CORS support for a
controller.
4
Exception Handling
• @ExceptionHandler(Exception.class) : Catches
exceptions at the controller level.
• @ResponseStatus(HttpStatus.NOT_FOUND) : Defines
HTTP response status codes.
• @ControllerAdvice : Defines global exception handling
logic for multiple controllers.
5
Spring Messaging & WebSockets
• @EnableWebSocket : Enables WebSocket support.
• @MessageMapping("/chat") : Maps messages to
handler methods.
• @SendTo("/topic/updates") : Sends the response to a
specific destination after handling a WebSocket
message.
Spring Caching
• @EnableCaching : Enables caching in Spring.
• @Cacheable("cacheName") : Caches method results.
• @CachePut("cacheName") : Updates cache with fresh
data.
• @CacheEvict("cacheName") : Removes entries from
cache.
• @CacheConfig(cacheNames={"defaultCache"}) :
Configures caching settings at the class level.
6
Spring Security
• @EnableWebSecurity : Enables Spring Security
configuration.
• @Secured("ROLE_ADMIN") : Secures methods based
on roles.
• @RolesAllowed({"ROLE_USER", "ROLE_ADMIN"}) :
Defines allowed roles.
• @AuthenticationPrincipal : Injects the authenticated
user.
• @PreFilter : Filters method arguments before method
execution.
• @PostFilter : Filters the method return value after
method execution.
• @PreAuthorize("hasRole('ROLE_USER')") : Performs pre-
execution authorization based on roles.
• @PostAuthorize("returnObject.owner == authentication
.name") : Performs post-execution authorization.
7
Spring Data Cassandra Annotations
• @Table : Defines a Cassandra table for a class (similar to
@Entity in JPA).
• @PrimaryKeyColumn : Marks a field as part of the
primary key in Cassandra.
• @Column : Marks a field to be mapped to a Cassandra
column.
• @PartitionKey : Marks a field as the partition key for
Cassandra queries.
8
Spring Data JPA & Transactions
Entity Mapping
• @Entity : Declares a JPA entity.
• @Table(name="users") : Specifies table name.
• @Id : Marks a field as the primary key.
• @GeneratedValue(strategy=GenerationType.AUTO) :
Auto-generates primary key values.
• @Column(name="username") : Maps a field to a
database column.
• @OneToOne, @OneToMany, @ManyToOne,
@ManyToMany : Defines entity relationships.
• @Transient : Marks a field to be ignored by JPA (not
persisted in the database).
• @Embeddable : Marks a class as embeddable.
• @Embedded : Used to embed an embeddable class into
an entity.
• @Enumerated (EnumType.STRING): Specifies that an
enum type should be persisted in the database
• @Fetch: Specifies how collections are fetched from the
database (eager or lazy loading).
• @MappedBy("user") : Defines the inverse side of a
relationship.
9
Repository & Transactions
• @Transactional : Manages transactions at the method
or class level.
• @Query("SELECT u FROM User u WHERE u.email = ?1") :
Custom JPA queries.
• @Modifying : Used for update/delete queries.
• @EnableJpaRepositories : Enables Spring Data JPA
repositories.
• @Param: Binds method parameters to query parameters
in @Query.
10