In this article we will learn about the @Primary Annotation in Spring with a very simple example.
Let’s first try to understand why and when we need this @Primary annotation. In real life scenarios, we encounter use cases, where we find multiple auto-wiring candidates for the same type. In these cases, we can use @Primary annotation to give preference to a particular bean, so that it becomes the autowired value by default. We can use @Qualifier annotation for autowiring other non primary beans.
We will take a simple example of a Car. Hatchback, Sedan and Van being the types of Car.
Here, We will assign the precedence to Hatchback bean by annotating it with @Primary.
Let’s get started then without much ado.
Technology Stack
Project Structure
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringPrimary</groupId>
<artifactId>SpringPrimary</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
package com.spring.beans;
public interface Car {
String type();
}
package com.spring.beans;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Component
@Primary
public class Hatchback implements Car{
@Override
public String type() {
return "Hatchback";
}
}
package com.spring.beans;
import org.springframework.stereotype.Component;
@Component
public class Sedan implements Car{
@Override
public String type() {
return "Sedan";
}
}
package com.spring.beans;
import org.springframework.stereotype.Component;
@Component
public class Van implements Car{
@Override
public String type() {
return "Van";
}
}
package com.spring.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.spring.beans.Car;
@SpringBootApplication(scanBasePackages="com.spring.beans")
public class Application implements CommandLineRunner{
// This will inject the bean of the component which is marked with @Primary
@Autowired
private Car car;
//Wiring Sedan bean by using Qualifier
@Autowired
@Qualifier("sedan")
private Car sedan;
//Wiring Van bean by using Qualifier
@Autowired
@Qualifier("van")
private Car van;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(car.type());
System.out.println(sedan.type());
System.out.println(van.type());
System.exit(1);
}
}
Hatchback
Sedan
Van
Download Code
Related Articles