Skip to content
Related Articles

Related Articles

Spring Core Annotations

View Discussion
Improve Article
Save Article
  • Last Updated : 02 Feb, 2022
View Discussion
Improve Article
Save Article

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.

  1. Spring Core Annotations
  2. Spring Web Annotations
  3. Spring Boot Annotations
  4. Spring Scheduling Annotations
  5. Spring Data Annotations
  6. 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:

  1. DI-Related Annotations
  2. Context Configuration Annotations

1. DI-Related Annotations

Some of the DI-Related Annotations that are available in Spring Framework are listed below

  • @Autowired
  • @Qualifier
  • @Primary
  • @Bean
  • @Lazy
  • @Required
  • @Value
  • @Scope
  • @Lookup, etc.

@Autowired Annotation

@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.

Examples

1.1: Field injection

Java




class Student {
    @Autowired
    Address address;
}

1.2: Constructor injection

Java




class Student {
    Address address;
  
    @Autowired
    Student(Address address) {
        this.address = address;
    }
}

1.3: Setter injection

Java




class Student {
    Address address;
  
    @Autowired
    void setaddress(Address address) {
        this.address = address;
    }
}

@Qualifier Annotation

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.

Illustration:

Java




public interface Vehicle {
     public void start();
}

Implementation: 

Suppose there are two beans, Car and Bike implements Vehicle interface

Example:

Java




@Component(value="car")
public class Car implements Vehicle {
  
     @Override
     public void start() {
           System.out.println("Car started");
     }
  
 }
  
@Component(value="bike")
public class Bike implements Vehicle {
  
     @Override
     public void start() {
          System.out.println("Bike started");
     }
  
}

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.

Example:

Java




@Component
public class VehicleService {
  
    @Autowired
    @Qualifier("bike")
    private Vehicle vehicle;
  
    public void service() {
         vehicle.start();
    }
}

@Primary Annotation

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:

Java




@Configuration
public class Config {
  
    @Bean
    public Employee Employee1() {
        return new Employee("Employee1");
    }
  
    @Bean
    public Employee Employee2() {
        return new Employee("Employee2");
    }
}

In this case, if we try to run the application Spring will throw NoUniqueBeanDefinitionException. To resolve this issue Spring offers the @Primary annotation.

Java




@Configuration
public class Config {
  
    @Bean
    public Employee Employee1() {
        return new Employee("Employee1");
    }
  
    @Bean
      @Primary
    public Employee Employee2() {
        return new Employee("Employee2");
    }
}

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

  • @Profile
  • @Import
  • @ImportResource
  • @PropertySource, etc.

@Profile Annotation

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. 

Example

Java




@Component
@Profile("developer")
public class Employee {}

@Import Annotation

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.

Example:

Java




@Configuration
@Import(Conf1.class)
public class Config {
   ------
}

We can also import more than one configuration class as follows.

Java




@Configuration
@Import({Conf1.class, Conf2.class})
public class Config {
   ------
}

@PropertySource Annotation

Spring @PropertySource annotation is used to provide properties file to Spring Environment. This annotation is used with @Configuration classes. 

Example:

Java




@Configuration
@PropertySource("classpath:db.properties")
public class DBConfiguration {
    //...
}

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. 

Example:

Java




@Configuration
@PropertySource("classpath:db.properties")
@PropertySource("classpath:root.properties")
public class DBConfiguration {
    //...
}


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!