0
0
SpringbootConceptBeginner · 3 min read

@Primary in Spring: What It Is and When to Use It

@Primary is a Spring annotation used to mark a bean as the default choice when multiple beans of the same type exist. It tells Spring which bean to inject automatically without needing extra qualifiers.
⚙️

How It Works

Imagine you have several friends who can help you with a task, but you want to pick your favorite one by default. In Spring, when multiple beans of the same type exist, it gets confused about which one to use. The @Primary annotation acts like your favorite friend’s name tag, telling Spring, "Pick me first!"

When Spring’s dependency injection finds more than one bean for a type, it looks for the one marked with @Primary. If found, it injects that bean automatically, avoiding errors or the need to specify which bean to use every time.

💻

Example

This example shows two beans of the same type, but one is marked with @Primary. Spring will inject the primary bean by default.

java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

interface GreetingService {
    String greet();
}

class EnglishGreetingService implements GreetingService {
    public String greet() {
        return "Hello!";
    }
}

class SpanishGreetingService implements GreetingService {
    public String greet() {
        return "¡Hola!";
    }
}

@Configuration
class AppConfig {
    @Bean
    @Primary
    public GreetingService englishGreetingService() {
        return new EnglishGreetingService();
    }

    @Bean
    public GreetingService spanishGreetingService() {
        return new SpanishGreetingService();
    }
}

// Usage in a Spring component
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;

@Component
class Greeter {
    private final GreetingService greetingService;

    @Autowired
    public Greeter(GreetingService greetingService) {
        this.greetingService = greetingService;
    }

    public void sayHello() {
        System.out.println(greetingService.greet());
    }
}
Output
Hello!
🎯

When to Use

Use @Primary when you have multiple beans of the same type and want to set a default one for injection. This avoids confusion and errors when Spring tries to pick a bean automatically.

For example, if you have different implementations of a service for testing and production, mark the production one as @Primary so it is used by default. You can still override it with qualifiers if needed.

Key Points

  • @Primary marks a bean as the default choice for injection.
  • It helps Spring decide which bean to use when multiple candidates exist.
  • You can still use qualifiers to override the primary bean.
  • It simplifies configuration and avoids injection errors.

Key Takeaways

@Primary sets the default bean when multiple beans of the same type exist.
It helps Spring avoid confusion during dependency injection.
Use it to simplify your configuration and reduce the need for qualifiers.
You can override the primary bean with explicit qualifiers if needed.