A, pre –

@Autowired
private List<IUserService> list;
@Autowired
private Map<String,IUserService> map;
Copy the code

Two implementation classes for IUserService are automatically injected into the mapAnd that’s one of the questions that a lot of students ask, how does this work? Where did you configure or override methods to inject the implementation class of the corresponding interface into the Map or List collection? In fact, the Spring framework has already wrapped it for us, so we don’t need any additional operations.

Second, the principle of

When Spring scans the @AutoWired annotation, it will inject the corresponding bean in the current class through reflection, and inject the collection of beans through type-lookup when it is a collection.

Three, the source code

Entrance org. Springframework. Beans. Factory. The annotation. AutowiredAnnotationBeanPostProcessor. AutowiredFieldElement# inject

Continue to org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveDependency

In-depth org. Springframework. Beans. Factory. Support. DefaultListableBeanFactory# doResolveDependency

The target method org. Springframework. Beans. Factory. Support. DefaultListableBeanFactory# resolveMultipleBeans array related implementation

List related implementation

Map dependent implementation

conclusion

Learn endless, only understand the bottom and in-depth analysis of the source code to better use.