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方法中创建