Spring Boot makes extensive use of annotations to simplify and streamline application development. Here’s a comprehensive list of common annotations used in Spring Boot, along with brief explanations:
Core Annotations
1. @SpringBootApplication
- Description: A
convenience annotation that combines
@Configuration
,@EnableAutoConfiguration
, and@ComponentScan
. It's typically placed on the main class to enable auto-configuration, component scanning, and configuration.
2. @Component
- Description: Marks a
class as a Spring-managed component. It's a generic stereotype for any
Spring-managed component.
3. @Service
- Description: A
specialization of
@Component
used to indicate that a class provides a service. Typically used in service layer classes.
4. @Repository
- Description: A
specialization of
@Component
used to indicate that a class is a data repository. It also provides additional support for exception translation in the persistence layer.
5. @Controller
- Description: Marks a
class as a Spring MVC controller, capable of handling web requests.
6. @RestController
- Description: A
combination of
@Controller
and@ResponseBody
, used for RESTful web services. It indicates that the class's methods return JSON or XML directly to the client.
7. @Configuration
- Description: Indicates
that a class declares one or more
@Bean
methods and may be processed by the Spring container to generate bean definitions and service requests.
8. @Bean
- Description: Indicates
that a method produces a bean to be managed by the Spring container.
Data Access Annotations
9. @Entity
- Description: Marks a
class as a JPA entity, representing a table in a database.
10. @Table
- Description: Specifies
the table to be used for mapping an entity. It’s optional; if not
provided, the table name defaults to the entity name.
11. @Id
- Description: Specifies
the primary key of an entity.
12. @GeneratedValue
- Description: Defines the
strategy for generating primary key values.
13. @Column
- Description: Specifies
the details of a column in a database table.
14. @Repository
- Description: Marks a
class as a Data Access Object (DAO) and enables exception translation.
15. @Transactional
- Description: Describes a
transaction boundary for a method or class.
Web Layer Annotations
16. @RequestMapping
- Description: Maps web
requests to specific handler methods or classes.
17. @GetMapping
- Description: A shortcut
for
@RequestMapping(method = RequestMethod.GET)
.
18. @PostMapping
- Description: A shortcut
for
@RequestMapping(method = RequestMethod.POST)
.
19. @PutMapping
- Description: A shortcut
for
@RequestMapping(method = RequestMethod.PUT)
.
20. @DeleteMapping
- Description: A shortcut
for
@RequestMapping(method = RequestMethod.DELETE)
.
21. @PathVariable
- Description: Binds a
method parameter to a URI template variable.
22. @RequestParam
- Description: Binds a
method parameter to a request parameter.
23. @RequestBody
- Description: Binds the
HTTP request body to a method parameter.
24. @ResponseBody
- Description: Indicates
that the return value of a method should be bound to the web response
body.
25. @RequestHeader
- Description: Binds a
method parameter to a request header.
26. @CookieValue
- Description: Binds a
method parameter to an HTTP cookie.
Configuration and Profiles
27. @Profile
- Description: Specifies
the profile(s) for which a bean should be registered.
28. @PropertySource
- Description: Specifies
the locations of properties files to be loaded.
29. @Value
- Description: Injects
values into fields from property files or other sources.
Testing Annotations
30. @SpringBootTest
- Description: Indicates
that the annotated class is a Spring Boot test, which provides full
application context support.
31. @MockBean
- Description: Adds
Mockito mocks as a Spring Bean.
32. @DataJpaTest
- Description: Focuses on
JPA components and configuration, typically used for testing JPA
repositories.
33. @WebMvcTest
- Description: Configures
a Spring MVC test that focuses on Spring MVC components.
Miscellaneous
34. @Autowired
- Description: Injects
dependencies automatically by type.
35. @Qualifier
- Description: Specifies
which bean to inject when multiple beans of the same type exist.
36. @PostConstruct
- Description: Indicates a
method to be executed after dependency injection is complete.
37. @PreDestroy
- Description: Indicates a
method to be executed before a bean is destroyed.
38. @EnableAutoConfiguration
- Description: Tells
Spring Boot to start adding beans based on classpath settings, other
beans, and various property settings.
39. @EnableJpaRepositories
- Description: Enables JPA
repositories and provides configuration for them.
@Query
Annotation
Purpose: The @Query
annotation is used to define custom JPQL (Java Persistence Query Language) or
SQL queries directly on repository methods in Spring Data JPA. This is
particularly useful when you need more complex queries that cannot be easily
expressed using Spring Data JPA’s method query derivation.
Usage: The @Query
annotation is applied to repository methods in a Spring Data JPA repository
interface. It allows you to specify the exact query to execute, which can be
either JPQL or native SQL.
Syntax
and Example
1. JPQL Query:
public
interface
UserRepository
extends
JpaRepository<User, Long> {
@Query("SELECT u FROM User u WHERE u.username = :username")
User
findByUsername(@Param("username") String username);
}
2. Native SQL Query:
public
interface
UserRepository
extends
JpaRepository<User, Long> {
@Query(value = "SELECT * FROM users WHERE username = :username", nativeQuery = true)
User
findByUsernameNative(@Param("username") String username);
}
Parameters:
@Param("paramName")
: Specifies the name of the parameter in the query. This is necessary for mapping method parameters to query parameters.nativeQuery = true
: Indicates that the query is a native SQL query rather than JPQL.
No comments:
Post a Comment