Implementing the Strategy Pattern in Spring with Built‑in @Autowired
The article shows how to replace bulky if‑else logic in Spring Boot by using the strategy pattern together with a factory that autowires all Strategy implementations into a Map, explains the @Autowired semantics for collections, and provides concrete code examples.
In a Spring Boot project, the author demonstrates how to avoid extensive if‑else statements by combining the strategy pattern with a factory that selects a concrete rule based on an enum key.
Factory with @Autowired Map
The SignalLightRulesFactory is annotated with @Component and declares a field
Map<String, SignalLightRules> signalLightRulesStrategy;which is marked with @Autowired. Spring automatically injects every bean that implements the SignalLightRules interface; the map key is the bean name (the value of the @Component annotation) and the value is the corresponding strategy instance. The factory method simply looks up the map:
public SignalLightRules getSignalLightRules(String signalLightKey) {
return signalLightRulesStrategy.get(signalLightKey);
}Strategy Interface and Example Implementation
The strategy interface is defined as
public interface SignalLightRules {
List<SignalrightDevsDTO> getSignalrightDevsDtoList(String mineApiCode);
}An example implementation is annotated with a custom name:
@Component(Constants.SIFANGJISIGNAL)
public class SiFangJiSignalLightRules implements SignalLightRules {
@Override
public List<SignalrightDevsDTO> getSignalrightDevsDtoList(String mineApiCode) {
List<SignalrightDevsDTO> signalrightDevsDtoList = null;
// ... omitted logic ...
return signalrightDevsDtoList;
}
}How @Autowired Works for Collections
The article extracts the Javadoc of org.springframework.beans.factory.annotation.Autowired, highlighting that when the injection point is an array, java.util.Collection, or java.util.Map, the container autowires all matching beans. For a map, the keys must be of type String and are resolved to bean names. Ordering follows @Order or Ordered values if present.
Key Points from the Annotation Source
The annotation can be placed on constructors, fields, setter methods, config methods, and (since Spring 5.0) on individual parameters. The required attribute defaults to true, controlling whether missing beans cause a failure. Autowiring is performed by AutowiredAnnotationBeanPostProcessor, which cannot be used inside BeanPostProcessor or BeanFactoryPostProcessor implementations.
Finally, the author notes that the same injection technique works for arrays and other collection types, and points readers to the source code of @Autowired for deeper understanding.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
The Dominant Programmer
Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
