项目工程 - microsphere-spring
核心代码 - io.microsphere.spring.context.event.ParallelBeanFactoryListener
指定 BeanDefinition class
指定 BeanDefinition class -> FactoryBean 实现
@Component AnnotatedBeanDefinition class = 配置类
BeanDefinition#setFactoryBean (可选)
- 如果存在的话,说明工厂方法是对象方法
- 如果存在的话,说明工厂方法是类方法
BeanDefinition#setFactoryMethod(可选)
Spring 3.0 +
Configuration class @Bean 方法声明
通过 AnnotatedBeanDefinition#getFactoryMethodMetadata() 获取 @Bean 方法信息
Spring 5.0 +
BeanDefinition 执行 org.springframework.beans.factory.support.AbstractBeanDefinition#setInstanceSupplier
该 Bean 通过 AbstractBeanDefinition#getInstanceSupplier() 方法返回 Supplier 来获取
SingletonBeanRegistry#registerSingleton(String,Object)
被注册 Singleton 对象的生命周期由 IoC 容器外部管理,该对象是一个已准备就绪的 Bean
手动注册,AbstractApplicationContext
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
...
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
...
}
public class SomeBean {
@Autowired
private ApplicationContext context; // 非 Spring Bean,而是 ResolvableDependency
// 非 Spring Bean,而是 ResolvableDependency
@Autowired
private ConfigurableApplicationContext applicationContext;
@Autowired
private EchoService echoService; // EchoService Spring Bean,通过类型依赖查找
@Autowired
@Qualifier("userService")
private UserService userService; // UserService Spring Bean,通过 @Qualifier 查找
@Autowird
private Map<String,UserService> userServices;
}
SomeBean 实际上依赖的 Spring Beans:EchoService 和 UserService,需要进一步分析出两个类型对应的 Bean 名称。
public MybatisAutoConfiguration(MybatisProperties properties,
ObjectProvider<Interceptor[]> interceptorsProvider,
ObjectProvider<TypeHandler[]> typeHandlersProvider,
ObjectProvider<LanguageDriver[]> languageDriversProvider,
ResourceLoader resourceLoader,
ObjectProvider<DatabaseIdProvider> databaseIdProvider,
ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider,
ObjectProvider<List<SqlSessionFactoryBeanCustomizer>> sqlSessionFactoryBeanCustomizers) {
this.properties = properties;
this.interceptors = interceptorsProvider.getIfAvailable();
this.typeHandlers = typeHandlersProvider.getIfAvailable();
this.languageDrivers = languageDriversProvider.getIfAvailable();
this.resourceLoader = resourceLoader;
this.databaseIdProvider = databaseIdProvider.getIfAvailable();
this.configurationCustomizers = configurationCustomizersProvider.getIfAvailable();
this.sqlSessionFactoryBeanCustomizers = sqlSessionFactoryBeanCustomizers.getIfAvailable();
}
Spring Bean 类型:
- MybatisProperties
- parameter.getParameterizedType() == MybatisProperties.class
- Interceptor - ObjectProvider<Interceptor[]>
- parameter.getParameterizedType() == ObjectProvider<Interceptor[]>>
- rawType = ObjectProvider.class
- actualTypeParameters =
- [0] = Interceptor[].class
- parameter.getParameterizedType() == ObjectProvider<Interceptor[]>>
- TypeHandler - ObjectProvider<TypeHandler[]>
- parameter.getParameterizedType() == ObjectProvider<TypeHandler[]>>
- rawType = ObjectProvider.class
- actualTypeParameters =
- [0] = TypeHandler[].class
- parameter.getParameterizedType() == ObjectProvider<TypeHandler[]>>
- LanguageDriver - ObjectProvider<LanguageDriver[]>
- DatabaseIdProvider - ObjectProvider
- ConfigurationCustomizer - ObjectProvider<List>
- parameter.getParameterizedType() == ObjectProvider<List>
- rawType = ObjectProvider.class
- actualTypeParameters =
- [0] = List
- rawType = List.class
- actualTypeParameters =
- [0] = ConfigurationCustomizer.class
- [0] = List
- parameter.getParameterizedType() == ObjectProvider<List>
- SqlSessionFactoryBeanCustomizer - ObjectProvider<List>
Spring ResovlableType: