Spring @Primary Annotation Example

In this section we will learn about @Primary Annotation.

When there are multiple beans of the same data-type in Spring containerall of them are qualified to be autowired to single-valued dependency. That causes ambiguity and leads to throw an exception by framework. To avoid this issue, we can use the Spring-specific @Primary annotation that automatically gives the higher preference to a particular bean. 

This annotation can be used on any class annotated with the @Component, @Service & @Repository annotation or on methods annotated with the @Bean annotation.

There should be only one @Primary bean among same type of beans.


Using @Primary with @Bean

Create MessageService interface for message service implementations.

public interface MessageService {

void printMessaage();

}

Create implementations - SMSService and EmailService classes.  

public class SMSService implements MessageService{

@Override
public void printMessaage() {
System.out.println("SMS Service");
}
}


public class EmailService implements  MessageService{

@Override
public void printMessaage() {
System.out.println("Email Service");
}
}

Here is a Configuration class where we have defined a @Bean method for SMSService and EmailService classes. Here we defined @Primary annotation for SMSService bean.

@Configuration
public class AppConfig {

@Bean
@Primary
public MessageService smsService() {
return new SMSService();
}

@Bean
public MessageService emailService() {
return new EmailService();
}
}


Using @Primary with @Service

Create RoleService interface for message service implementations.

public interface RoleService {

void printRole();
}

Create implementations - AdminRoleService and UserRoleService classes. Here we defined @Primary annotation for AdminRoleService bean.

@Service
@Primary
public class AdminRoleService implements RoleService {
@Override
public void printRole() {
System.out.println("Admin Role");
}
}


@Service
public class UserRoleService implements RoleService {
@Override
public void printRole() {
System.out.println("User Role");
}
}


The following example creates a Spring Boot application which uses @Primary annotation.

Project Directory


pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.dev.demo</groupId>
<artifactId>spring-primary-annotation-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-primary-annotation-example</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>


MessageService.java

package com.knf.dev.demo.service;

public interface MessageService {

void printMessaage();

}


SMSService.java

package com.knf.dev.demo.service;

public class SMSService implements MessageService{

@Override
public void printMessaage() {
System.out.println("SMS Service");
}
}


EmailService.java

package com.knf.dev.demo.service;

public class EmailService implements MessageService{

@Override
public void printMessaage() {
System.out.println("Email Service");
}
}


Configuration - AppConfig.java

package com.knf.dev.demo.config;

import com.knf.dev.demo.service.EmailService;
import com.knf.dev.demo.service.MessageService;
import com.knf.dev.demo.service.SMSService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class AppConfig {

@Bean
@Primary
public MessageService smsService() {
return new SMSService();
}

@Bean
public MessageService emailService() {
return new EmailService();
}
}


RoleService.java

package com.knf.dev.demo.service;

public interface RoleService {

void printRole();
}


AdminRoleService.java

package com.knf.dev.demo.service;

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

@Service
@Primary
public class AdminRoleService implements RoleService {
@Override
public void printRole() {
System.out.println("Admin Role");
}
}


UserRoleService.java

package com.knf.dev.demo.service;

import org.springframework.stereotype.Service;

@Service
public class UserRoleService implements RoleService {
@Override
public void printRole() {
System.out.println("User Role");
}
}


Run the application - Application.java

package com.knf.dev.demo;

import com.knf.dev.demo.service.MessageService;
import com.knf.dev.demo.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application implements CommandLineRunner {

@Autowired
MessageService messageService;

@Autowired
RoleService roleService;

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Override
public void run(String... args) throws Exception {
messageService.printMessaage();
roleService.printRole();
}
}

Application is the entry point that sets up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning.

Let's run this Spring boot application from either IntelliJ IDEA IDE by right click - Run 'Application.main()'
Or you can use the below maven command to run:

mvn spring-boot:run

Console Output:
SMS Service
Admin Role


Download Source Code

More related topics,

Spring Web Annotations


Spring Core Annotations


Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - DES Encryption and Decryption example

Java - Blowfish Encryption and decryption Example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Top 5 Java ORM tools - 2024

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String