We use @Primary annotation to give a higher preference for a bean when there are multiple beans of the same type.
YouTube Video - @Primary Annotation
Spring @Primary Annotation Example
Let's create an example to demonstrate the use of @Primary annotation in a spring application.Create a Simple Maven Project
Project Structure
The pom.xml File
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>learn-spring-framework</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.1.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.8</version>
</dependency>
</dependencies>
</project>DataSource.java
package com.spring.core.assignment;
public interface DataSource {
String[] getEmails();
}MySQLDataSource.java
package com.spring.core.assignment;
import org.springframework.stereotype.Component;
@Component
public class MySQLDataSource implements DataSource{
@Override
public String[] getEmails() {
String[] emails = {"ramesh@gmail.com", "tony@gmail.com", "john@gmail.com"};
return emails;
}
}
PosgreSQLDataSource.java
package com.spring.core.assignment;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Component
@Primary
public class PostgreSQLDataSource implements DataSource{
@Override
public String[] getEmails() {
String[] emails = {"ramesh@gmail.com", "tony@gmail.com", "john@gmail.com"};
return emails;
}
}EmailService.java
package com.spring.core.assignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class EmailService {
private DataSource dataSource;
@Autowired
public EmailService(DataSource dataSource) {
this.dataSource = dataSource;
}
void sendEmail(){
String[] emails = dataSource.getEmails();
for(String email: emails){
System.out.println(email);
}
}
}
Annotation-Based Configuration - AppConfig.java
package com.spring.core.assignment;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.spring.core.assignment")
public class AppConfig {
}Running Spring Application - Application.java
Let's create a main class and run an application.package com.spring.core.assignment;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Client {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
EmailService emailService = applicationContext.getBean(EmailService.class);
emailService.sendEmail();
}
}ramesh@gmail.com
tony@gmail.com
john@gmail.com
The source code of this article is available on my GitHub repository https://github.com/RameshMF/spring-core-tutorial
Related Spring and Spring Boot Annotations
- Spring Boot @Bean Annotation Example
- Spring @Qualifier Annotation Example
- Spring @Autowired Annotation with Example
- Spring @Bean Annotation with Example
- Spring @Configuration Annotation with Example
- Spring @PropertySource Annotation with Example
- Spring @Import Annotation with Example
- Spring @ImportResource Annotation Example
- Spring - @Lazy Annotation Example
- Spring - @Primary Annotation Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @Repository Annotation
- Spring @Service Annotation
- The Spring @Controller and @RestController Annotations
- Spring Boot @Component, @Controller, @Repository and @Service
- Spring @Scope annotation with Prototype Scope Example
- Spring @Scope annotation with Singleton Scope Example
- Spring Boot @PathVariable
- Spring Boot @ResponseBody
- Spring @RequestBody - Binding Method Parameters to Request Body
- Spring Boot @ResponseStatus Annotation
- Spring Boot - Creating Asynchronous Methods using @Async Annotation
- @SpringBootTest Spring Boot Example
- @SpringBootTest vs @WebMvcTest
- @DataJpaTest Spring Boot Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
- Spring Boot @EnableAutoConfiguration Annotation with Example
- Spring Boot @SpringBootApplication Annotation with Example
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment