学习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 before 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.
* 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 after 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.
*
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.
*
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.
*
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;
}
想学习源码的同学可以加一个关注,将推出一系列源码文章,大家一起学习!
公众号回复 【redis设计与实现】获取电子书
页面更新:2024-04-01
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号