Spring Annotations
Spring Annotations
📌 Spring Kafka
@EnableKafka: Enables Kafka-related
annotations.
@KafkaListener: Listens for Kafka messages.
@KafkaHandler: Used with class-level
@KafkaListener for multiple methods.
📌 Messaging & WebSockets
@EnableWebSocket: Enables WebSocket
configuration.
@MessageMapping: Maps incoming messages
to methods (used in STOMP).
@SendTo: Sends response message to a
specific topic.
@SubscribeMapping: Handles subscription
requests.
@JmsListener: Listens to JMS messages.
@RabbitListener: Listens to RabbitMQ
messages.
📌 Caching
@EnableCaching: Enables Spring's annotation-
driven cache management.
@Cacheable: Caches the result of a method.
@CachePut: Updates the cache with the
method result.
@CacheEvict: Removes an entry from the
cache.
📌 Spring Security
@EnableWebSecurity: Enables Spring
Security configuration.
@PreAuthorize: Method-level
security using expressions.
@Secured: Defines security
constraints on methods.
@WithMockUser: Mocks a user for
testing purposes.
📌 Spring Testing
@SpringBootTest: Loads a full application
context for integration testing.
@DataJpaTest: Configures an in-memory
database and tests only JPA components.
@WebMvcTest: Loads only web layer
(controllers) for testing.
@MockBean: Adds Mockito mock into the
Spring ApplicationContext.
@TestConfiguration: Provides test-specific
bean configurations.
📌 JPA & Transactions
@Entity: Marks a class as a JPA entity.
@Table: Maps the entity to a database table.
@Column: Maps a field to a database
column.
@Id: Marks a field as a primary key.
@GeneratedValue: Defines primary key
generation strategy.
@OneToMany, @ManyToOne: Defines
relationships between entities.
@Transactional: Declares a method or class
as transactional.
@EnableTransactionManagement: Enables
annotation-driven transaction
management.
📌 Spring Repository & Query
@Repository: Indicates a data access
component.
@EnableJpaRepositories: Enables JPA
repository scanning.
@Query: Defines a custom JPQL/SQL
query.
@Modifying: Marks a modifying query
(update/delete).
📌 Validation
@Valid: Triggers validation on method
arguments.
@NotNull: Validates that a value is not
null.
@Size: Validates size constraints (length or
collection size).
@Email: Validates that a field contains a
valid email address.
@Pattern: Validates a string against a
regex.
@Min, @Max: Validates numeric
boundaries.