Spring源码之四prepareBeanFactory()办法
大家好,我是程序员田同学!
明天带大家解读refresh()办法的第三个办法prepareBeanFactory(),通过对refresh()的一步步解读,想必有一天小伙伴们能揭开Spring的神秘面纱。
照例,先站在prepareBeanFactory()的门外看它都做了什么,而后再逐渐深刻分析。prepareBeanFactory()办法次要是:
设置 BeanFactory 的类加载器,增加 BeanPostProcessor,手动注册几个非凡的 bean。
扩大:BeanPostProcessor是Spring IOC容器给咱们提供的一个扩大接口。
//bean初始化办法调用前被调用 @Nullable default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { return bean; } //bean初始化办法调用后被调用 @Nullable default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; }
BeanPostProcessor在整个bean的生命周期中处于红色箭头所示的地位。
prepareBeanFactory()的每个具体办法在以下代码正文中。
// Tell the internal bean factory to use the context's class loader etc. //设置上下文环境的启动类加载器 beanFactory.setBeanClassLoader(getClassLoader()); //增加bean表达式解释器,为了可能让咱们的beanFactory去解析bean表达式 beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader())); //spring外部的属性编辑器、既读取配置文件 beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment())); // Configure the bean factory with context callbacks. //增加BeanPostProcessor beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this)); //跳过以下6个属性的主动注入 //因为在ApplicationContextAwareProcessor后置处理器中通过setter注入 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. ////注册四个非凡的bean //在利用代码就能够通过类型主动拆卸把工厂实例和ApplicationContext实例设置到自定义bean的属性中 //这四个属性都会被主动设置,尽管没有在显示的在bean定义xml中注入它们 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. //注册事件监听器 beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this)); // 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. // 如果没有定义 "environment" 这个 bean,那么 Spring 会 "手动" 注册一个 if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) { beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment()); } // 如果没有定义 "systemProperties" 这个 bean,那么 Spring 会 "手动" 注册一个 if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) { beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties()); } // 如果没有定义 "systemEnvironment" 这个 bean,那么 Spring 会 "手动" 注册一个 if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) { beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment()); }
有一些同学想必对ignoreDependencyInterface()办法有些许的纳闷。
ignoreDependencyInterface的次要性能是疏忽给定接口的主动拆卸性能,也就是当有疏忽的接口类,主动拆卸会疏忽这部分类的初始化拆卸,因为某种状况下,此时的接口实现类不能初始化。
例如BeanNameAware,要想拆卸这个接口的实现对象,能够实现这个接口,通过实现的set办法进行拆卸