Spring源码深度解析-IOC的初始化过程之refresh()--prepareBeanFactory

 1.Spring的prepareBeanFactory源码如下

protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
		// Tell the internal bean factory to use the context's class loader etc.
		/**
		 * 设置上下文类加载器
		 */
		beanFactory.setBeanClassLoader(getClassLoader());

		/**
		 * 添加bean表达式解释器,为了能够让我们的beanFactory去解析bean表达式
		 * 模板默认以前缀“#{”开头,以后缀“}”结尾
		 * 可以修改默认额前缀后缀
		 * 通过beanFactory.getBeanExpressionResolver()获得BeanExpressionResolver
		 * 然后resolver.setExpressionPrefix("%{");resolver.setExpressionSuffix("}");
		 *
		 * 那么什么时候用到这个解析器?
		 * 就是在Bean进行初始化后会有属性填充的一步,方法如下:
		 * protected void populateBean(String beanName, RootBeanDefinition mbd, BeanWrapper bw) {
		 * 	//属性填充
		 * 	applyPropertyValues(beanName, mbd, bw, pvs);
		 * }
		 * 最终会通过AbstractBeanFactory中的evaluateBeanDefinitionString方法进行解析
		 * 然后这时候就进到StandardBeanExpressionResolver中的evaluate方法中进行解析了
		 */
		beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));

		/**
		 * spring内部的属性编辑器
		 * 添加PropertyEditor属性编辑器(可以将我们的property动态设置为bean里面对应的属性类型)
		 * 比如:property赋值的是路径名(classpath/spring.xml),而对应bean属性设置的是Resource,则有spring的ResourceEditor完成转换
		 * springframework-bean下的propertyEditors包下有很多spring自带的属性编辑器
		 * 其中刚才提到的ResourceEditor在springframework-core下的io包里面
		 *
		 * 可以自定义属性编辑器,通过实现PropertyEditorSupport接口,spring中自带的属性编辑器也是这么做的
		 * 使用ApplicationContext,只需要在配置文件中通过CustomEditorConfigurer注册即可。
		 * CustomEditorConfigurer实现了BeanFactoryPostProcessor接口,因而是一个Bean工厂后置处理器
		 * 在Spring容器中加载配置文件并生成BeanDefinition后会被执行。CustomEditorConfigurer在容器启动时有机会注册自定义的属性编辑器
		 */
		beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

		// Configure the bean factory with context callbacks.
		/**
		 * 添加一个BPP
		 * ApplicationContextAwareProcessor:能够在bean中获得各种*Aware
		 */
		beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
		/**
		 * 跳过以下6个属性的自动注入
		 * 因为在ApplicationContextAwareProcessor中已经完成了手动注入
		 */
		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.
		/**
		 * 注册几个自动装配相关的类和实例
		 * 在应用代码就可以通过类型自动装配把工厂实例和ApplicationContext实例设置到自定义bean的属性中
		 *
		 * 例如:这几个属性都会被自动设置,虽然没有在显示的在bean定义xml中注入它们
		 * private BeanFactory beanFactory;
		 * private ResourceLoader resourceLoader;
		 * private ApplicationEventPublisher appEventPublisher;
		 * private ApplicationContext appContext;
		 */
		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));

		// Detect a LoadTimeWeaver and prepare for weaving, if found.
		/**
		 * 当容器检查到定义了名称为loadTimeWeaver的bean时
		 * 会注册一个LoadTimeWeaverAwareProcessor到容器中
		 *
		 * 这个BPP用来处理LoadTimeWeaverAware接口的
		 * 把LTW实例设置到实现了LoadTimeWeaverAware接口的bean中
		 */
		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则spring自己注册
		 */
		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());
		}
	}

2. 主要的几个特征进行分析

  • BeanExpressionResolver
  • PropertyEditorRegistrar
  • BeanPostProcessor
  • ResourceLoader
  • ApplicationEventPublisher
  • ApplicationListenerDetector
  • LoadTimeWeaverAwareProcessor
  • ContextTypeMatchClassLoader

你可能感兴趣的:(Spring,源码分析,spring,java)