Java 1.5 introduced annotations and now it’s heavily used in Java EE frameworks like Hibernate, Jersey, and Spring. Java Annotation is metadata about the program embedded in the program itself.
- @Controller
- @RequestMapping
- @PathVariable
- @RequestParam
- @ModelAttribute
- @RequestBody and @ResponseBody
- @RequestHeader and @ResponseHeader
- @SpringBootApplication
- @EnableAutoConfiguration
- @Configuration
- @ComponentScan
Spring boot introduced @SpringBootApplication annotation. This single annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan.
As a result, when we run this Spring Boot application, it will automatically scan the components in the current package and its sub-packages. Thus it will register them in Spring's Application Context, and allow us to inject beans using @Autowired.
- @Component
- @Service
- @RestController/ @Controller
- @Repository
- @RestController
- @RequestMapping
- @GetMapping
- @PostMapping
- @PutMapping
- @DeleteMapping
- @RequestMapping
- @RequestBody
- @PathVariable
- @RequestParam
- @ControllerAdvice
- @ExceptionHandler
- @Entity
- @Table
- @Column
- @Transactional
- @OnetoOne
- @OnetoMany
- @ManytoOne
- @ManytoMany
- @CrossOrigin
- @Secured
- @PreAuthorize
- @PermitAll
- @EnableCaching
- @Cacheable
- @CachePut
- @CacheEvict
- @Aspect
- @Pointcut
- @AfterRunning
- @AfterThrowing
- @Around
- @Before
@Component: this is the root annotation. Remaining three annotaions derived from @Component annotation.
Now question is why we need Remaining three annotations if we can use @Component every where? Yes We can use @Component everywhere where we need these but they denote that which class have what purpose. for example we have @RestController, So its tell us what is this class use for. like we rest method in this class. So annotaion makes easy to identify the class.
Same we have @Service annotation tells us that it is my service class and I have to write my business logic here.
@Repository annotaion where we write database logic. So three different annotation to define the role of classes.
- @Configuration
- @Bean
- @Autowired
- @Qualifier
- @Primary
- @Lazy
- @Value
- @PropertySource
- @ConfigurationProperties
- @Profile
- @Scope
we use @Bean inside a @Configuration class.
@Autowired: Spring Autowire annotation used to automatic injection of beans. if remove the @autowired annotation spring not able to create object of it and it will create NullPointerException. example in spring boot application we use it generally in controller class.
@Qualifier: Spring @Qualifier annotation is used in conjunction with Autowired to avoid confusion when we have two of more bean configured for same type.
We use @Qualifier in Spring to autowire a specific bean among same type of beans, where as @Primary is used to give high preference to the specific bean among multiple beans of same type to inject to a bean.
There's another annotation called @Primary that we can use to decide which bean to inject when ambiguity is present regarding dependency injection.
This annotation defines a preference when multiple beans of the same type are present. The bean associated with the @Primary annotation will be used unless otherwise indicated.
Let's see how we can use @Qualifier annotation to indicate the required bean. First we difine two beans of type of formatter
@Component("fooFormatter")
public class FooFormatter implements Formatter {
public String format() {
return "foo";
}
}
@Component("barFormatter")
public class BarFormatter implements Formatter {
public String format() {
return "bar";
}
}
here we have two different classes which implemetning same interface. lets inject formatter in Forservice class.
public class FooService {
@Autowired
private Formatter formatter;
}
In our example, there are two concrete implementations of Formatter available for the Spring container. As a result, Spring will throw a NoUniqueBeanDefinitionException exception when constructing the FooService: ........... We can avoid this by narrowing the implementation using a @Qualifier annotation:
public class FooService {
@Autowired
@Qualifier("fooFormatter")
private Formatter formatter;
}
When there are multiple beans of the same type, it's a good idea to use @Qualifier to avoid ambiguity.
Please note that the value of the @Qualifier annotation matches with the name declared in the @Component annotation of our FooFormatter implementation.
we use @Primary to give higher preference to a bean when there are multiple beans of the same type. we need @Primary to register multiple beans with same name.