Spring Annotation

Annotation


πŸ‘‰ @SpringBootApplication - Its combination of 3 annotations 
  1. @Configuration - Enable java-based configuration, and class annotated with @configuration is used to define spring beans.
  2. @ComponentScan - Scan package and sub package for stereotype annotations such as @component @Service, @controller, @Repository. It used to auto detect and register bean in spring application context.
  3. @EnableAutoConfiguration - Enable auto configure feature of spring boot which can automatically configure certain spring feature based upon dependency present in class path. We can exclude certain classes from auto configuration using exclude attribute.
πŸ‘‰ Stereotype annotations (Spring IOC scan beans and manage lifecycle of beans)
  • @Component - Annotation that allows spring to detect our custom bean automatically.
  • @Service - The business logic of an application usually resides within the service layer, so we’ll use the @Service annotation to indicate that a class belongs to that layer.
  • @RestController - Specialized version of Controller, that combine @Controller and @ResponseBody and used for building RESTful web service. It converts return object into Json or XML response.
  • @Controller - Mark as spring MVC controller. It can handle incoming HTTP request and return webpage or Json object handle webpage.
  • @Repository - @Repository’s job is to catch persistence-specific exceptions and re-throw them as one of Spring’s unified unchecked exceptions.

πŸ‘‰ Spring Core
  • @Configuration - Class represent source of bean definitions.
  • @Bean - Is applied on method to return bean to be managed by spring context.
  • @Autowired - Inject beans in our class
  • @Qualifier - To avoid conflict. (If Interface have more than one implementation then we use @Qualifier with auto wiring to inject right implementation object)
  • @Lazy - Mark bean as lazy initialized. (Bean will not initiate until it requested by another bean or user)
  • @Value - Read value from property file
  • @PropertySource - provide properties file to spring environment.
  • @ConfigurationProperties - maps entire properties or YMAL file to object
  • @Profile - mapping the bean to particular profile
  • @Scope - to specify bean lifecycle


πŸ‘‰ Rest API

  • @RestController- Combination @Controller and @ResponseBody. convert return value of handler mapping method to Json or XML format. used in Restful web service API.
  • @RequestMapping - Used for map http request to handler method use with GET, PUT, POST, DELETE method
  • @GetMapping - Most specific to map request to handler method with GET method
  • @PostMapping- Most specific to map request to handler method with POST method
  • @PutMapping- Most specific to map request to handler method with PUT method
  • @DeleteMapping - Most specific to map request to handler method with DELETE method
  • @RequestBody - Map HTTP request body to domain object. Spring automatically deserializes the JSON into a Java type.
  • @PathVariable- Accessing value from URI template
  • @RequestParam -Accessing query parameter value from request
  • @ControllerAdvice- Allows handling exceptions across the whole application in one global handling component
  • @ExceptionHandler - Handle specific exceptions and sending custom response to client.
NOTE: 
@RequestParam-
http://localhost:8080/springmvc/hello/101?param1=10&param2=20

In the above URL request, the values for param1 and param2 can be accessed as below:

public String getDetails(
    @RequestParam(value="param1", required=true) String param1,
        @RequestParam(value="param2", required=false) String param2){
...
}
@PathVariable-

@PathVariable identifies the pattern that is used in the URI for the incoming request. Let’s look at the below request URL:

http://localhost:8080/springmvc/hello/101?param1=10&param2=20

The above URL request can be written in your Spring MVC as below:

@RequestMapping("/hello/{id}")    public String getDetails(@PathVariable(value="id") String id,
    @RequestParam(value="param1", required=true) String param1,
    @RequestParam(value="param2", required=false) String param2){
.......
}
πŸ‘‰ Spring Data JPA 
  • @Entity- Mark class as Entity and mapped to database table
  • @Table- Specify details of database table
  • @Column - Specify details of column
  • @Transactional - To wrap method in database transaction. It allows us to set propagation, isolation, timeout, read-only, and rollback conditions for our transaction.


πŸ‘‰ Entity class relationship
  • @OneToOne- Map source entity to target entity
  • @OneToMany - One row of 1 table to map with multiple rows of another table
  • @ManyToOne - Many records of one table to mapped with one record of other table
  • @ManyToMany -
πŸ‘‰ Security related 
  • @CrossOrigin
  • @Secured
  • @PreAuthorize
  • @PermitAll
πŸ‘‰ Caching
  • @EnableCaching
  • @Cacheable
  • @CachePut
  • @CacheEvict
πŸ‘‰ Aspect Oriented -AOP

  • @Aspect
  • @Pointcut
  • @AfterRunning
  • @AfterThowing
  • @Around
  • @Before


πŸ‘‰ Junit
  • @ContextConfiguration - This annotation specifies how to load the application context while writing a unit test for the Spring environment. 
  • Here is an example of using @ContextConfigurationalong with @RunWith annotation of JUnit to test a Service class in Spring Boot
  • @SpringApplicationConfiguration
  • This is the annotation that addresses the shortcomings of @ContextConfigurationannotation discussed in the previous section. It provides full Spring Boot treatment to your test classes like it not only load the beans in the Spring application context but also enables logging and loads properties from application.properties file.


Soon will update more!!!

Comments

Popular posts from this blog

Welcome to Java study !!!

Spring Dependency Injection