Two Beans, One Type: Spring @Primary and @Qualifier
PostService asks for a Notifier and there are two: EmailNotifier and SmsNotifier. With no hint Spring stops; @Primary picks a default; @Qualifier("smsNotifier") asks by name and wins.
What you will learn
- Explain why two beans of one interface type stop Spring at startup
- Mark a default bean with @Primary
- Ask for a bean by name with @Qualifier on a constructor parameter
- Work out a bean's default name from its class name
Before you start
- Spring Autowiring
- Java interfaces
The blog can now send an email or a text message, so there are two notifiers. PostService asks for "a Notifier", not specifically an email or SMS one. Which one does Spring hand in? For this post we want the SMS.
One interface, two beans
package org.studyeasy.blog;
// Anything that can tell the readers about a new post.
public interface Notifier {
void send(String title);
}
An interface is one type that many classes can be. There's no @Component on it: an interface isn't a bean, the classes are.
package org.studyeasy.blog;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Component
@Primary
public class EmailNotifier implements Notifier {
public void send(String title) {
System.out.println("Email sent: " + title);
}
}
package org.studyeasy.blog;
import org.springframework.stereotype.Component;
@Component
public class SmsNotifier implements Notifier {
public void send(String title) {
System.out.println("SMS sent: " + title);
}
}
A bean's default name is its class name with a small first letter: emailNotifier and smsNotifier.
Fix it one line at a time
- No hint. The constructor is just
PostService(Notifier notifier). Spring stops with an error saying it expected a single matching bean but found 2:emailNotifierandsmsNotifier. Spring won't guess for you. - @Primary. Add
@PrimarytoEmailNotifier. That marks it as the default, the app runs, and you get "Email sent: Hello, Spring". - @Qualifier. Also add
@Qualifier("smsNotifier")on the constructor parameter. That asks for one bean by name, and now the app prints "SMS sent", even thoughEmailNotifieris still@Primary.
The final PostService
package org.studyeasy.blog;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class PostService {
private final Notifier notifier;
public PostService(@Qualifier("smsNotifier") Notifier notifier) {
this.notifier = notifier;
}
public void publish(String title) {
System.out.println("Post published: " + title);
notifier.send(title);
}
}
The field type is the interface, so any Notifier would do. The qualifier says "a Notifier, but please the one named smsNotifier". main doesn't choose anything; it starts Spring with BlogConfig (scanning finds three components: both notifiers and PostService), gets the service and publishes:
Post published: Hello, Spring
SMS sent: Hello, Spring
Spring makes both notifiers at startup. The EmailNotifier bean exists, it just isn't used in this run, because PostService named the other one.
Common mistakes
- Misspelling the qualifier name. It has to match the bean name exactly,
smsNotifierwith a small s. - Marking both classes
@Primary. Then there's no single default again. - Expecting
@Primaryto beat@Qualifier. A name asked for explicitly always wins.
Quick recap
| Situation | What Spring does |
|---|---|
| Two beans of one type, no hint | stops at startup: found 2 |
@Primary on one class | uses it as the default |
@Qualifier("name") on the parameter | uses the bean with that name |
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Why Spring in 2026: What the Container Does and What Spring Framework 7 Changed
Spring in one line: a container that creates your classes and hands each one what it needs. Plus the Spring Framework 7 baseline (JDK 17 to 25, Jakarta EE 11, Tomcat 11, JUnit 6), what got better than Spring 6, and a two-line version check.
A Plain Spring Project, No Boot: Your First Bean With spring-context
One dependency, one recipe, one getBean: a Maven project with spring-context 7.0.9, a Blog record, an AppConfig with @Bean, and a main that asks the container for the Blog. Then delete @Bean and meet NoSuchBeanDefinitionException.
Inversion of Control and Dependency Injection in Spring: One Program, Built Three Ways
A PostService needs a PostRepository. Build it three ways: the service calls new itself (tight coupling), main hands it in through the constructor (DI by hand), and the Spring container does the wiring from a @Configuration recipe.
Spring Component Scanning: @Component and @ComponentScan Instead of @Bean Methods
Instead of one @Bean method per class, put @Component on EmailNotifier and PostService and @ComponentScan on an empty BlogConfig. Spring walks the package, finds the stickers and builds the beans; main doesn't change.
