栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot源码(十二)一个Bean实现多个接口,注入时源码是怎样的?

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot源码(十二)一个Bean实现多个接口,注入时源码是怎样的?

在用Spring框架写Service层服务的时候,我们一般都习惯定义一个service接口,再定义一个service实现类,在引用的时候我们就通过一个@Resource注解或者@Autowired注解,此时Spring会根据名称优先或类型优先注入Spring的Bean。

如果一个service实现类,实现了两个Service接口,Spring是怎么处理注入的呢?

一、代码示例

我们就通过如下代码示例,看看一个Service实现类实现两个接口源码是怎么加载的:

public class AchievementController {
    @Resource
    private TestTwoApiService testTwoApiService;
@Service
@Slf4j
public class AchievementServiceImpl implements AchievementService, TestTwoApiService {
二、注入的源码

我们知道Bean的属性注入,会通过:

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean

三、不同注解方式的处理 1、@Resource注解

进入到:

org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#autowireResource

通过name获取bean走的是这段代码:

			if (this.fallbackToDefaultTypeMatch && element.isDefaultName && !factory.containsBean(name)) {
				autowiredBeanNames = new linkedHashSet<>();
				resource = beanFactory.resolveDependency(descriptor, requestingBeanName, autowiredBeanNames, null);
				if (resource == null) {
					throw new NoSuchBeanDefinitionException(element.getLookupType(), "No resolvable resource object");
				}
			}
			else {
				resource = beanFactory.resolveBeanByName(name, descriptor);
				autowiredBeanNames = Collections.singleton(name);
			}

我们知道BeanFactory存放Bean的beanName是什么?是Bean的类名的首字母小写,很遗憾,最后是通过遍历所有的bean,才匹配到:testTwoApiService。

	private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit) {
		List result = new ArrayList<>();

		// Check all bean definitions.
		for (String beanName : this.beanDefinitionNames) {

这个流程和我们平时一个Bean的时候注入接口类是一致的,都要进行bean的遍历。

2、@Autowired注解

上面的:

org.springframework.beans.factory.support.DefaultListableBeanFactory#doGetBeanNamesForType
方法走的也是@Autowired注解的方法

区别是在populateBean方法后,走的是这块方法:

org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#postProcessProperties
四、总结

,所以就算一个Bean有多个接口,其注入的@Resource注解和@Autowired注解,源码的路径是不变的。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/696620.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号