Skip to content

Files

Latest commit

 

History

History

2023.06.14 「小马哥技术周报」- 第五十六期:Spring 容器并行 Bean 生命周期处理实现

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

底层实现

代码实现

项目工程 - microsphere-spring
核心代码 - io.microsphere.spring.context.event.ParallelBeanFactoryListener

知识贮备

Spring 可依赖对象

Spring Bean 对象

Spring BeanDefinition

Spring Class 级别 BeanDefinition

指定 BeanDefinition class
指定 BeanDefinition class -> FactoryBean 实现
@Component AnnotatedBeanDefinition class = 配置类

Spring 工厂方法 BeanDefinition

BeanDefinition#setFactoryBean (可选)

  • 如果存在的话,说明工厂方法是对象方法
  • 如果存在的话,说明工厂方法是类方法

BeanDefinition#setFactoryMethod(可选)

  • 如果存在的话,Bean 会通过工厂方法来创建
Spring @Bean BeanDefinition

Spring 3.0 +
Configuration class @Bean 方法声明
通过 AnnotatedBeanDefinition#getFactoryMethodMetadata() 获取 @Bean 方法信息

Spring Suppiler BeanDefinition

Spring 5.0 +
BeanDefinition 执行 org.springframework.beans.factory.support.AbstractBeanDefinition#setInstanceSupplier
该 Bean 通过 AbstractBeanDefinition#getInstanceSupplier() 方法返回 Supplier 来获取

Spring 外部注册的 Singleton 对象

SingletonBeanRegistry#registerSingleton(String,Object)
被注册 Singleton 对象的生命周期由 IoC 容器外部管理,该对象是一个已准备就绪的 Bean

Spring ResolvableDependency

手动注册,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 类型:

  1. MybatisProperties
    1. parameter.getParameterizedType() == MybatisProperties.class
  2. Interceptor - ObjectProvider<Interceptor[]>
    1. parameter.getParameterizedType() == ObjectProvider<Interceptor[]>>
      1. rawType = ObjectProvider.class
      2. actualTypeParameters =
        1. [0] = Interceptor[].class
  3. TypeHandler - ObjectProvider<TypeHandler[]>
    1. parameter.getParameterizedType() == ObjectProvider<TypeHandler[]>>
      1. rawType = ObjectProvider.class
      2. actualTypeParameters =
        1. [0] = TypeHandler[].class
  4. LanguageDriver - ObjectProvider<LanguageDriver[]>
  5. DatabaseIdProvider - ObjectProvider
  6. ConfigurationCustomizer - ObjectProvider<List>
    1. parameter.getParameterizedType() == ObjectProvider<List>
      1. rawType = ObjectProvider.class
      2. actualTypeParameters =
        1. [0] = List
          1. rawType = List.class
          2. actualTypeParameters =
            1. [0] = ConfigurationCustomizer.class
  7. SqlSessionFactoryBeanCustomizer - ObjectProvider<List>

Spring ResovlableType:

  1. ResourceLoader

Bean 作用域

Singleton Scope Bean