spring源码解析一:bean

2023-02-05 0 1,050

自学spring源代码是两件十分折磨且很有考验的事,在他们日常生活的组织工作时用的最多的架构可能将是spring或其的开拓,这是两个极为巨大的家族企业,具体来说他们来嘿嘿自学spring源代码,接着在它的基础上自学spring mvc ,springboot,spring colud等。

spring源码解析一:bean

1、 BeanFactory 对bean的开发周期展开管理组织工作,制造各式各样所须要的bean供他们采用

BeanFactory是两个USB,它的子USB跟同时实现有:

AnnotationConfigApplicationContext 是用以管理组织工作注释bean的罐子;GenericApplicationContext 通用型应用领域语句; Annota种方式

2、BeanFactoryPostProcessor:对beanFactory展开操作方式预览的类;

@FunctionalInterface public interface BeanFactoryPostProcessor { /** * Modify the application contexts 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. *@parambeanFactory 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 同时实现了好几个USB,他们简单说一下:BeanDefinitionRegistryPostProcessor:BeanFactoryPostProcessor的子USB,在常规的BeanFactoryPostProcessor检测之前注册bean的定义信息

PriorityOrdered:设置优先级

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 {@codeBeanPostProcessor} to the given new bean instance <i>before</i> any bean * initialization callbacks (like InitializingBeans {@codeafterPropertiesSet} * 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 {@codebean} 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 {@codenull}, no subsequent BeanPostProcessors will be invoked *@throwsorg.springframework.beans.BeansException in case of errors *@seeorg.springframework.beans.factory.InitializingBean#afterPropertiesSet */ @Nullable defaultObject postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean; } /** * 在初始化方式指定后要展开的处理逻辑 * * Apply this {@codeBeanPostProcessor} to the given new bean instance <i>after</i> any bean * initialization callbacks (like InitializingBeans {@codeafterPropertiesSet} * 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 {@codebean instanceof FactoryBean} checks. * <p>This callback will also be invoked after a short-circuiting triggered by a * {@linkInstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method, * in contrast to all other {@codeBeanPostProcessor} callbacks. * <p>The default implementation returns the given {@code bean} as-is. * @param bean the new bean instance * @parambeanName 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 * @throwsorg.springframework.beans.BeansException in case of errors *@seeorg.springframework.beans.factory.InitializingBean#afterPropertiesSet *@see org.springframework.beans.factory.FactoryBean */ @Nullable default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; }

想自学源代码的同学可以加两个关注,将推出一系列源代码文章,大家一起自学!

redis设计与同时实现

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务