1 判断类型注入的属性判断其类型:
- Optional
- ObjectFactory
- ObjectProvider
- JAVAx.inject.Provider
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {private static Class<?> javaxInjectProviderClass;static {try {javaxInjectProviderClass =ClassUtils.forName("javax.inject.Provider", DefaultListableBeanFactory.class.getClassLoader());} catch (ClassNotFoundException ex) {javaxInjectProviderClass = null;}}public Object resolveDependency(DependencyDescriptor descriptor, @Nullable String requestingBeanName,@Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException {if (Optional.class == descriptor.getDependencyType()) {return createOptionalDependency(descriptor, requestingBeanName);} else if (ObjectFactory.class == descriptor.getDependencyType() ||ObjectProvider.class == descriptor.getDependencyType()) {return new DependencyObjectProvider(descriptor, requestingBeanName);} else if (javaxInjectProviderClass == descriptor.getDependencyType()) {return new Jsr330Factory().createDependencyProvider(descriptor, requestingBeanName);} else {// 判断是否有@Lazy注解 , 如果有该注解那么会创建代理对象 , 否则返回null// 总结:如果注入的属性有@Lazy注解 , 那么会返回由ProxyFactory对象创建的代理对象Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary(descriptor, requestingBeanName);if (result == null) {// 解析查找依赖注入的对象result = doResolveDependency(descriptor, requestingBeanName, autowiredBeanNames, typeConverter);}return result;}}}
2 查找依赖对象public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {public Object doResolveDependency(DependencyDescriptor descriptor, @Nullable String beanName,@Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException {InjectionPoint previousInjectionPoint = ConstructorResolver.setCurrentInjectionPoint(descriptor);try {Object shortcut = descriptor.resolveShortcut(this);if (shortcut != null) {return shortcut;}Class<?> type = descriptor.getDependencyType();// 判断需要注入的类型是否是一个集合(如何:List<Users> beans)// 2.1Object multipleBeans = resolveMultipleBeans(descriptor, beanName, autowiredBeanNames, typeConverter);if (multipleBeans != null) {return multipleBeans;}// 查找与所需类型匹配的bean实例// 2.2Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor);if (matchingBeans.isEmpty()) {if (isRequired(descriptor)) {rAIseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor);}return null;}String autowiredBeanName;Object instanceCandidate;// 如果找到多个if (matchingBeans.size() > 1) {// 在这里会判断多个Bean中是否有一个@Primary注解的Bean// 如果没有@Primary , 继续获取是否有@Priority(优先级)注解// 如果没有则返回null , 如果有则返回值越小的优先级越高(如果有多个优先级一样 , 则系统报错)// 如果上面都返回的null , 那么最后会通过beanName进行匹配autowiredBeanName = determineAutowireCandidate(matchingBeans, descriptor);if (autowiredBeanName == null) {if (isRequired(descriptor) || !indicatesMultipleBeans(type)) {// 如果是必须的 或者 不是集合类型的Bean注入;那么这里抛出异常return descriptor.resolveNotUnique(descriptor.getResolvableType(), matchingBeans);} else {return null;}}instanceCandidate = matchingBeans.get(autowiredBeanName);} else {// We have exactly one match.Map.Entry<String, Object> entry = matchingBeans.entrySet().iterator().next();autowiredBeanName = entry.getKey();instanceCandidate = entry.getValue();}if (autowiredBeanNames != null) {autowiredBeanNames.add(autowiredBeanName);}if (instanceCandidate instanceof Class) {instanceCandidate = descriptor.resolveCandidate(autowiredBeanName, type, this);}Object result = instanceCandidate;if (result instanceof NullBean) {if (isRequired(descriptor)) {raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor);}result = null;}if (!ClassUtils.isAssignableValue(type, result)) {throw new BeanNotOfRequiredTypeException(autowiredBeanName, type, instanceCandidate.getClass());}return result;} finally {ConstructorResolver.setCurrentInjectionPoint(previousInjectionPoint);}}}
2.1 判断集合类型判断需要注入的属性的数据类型是否是如下几种 。- StreamDependencyDescriptor
- Array
推荐阅读
- RestTemplate详解 Springboot — 用更优雅的方式发HTTP请求
- 谷雨养生知识详解 谷雨养生知识详解图
- ELK 处理 Spring Boot 日志,不错!
- Spring中这两个对象ObjectFactory与FactoryBean接口你使用过吗?
- 保护Linux文件和目录的秘密武器:chattr命令详解
- Spring定义Controller接口的这些方式你肯定不知道
- 钓鱼必杀技:盒饭钓法详解,野钓大鲫鱼第一利器
- SpringBoot实现多数据源配置详解
- 使用了Spring的事件机制真香!
- 华为Mate60系列支持《王者荣耀》高帧率模式