0% found this document useful (0 votes)
7 views

Spring_Annotations

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Spring_Annotations

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Spring Framework Annotations and Descriptions

Core Spring Annotations

@Component: Marks a class as a Spring-managed component.

@Controller: Marks a class as a Spring MVC controller.

@Service: Marks a class as a service component.

@Repository: Indicates a Data Access Object (DAO) class.

@Configuration: Denotes a class as a source of bean definitions.

@Bean: Marks a method as a producer of a Spring-managed bean.

@Autowired: Injects a bean by type into a field, setter, or constructor.

@Qualifier: Specifies which bean to inject when multiple beans of the same type exist.

@Value: Injects values from properties files or environment variables.

Spring Boot-Specific Annotations

@SpringBootApplication: Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan.

@EnableAutoConfiguration: Automatically configures Spring Boot components based on dependencies.

@RestController: Combines @Controller and @ResponseBody for RESTful web services.

@SpringBootTest: Used for integration testing in Spring Boot.

Spring MVC Annotations

@RequestMapping: Maps HTTP requests to specific handler methods or classes.

@GetMapping: Shortcut for mapping HTTP GET requests.

@PostMapping: Shortcut for mapping HTTP POST requests.

@PutMapping: Shortcut for mapping HTTP PUT requests.

@DeleteMapping: Shortcut for mapping HTTP DELETE requests.

@RequestParam: Binds HTTP request parameters to method arguments.

@PathVariable: Binds URI template variables to method arguments.


@RequestBody: Maps the body of an HTTP request to a method argument.

@ResponseBody: Writes the return value of a method to the HTTP response body.

@ModelAttribute: Binds method parameters or return values to a named model attribute.

@ExceptionHandler: Handles specific exceptions thrown in a controller.

@CrossOrigin: Enables Cross-Origin Resource Sharing (CORS).

Spring AOP (Aspect-Oriented Programming) Annotations

@Aspect: Marks a class as an aspect in AOP.

@Before: Runs advice before a matched method execution.

@After: Runs advice after a matched method execution.

@AfterReturning: Runs advice after a matched method successfully returns.

@AfterThrowing: Runs advice when a matched method throws an exception.

@Around: Surrounds a matched method with advice.

Spring Data JPA Annotations

@Entity: Marks a class as a JPA entity.

@Table: Specifies the table name for an entity.

@Id: Marks a field as the primary key.

@GeneratedValue: Specifies the generation strategy for primary keys.

@Column: Maps a field to a database column.

@OneToOne: Defines a one-to-one relationship between entities.

@OneToMany: Defines a one-to-many relationship between entities.

@ManyToOne: Defines a many-to-one relationship between entities.

@ManyToMany: Defines a many-to-many relationship between entities.

@JoinColumn: Specifies a foreign key column for relationships.

@Query: Defines custom SQL queries for repository methods.

@Transactional: Declares transactional behavior on a method or class.


Spring Security Annotations

@EnableWebSecurity: Enables Spring Security configuration.

@Secured: Restricts access to methods based on roles.

@PreAuthorize: Allows method-level security using SpEL.

@PostAuthorize: Applies security checks after a method is executed.

@RolesAllowed: Specifies roles allowed to access a method.

Spring Test Annotations

@Test: Marks a method as a test method.

@MockBean: Adds a mock bean to the Spring context for testing.

@SpyBean: Adds a spy bean to the Spring context for testing.

@WebMvcTest: Tests only the web layer of a Spring Boot application.

@DataJpaTest: Tests JPA repositories.

Miscellaneous Annotations

@EnableScheduling: Enables scheduling support in the application.

@Scheduled: Defines scheduled tasks to run periodically.

@EnableAsync: Enables asynchronous method execution.

@Async: Marks a method to be executed asynchronously.

@PropertySource: Loads properties files into the Spring environment.

@Profile: Activates a bean or component based on the active profile.

@Lazy: Delays the initialization of a bean until it's required.

You might also like