spring 源码解析:BeanFactory

学习spring源码是一件非常煎熬且很有挑战的事情,在我们日常的工作中用的最多的框架可能就是spring及其的拓展,这是一个极其庞大的家族,首先我们来先来学习spring源码,然后在它的基础上学习spring mvc ,springboot,spring colud等。

1、 BeanFactory 对bean的生命周期进行管理,生产各种所需要的bean供我们使用

BeanFactory是一个接口,它的子接口跟实现有:
AnnotationConfigApplicationContext 是用来管理注解bean的容器;GenericApplicationContext 通用应用上下文; AnnotationConfigRegistry 注解配置注册表,ListableBeanFactory根据各种条件获取bean的配置清单;ConfigurableBeanFactory提供配置factory的各种方法

2、BeanFactoryPostProcessor:对beanFactory进行操作更新的类;

@FunctionalInterface
public interface BeanFactoryPostProcessor {

 /**
   * Modify the application context's internal bean factory after its standard
   * initialization. All bean definitions will have been loaded, but no beans
   * will have been instantiated yet. This allows for overriding or adding
   * properties even to eager-initializing beans.
   * @param beanFactory the bean factory used by the application context
   * @throws org.springframework.beans.BeansException in case of errors
   */
  void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;


}

BeanFactoryPostProcessor 就一个方法 postProcessBeanFactory,看一下他的实现ConfigurationClassPostProcessor

class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPostProcessor,
    PriorityOrdered, ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware {

ConfigurationClassPostProcessor 实现了好几个接口,我们简单说一下:BeanDefinitionRegistryPostProcessor:BeanFactoryPostProcessor的子接口,在常规的BeanFactoryPostProcessor检测之前注册bean的定义信息

PriorityOrdered:设置优先级

ResourceLoaderAware:设置资源加载器,可以获取到外部资源文件

BeanClassLoaderAware:设置类加载器

EnvironmentAware:设置环境变量

/**
   * 添加CGLIB增强处理及ImportAwareBeanPostProcessor后置处理类
   *
   * Prepare the Configuration classes for servicing bean requests at runtime
   * by replacing them with CGLIB-enhanced subclasses.
   */
  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    int factoryId = System.identityHashCode(beanFactory);
    if (this.factoriesPostProcessed.contains(factoryId)) {
      throw new IllegalStateException(
          "postProcessBeanFactory already called on this post-processor against " + beanFactory);
    }
    this.factoriesPostProcessed.add(factoryId);
    if (!this.registriesPostProcessed.contains(factoryId)) {


//构建和验证一个类是否被@Configuration修饰,并做相关的解析工作
      processConfigBeanDefinitions((BeanDefinitionRegistry) beanFactory);
    }
  //增强配置
    enhanceConfigurationClasses(beanFactory);
    //添加后置处理类
    beanFactory.addBeanPostProcessor(new ImportAwareBeanPostProcessor(beanFactory));
  }

3、BeanDefinition:bean的定义描述信息;

从加载配置文件后生成bean定义信息,主要就是设置类名,作用域,单例还是原型,是否懒加载等

4、 beanPostProcessor:对初始化前后进行处理的类;

主要就两个方法

public interface BeanPostProcessor {

/**
    * 初始化方法调用前要进行的处理逻辑
    *
    * Apply this {@code BeanPostProcessor} to the given new bean instance <i>before</i> any bean
    * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
    * or a custom init-method). The bean will already be populated with property values.
    * The returned bean instance may be a wrapper around the original.
    * <p>The default implementation returns the given {@code bean} as-is.
    * @param bean the new bean instance
    * @param beanName the name of the bean
    * @return the bean instance to use, either the original or a wrapped one;
    * if {@code null}, no subsequent BeanPostProcessors will be invoked
    * @throws org.springframework.beans.BeansException in case of errors
    * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
    */
   @Nullable
   default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
      return bean;
   }
  /**
    * 在初始化方法指定后要进行的处理逻辑
    *
    * Apply this {@code BeanPostProcessor} to the given new bean instance <i>after</i> any bean
    * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
    * or a custom init-method). The bean will already be populated with property values.
    * The returned bean instance may be a wrapper around the original.
    * <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
    * instance and the objects created by the FactoryBean (as of Spring 2.0). The
    * post-processor can decide whether to apply to either the FactoryBean or created
    * objects or both through corresponding {@code bean instanceof FactoryBean} checks.
    * <p>This callback will also be invoked after a short-circuiting triggered by a
    * {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
    * in contrast to all other {@code BeanPostProcessor} callbacks.
    * <p>The default implementation returns the given {@code bean} as-is.
    * @param bean the new bean instance
    * @param beanName the name of the bean
    * @return the bean instance to use, either the original or a wrapped one;
    * if {@code null}, no subsequent BeanPostProcessors will be invoked
    * @throws org.springframework.beans.BeansException in case of errors
    * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
    * @see org.springframework.beans.factory.FactoryBean
    */
   @Nullable
   default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
      return bean;
   }

想学习源码的同学可以加一个关注,将推出一系列源码文章!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值