SoFunction
Updated on 2025-03-03

Six ways to correctly inject interfaces in SpringBoot

In Spring Boot, it is a common requirement to properly inject these implementations where they need to be used when an interface has multiple implementations. Here are six ways to achieve this in Spring Boot:

1. Annotation with @Autowired and @Qualifier

This is the most direct way. Where the implementation of the injection interface is required, use@AutowiredAnnotation and pass@QualifierAnnotation specifies the specific implementation to be injected.

@Autowired  
@Qualifier("specificImplementation")  
private MyInterface myInterface;

Here, "specificImplementation" is what you use on some implementation class@Serviceor@ComponentThe name of the bean specified when annotating.

2. Use @Resource annotation

@ResourceAnnotations are injected by name (byName) by default. If a matching bean cannot be found, it is injected by type (byType). This way you can simplify the code because you don't need to use it explicitly@Qualifier

@Resource(name = "specificImplementation")  
private MyInterface myInterface;

3. Injection through construction method

Declare the interface implementation that needs to be injected in the constructor method, and Spring will automatically pass the corresponding implementation in when initializing the bean.

private final MyInterface myInterface;  
  
@Autowired  
public MyClass(MyInterface myInterface) {  
     = myInterface;  
}

This approach is type-safe and can be immediately checked in the constructor.

4. Use @Primary annotation

When you have multiple implementations, but one of them is primary or default, you can use it on that implementation@Primaryannotation. In this way, when Spring encounters multiple candidates during automatic assembly, the tag will be preferred.@PrimaryImplementation of .

@Primary  
@Service  
public class PrimaryImplementation implements MyInterface {  
    // Implementation method}

Then use it directly where it needs to be injected@Autowired

5. Use Java configuration classes

In Java configuration class, you can explicitly declare the bean to be injected and use@BeanAnnotation registers it into the Spring container.

@Configuration  
public class MyConfig {  
  
    @Bean  
    @Primary  
    public MyInterface primaryImplementation() {  
        return new PrimaryImplementation();  
    }  
  
    @Bean  
    public MyInterface secondaryImplementation() {  
        return new SecondaryImplementation();  
    }  
}

Where it needs to be injected, you can use it as before@Autowired, and if used@Primary, the main implementation will be automatically injected.

6. Inject all implementations into List or Map

Spring can inject all implementation classes of an interface into a List or Map. This is useful when you need to iterate through all implementations or choose implementations based on certain conditions.

@Autowired  
private List<MyInterface> allImplementations;  
  
// or  
@Autowired  
private Map<String, MyInterface> allImplementationsByName;

In this case, you don't need to use it on each implementation@Qualifieror@Primaryannotation. Spring will automatically inject all implementations into List or Map.

Summarize

The above six methods have their own advantages and disadvantages. Which method to choose depends on your specific needs and preferences. In most cases, use@Autowiredand@QualifierAnnotations are the most direct and flexible way. However, in some cases, use@PrimaryAnnotation, constructor injection, or Java configuration classes may be more suitable. Finally, injecting all implementations of the interface into List or Map provides a powerful way to handle multiple implementations.

This is the end of this article about six ways to correctly inject interfaces in SpringBoot. For more related content on SpringBoot multi-interface injection, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!