Spring Core Annotations
Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program. In the Spring framework, the annotations are classified into different types.
Types of Spring Framework Annotations
Basically, there are 6 types of annotation available in the whole spring framework.
- Spring Core Annotations
- Spring Web Annotations
- Spring Boot Annotations
- Spring Scheduling Annotations
- Spring Data Annotations
- Spring Bean Annotations
So in this article, we are going to discuss Spring Core Annotations.
Spring Core Annotations
Spring annotations present in the org.springframework.beans.factory.annotation and org.springframework.context.annotation packages are commonly known as Spring Core annotations. We can divide them into two categories:
- DI-Related Annotations
- Context Configuration Annotations
1. DI-Related Annotations
Some of the DI-Related Annotations that are available in Spring Framework are listed below
- @Lookup, etc.
@Autowired annotation is applied to the fields, setter methods, and constructors. It injects object dependency implicitly. We use @Autowired to mark the dependency that will be injected by the Spring container.
1.1: Field injection
1.2: Constructor injection
1.3: Setter injection
The @Qualifier annotation is used to resolve the autowiring conflict when there are multiple beans of the same type. The @Qualifier annotation can be used on any class annotated with @Component or on methods annotated with @Bean. This annotation can also be applied to constructor arguments or method parameters.
Suppose there are two beans, Car and Bike implements Vehicle interface
So in this case, if you want to inject the Bike bean in VehicleService then you must use @Autowired with @Qualifier annotation. If you didn’t use @Qualifier, it will throw NoUniqueBeanDefinitionException.
This indicates that a particular bean should be given preference when multiple beans are candidates to be autowired to a single-valued dependency. If exactly one ‘primary’ bean exists among the candidates, it will be the autowired value. Let’s understand this statement with an example
Example: In some cases, we need to register more than one bean of the same type. In this example Employee1() and Employee2() beans of the Employee type:
In this case, if we try to run the application Spring will throw NoUniqueBeanDefinitionException. To resolve this issue Spring offers the @Primary annotation.
In the above code, we mark Employee2() bean with @Primary. Spring will inject Employee2() bean preferentially over the Employee1().
2. Context Configuration Annotations
Some of the Context Configuration Annotations that are available in Spring Framework are listed below
- @PropertySource, etc.
If you want Spring to use a @Component class or a @Bean method only when a specific profile is active then you can mark it with @Profile.
Spring @Import annotation imports one or more @Configuration classes. It is equivalent to <import> element in Spring XML. If the application is using @ComponentScan, then all @Configuration classes will automatically be scanned and no need to use @Import annotation. But in some cases, we want to scan only selected @Configuration classes and in this case, @Import annotation is useful.
We can also import more than one configuration class as follows.
Spring @PropertySource annotation is used to provide properties file to Spring Environment. This annotation is used with @Configuration classes.
This annotation is repeatable, which means we can have multiple PropertySource on a Configuration class. This feature is available if you are using Java 8 or higher versions.