Spring - @Primary Annotation Example

In this quick article, we’ll discuss Spring’s @Primary annotation which was introduced with version 3.0 of the framework.
Simply put, we use @Primary to give higher preference to a bean when there are multiple beans of the same type.
Let’s describe the problem in detail.

Why is @Primary Needed?

In some cases, we need to register more than one bean of the same type.
In this example we have mySQLConnection() and oracleConnection() beans of the Connection type:
public class Config {
    public Connection mySQLConnection() {
        return new MySQLConnection();
    public Connection oracleConnection() {
        return new OracleConnection();
Spring throws NoUniqueBeanDefinitionException if we try to run the application.
To access beans with the same type we usually use @Qualifier(“beanName”) annotation.
We apply to at the injection point along with @Autowired. In our case, we select the beans at the configuration phase so @Qualifier can’t be applied here. We can learn more about @Qualifier annotation by following the link.
To resolve this issue Spring offers the @Primary annotation. The following example shows how to use @Primary annotation in a spring application.
The @Primary annotation may be used on any class directly or indirectly annotated with @Component or on factory methods annotated with @Bean. In this example, we will use @Primary annotation with @Component annotation.

Spring @Primary Annotation Example

Let's create an example to demonstrates usage of use @Primary annotation in a spring application.

Tools and technologies used

  • Spring Framework - 5.1.0.RELEASE
  • JDK - 8 or later
  • Maven - 3.2+
  • IDE - Eclipse Mars/STS

Create a Simple Maven Project

Create a simple maven project using your favorite IDE and refer below section for packaging structure. If you are new to maven then read this article How to Create a Simple Maven Project.

Project Structure

Below diagram shows a project structure for your reference -

The pom.xml File

<project xmlns="http://maven.apache.org/POM/4.0.0"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


     <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
Next, consider the following MessageService interface.


package net.javaguides.spring.primary;

public interface MessageService {
 public void sendMsg(); 
Create three beans named as FacebookMessageService, EmailMessageService, and TwitterMessageService, which implement the MessageService interface.


package net.javaguides.spring.primary;

import org.springframework.stereotype.Component;

public class FacebookMessageService implements MessageService {
    public void sendMsg() {
        System.out.println("FacebookMessageService implementation here");


package net.javaguides.spring.primary;

import org.springframework.stereotype.Component;

public class EmailMessageService implements MessageService {

    public void sendMsg() {
        System.out.println("EmailMessageService Implementation here");


package net.javaguides.spring.primary;

import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

public class TwitterMessageService implements MessageService {
    public void sendMsg() {
        System.out.println("TwitterMessageService Implementation here");
Note that in the above TwitterMessageService class we have added @Primary with @Component annotation.

Annotation Based Configuration - AppConfig.java

package net.javaguides.spring.primary;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@ComponentScan(basePackages = "net.javaguides.spring.primary")
public class AppConfig {

Running Spring Application - Application.java

Let's create a main class and run an application.
package net.javaguides.spring.primary;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Application {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        MessageService messageService = context.getBean(MessageService.class);


TwitterMessageService Implementation here
The source code of this article is available on my GitHub repository https://github.com/RameshMF/spring-core-tutorial

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours

Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course