Home » Java » Enterprise Java » Spring @Primary Annotation

About Shubhra Srivastava

Shubhra Srivastava
Shubhra is a software professional and founder of ProgrammerGirl. She has a great experience with Java/J2EE technologies and frameworks. She loves the amalgam of programming and coffee :)

Spring @Primary Annotation


Spring @Primary annotation is used to give a higher preference to the marked bean when multiple beans of the same type exist.

Spring, by default, auto-wires by type. And so, when Spring attempts to autowire and there are multiple beans of the same type, we’ll get a NoUniqueBeanDefinitionException:

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException
  : No qualifying bean of type [com.programmergirl.Person]
  is defined: expected single matching bean but found 2: student, teacher

To solve this, we can choose to use Spring @Primary annotation, thereby marking one bean to be the primary one.

In this tutorial, we’ll explore the usage of this annotation in more detail.

@Primary In Configuration Class:

Let’s say we have the following configuration class:

public class UniversityConfig {
    public Person student() {
        return new Student();
    public Person teacher() {
        return new Teacher();

Both Teacher and Student beans inherit from Person and so we have marked it as the return type of both of our @Bean annotated method.

However, please note that we have marked the Student bean to be the primary one using @Primary annotation. Now, let’s start up our application:

AnnotationConfigApplicationContext context =
Person student = context.getBean(Person.class);

We’ll see that a Student object got a preference while Spring attempted autowiring.

Spring Component With @Primary:

Let’s say we instead have our component scan enabled:

public class UniversityConfig {   

For such cases, we can directly annotate our Spring component class with @Primary:

public class Student implements Person {
public class Teacher implements Person {

Now, when directly trying to inject a Person type without a @Qualifier, a Student bean will get injected:

public class StudentService {
    // Student bean is primary and so it'll get injected
    private Person student;
    public void printStudentDetails() {


In this quick tutorial, we explored the usages of @Primary annotation in Spring.

As the name suggests, we can use @Primary annotation to define a primary one when having multiple beans of the same type.

Published on Java Code Geeks with permission by Shubhra Srivastava, partner at our JCG program. See the original article here: Spring @Primary Annotation

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments