Springboot加载bean查找父类

Springboot加载bean的时候,有时候有些类不在spring.factory文件中,但是声明的@Bean方法却能加载bean,spring是怎么处理的呢,我们以spring-webflux包中的WebFluxConfigurationSupport类为例说明,这个类有许多的@bean方法,其中之一:

 

	@Bean
	public DispatcherHandler webHandler() {
		return new DispatcherHandler();
	}

但是这个类有一个子类:EnableWebFluxConfiguration,这个类是WebFluxAutoConfiguration的内部类,而WebFluxAutoConfiguration类在Spring.factory中,所以能被加载到。

具体源码分析如下:在doCreateBean方法中有如下方法:

instanceWrapper = createBeanInstance(beanName, mbd, args);

因为是通过method构建的,会走如下方法:

		if (mbd.getFactoryMethodName() != null)  {
			return instantiateUsingFactoryMethod(beanName, mbd, args);
		}

最后真正获取父类的逻辑,是在ConstructorResolver类的instantiateUsingFactoryMethod方法中的getCandidateMethods方法。

最后会进入ReflectionUtils的doWithMethods方法:

	public static void doWithMethods(Class<?> clazz, MethodCallback mc, @Nullable MethodFilter mf) {
		// Keep backing up the inheritance hierarchy.
		Method[] methods = getDeclaredMethods(clazz);
		for (Method method : methods) {
			if (mf != null && !mf.matches(method)) {
				continue;
			}
			try {
				mc.doWith(method);
			}
			catch (IllegalAccessException ex) {
				throw new IllegalStateException("Not allowed to access method '" + method.getName() + "': " + ex);
			}
		}
		if (clazz.getSuperclass() != null) {
			doWithMethods(clazz.getSuperclass(), mc, mf);
		}
		else if (clazz.isInterface()) {
			for (Class<?> superIfc : clazz.getInterfaces()) {
				doWithMethods(superIfc, mc, mf);
			}
		}
	}

这是个递归方法,doWithMethods(clazz.getSuperclass(), mc, mf);这一行就是获取父类进行了递归。

时序图如下:

同时WebFluxConfigurationSupport这个类,我们需要的HandlerMapping,HandlerAdapter,ViewResolver等类都在这个类的@Bean方法中创建

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值