【Spring源码系列- IOC】
本文目录
Debug进入prepareBeanFactory(beanFactory)
prepareBeanFactory(beanFactory)是refresh()函数中的第三个函数,想了解前两个函数:准备工作的prepareRefresh()和创建工厂(包含配置文件的解析工作)的obtainFreshBeanFactory()可以参考【Spring源码】3. xml文件如何转换成BeanDefinition(主要涉及prepareRefresh()+ obtainFreshBeanFactory()两个函数,图解向,堆图预警)_AQin1012的博客-CSDN博客一堆图预警~~~通过测试启动类进入ApplicationContext容器设置(解析)配置文件路径setConfigLocations(configLocations);参数configLocations格式是String[]进入超级重要的refresh()方法1.准备工作:prepareRefresh()/** * Prepare this context for refreshing, setting its startup date andhttps://blog.csdn.net/aqin1012/article/details/124494175
Debug进入prepareBeanFactory(beanFactory)
完整prepareBeanFactory(ConfigurableListableBeanFactory beanFactory)函数源码(嘿嘿下面附带注解可复制版)
部分函数详解(注释超详细有没有~另外提一嘴的其实就一个)
注意注意~addPropertyEditorRegistrar这个函数是可以进行属性编辑相关的扩展的,保姆级教程参考本系列下一篇文章
带注解函数全文(体贴如我(o^^o))~~
/**
* Configure the factory's standard context characteristics,
* such as the context's ClassLoader and post-processors.
* @param beanFactory the BeanFactory to configure
*/
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
// Tell the internal bean factory to use the context's class loader etc.
/** 设置BeanFactory的classloader为当前context的classloader */
beanFactory.setBeanClassLoader(getClassLoader());
/** 设置BeanFactory的表达式语言处理器 */
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
/** 可扩展
* 为BeanFactory添加一个默认的propertyResolver(主要是bean属性设置管理的一个工具类)
* */
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));
// Configure the bean factory with context callbacks.
/** 添加BeanPostProcessor */
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
/** 设置要忽略自动装配的接口(这些接口的实现是由容器通过set方法进行注入的)*/
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
// BeanFactory interface not registered as resolvable type in a plain factory.
// MessageSource registered (and found for autowiring) as a bean.
/** 设置几个自动匹配的特殊规则,当进行ioc初始化是如果有多个实现,那么就使用指定的对象进行注入 */
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
// Register early post-processor for detecting inner beans as ApplicationListeners.
/** 注册BPP */
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
/**
* 增加对AspectJ的支持(AspectJ提供了两种织入方式:编译期织入、类加载期织入(如下面的LOAD_TIME_WEAVER_BEAN_NAME))
* 在java中织入为三种方式:编译期织入、类加载期织入、运行期织入
* * 编译期织入 指在java编译期,采用特殊的编译器,将切面织入java类中
* * 类加载期织入 指通过特殊的类加载器,在类的字节码加载到JVM时,织入切面
* * 运行期织入 指通过cglib或者jdk动态代理在程序运行时进行切面织入
*/
// Detect a LoadTimeWeaver and prepare for weaving, if found.
if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
// Set a temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
// Register default environment beans.
/** 注册默认的系统环境bean到一级缓存中 */
if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
}
if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
}
if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/135430.html