spring之prepareBeanFactory

prepareBeanFactory()

1.第一步

/**
			 * 为工厂添加一些材料,用于后面的操作的顺利进行,工厂添加一些属性值,用于后面对于bean的操作
			 * 添加了如下操作:
			 * 初始化类加载器
			 * 初始化spel解析器
			 * 设置属性注册解析器PropertyEditor 这个主要是对bean的属性等设置管理的一个工具
			 * 增加一个处理Aware相关接口的Bean处理器 并设置相关忽略的接口。
			 * 具体应用在Bean初始化之前,属性填充过程中,进行属性的过滤。对于实现这些接口的,不做处理,由ApplicationContextAwareProcess处理。
			 *加入一个监听器相关的监听器,主要作用是,将监听器的Bean加入到  详见 Bean相关的处理器(
			 * 将默认的环境Bean注入到容器中,environment、systemProperties、SystemEnvironment,
			 * 等等。。。。
			 */
			prepareBeanFactory(beanFactory);

2.第二步

/**
	 * Configure the factory's standard context characteristics,
	 * such as the context's ClassLoader and post-processors.
	 * @param beanFactory the BeanFactory to configure
	 */
	/**
	 * https://blog.csdn.net/NEW_BUGGER/article/details/106258460,博客地址
	 * @param beanFactory
	 */
	protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
		//Tell the internal bean factory to use the context's class loader etc .
		/**
		 * 设置工厂的bean的类加载器 ,设置类加载器:存在则直接设置/不存在则新建一个默认类加载器
		 */
		beanFactory.setBeanClassLoader(getClassLoader());
		/**
		 * 设置spring的表达式解析器,其实就是将 配置文件中对应的表达式解析的解析器,spel解析器,设置EL表达式解析器(Bean初始化完成后填充属性时会用到)
		 */
		beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));

		/**
		 *设置属性注册解析器PropertyEditor
		 */
		beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

		// Configure the bean factory with context callbacks.
		/**
		 * 将当前的ApplicationContext对象交给ApplicationContextAwareProcessor类来处理,从而在Aware接口实现类中的注入applicationContext
		 *添加一个beanpostprocessor,用于bean初始化前后调用
		 */
		beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
		/**
		 * 设置忽略自动装配的接口
		 */
		beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
		beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
		beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
		beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
		beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
		beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

		/**
		 * https://blog.csdn.net/yuge1123/article/details/106053857
		 * 在Spring自动装配的时候如果一个接口有多个实现类,并且都已经放到IOC中去了,
		 * 那么自动装配的时候就会出异常,因为spring不知道把哪个实现类注入进去,
		 * 但是如果我们自定义一个类,然后实现BeanFactoryPostProcessor接口
		 * 在该阶段调用这个方法,如果哪个地方要自动注入这个类型的对象的话,那么就注入进去我们指定的对象
		 */
		// BeanFactory interface not registered as resolvable type in a plain factory.
		// MessageSource registered (and found for autowiring) as a bean.
		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.
		/**
		 * 加入一个监听器相关的监听器,主要作用是,将监听器的Bean加入到  详见 Bean相关的处理器(二)、ApplicationListenerDetector 介绍
		 */
		beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

		// Detect a LoadTimeWeaver and prepare for weaving, if found.
		/**
		 * 检查容器中是否包含名称为loadTimeWeaver的bean  具体详见:代码的织入方式之loadTimeWeaver详解
		 * AspectJ采用编译期织入、类加载期织入两种方式进行切面的织入
		 */
		if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
			/**
			 *       // 在BEAN初始化之前检查BEAN是否实现了LoadTimeWeaverAware接口,
			 *       // 如果是,则进行加载时织入,即静态代理。
			 */
			beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
			// Set a temporary ClassLoader for type matching.
			beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
		}

		// Register default environment beans.
		/**
		 * 将默认的环境Bean注入到容器中,environment、systemProperties、SystemEnvironment
		 */
		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());
		}
	}

这里主要添加:

  • 添加两个后置处理器BeanPostProcessor ,ApplicationContextAwareProcessor,ApplicationListenerDetector
  • 添加可以解析的自动装配器,即在任何组件中都可以自动注入,BeanFactory,ResourceLoader,ApplicationEventPublisher,ApplicationContext
  • 添加三个组件:
    • 注册 environment 组件,类型是【ConfigurableEnvironment】
    • 注册 systemProperties 组件,类型是【Map<String, Object>】
    • 注册 systemEnvironment 组件,类型是【Map<String, Object>】
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BLUE_SEVEN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值