Spring源码学习(三):容器ApplicationContext的refresh(1)

本文聚焦Spring Boot中ApplicationContext的refresh操作。先从SpringApplication的run方法入手,关注refreshContext,最终会调用AbstractApplicationContext的refresh。解析了prepareBeanFactory时放入BeanFactory的重要对象及environment,还介绍了因Spring Boot启动,postProcessBeanFactory进行的web容器初始化等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言

第一篇地址,主要讲ApplicationContext的初始化-create:Spring源码学习(一):Spring容器创建和初始化工作准备
第二篇地址,主要讲ApplicationContext的准备工作-prepare:Spring源码学习(二):Spring容器之prepareContext和BeanFactoryPostProcessor的介绍
今天,咱们进行ApplicationContext的第三个重大步骤:refresh


一、Springboot中的refreshContext

继续从SpringApplication中run方法看下去,今天重点关注refresh操作
在这里插入图片描述
通过debug可以发现,最终会调用AbstractApplicationContext的refresh操作:

	/**
	 * Refresh the underlying {@link ApplicationContext}.
	 * @param applicationContext the application context to refresh
	 */
	protected void refresh(ApplicationContext applicationContext) {
		Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);
		((AbstractApplicationContext) applicationContext).refresh();
	}

二、AbstractApplicationContext的refresh

只挑重点的代码进行解析

1. prepareBeanFactory(beanFactory)

beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
这一句往容器里添加ApplicationContextAwareProcessor的BeanPostProcessor, ApplicationContextAwareProcessor的作用其实是在Bean的生命周期过程中的构造函数,这个构造函数主要是为了实现某些接口而存在
在这里插入图片描述
实现这些接口的方法,有些盆友在项目里写SpringUtils时,就通过实现ApplicationContextAware接口来给SpringUtils的ApplicationContext类型属性注入。

public interface ApplicationContextAware extends Aware {

	/**
	 * Set the ApplicationContext that this object runs in.
	 * Normally this call will be used to initialize the object.
	 * <p>Invoked after population of normal bean properties but before an init callback such
	 * as {@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet()}
	 * or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
	 * {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
	 * {@link MessageSourceAware}, if applicable.
	 * @param applicationContext the ApplicationContext object to be used by this object
	 * @throws ApplicationContextException in case of context initialization errors
	 * @throws BeansException if thrown by application context methods
	 * @see org.springframework.beans.factory.BeanInitializationException
	 */
	void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

}

其他几个接口也类似,都是为了实现接口方法
在这里插入图片描述
此处是往beanFactory里面直接注册了四个可直接使用的类型,这样如果咱们需要使用这四种类型的对象,直接通过@Autowired就可以引入。

		// Register early post-processor for detecting inner beans as ApplicationListeners.
		beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

这个其实也是添加一个新的BeanPostProcessor,在Bean的生命周期的最后阶段,通过执行postProcessAfterInitialization判断类是否是ApplicationListener,如果时的话,记录下来。方便后期进行事件处理

在这里插入图片描述
这段是给BeanFactory里面注册了environment、systemProperties、systemEnvironment属性,最常用的是Environment。实际项目中,可以通过如下方法,注入environment。
在这里插入图片描述
然后可以用**environment.getActiveProfiles()**来获取当前系统的运行环境。

2.refresh真正重要的部分开始

2.1 postProcessBeanFactory(beanFactory);

这个方法默认的实现是空的,如果我们直接运行Spring,启动的是AnnotationConfigApplicationContext类型的容器的话,这个方法将会什么也不做,但是目前我们是通过Springboot进来的,Springboot启动的是AnnotationConfigServletWebServerApplicationContext,他重写了该方法,主要调用了其父类ServletWebServerApplicationContext的postProcessBeanFactory方法,具体实现如下

	/**
	 * Register ServletContextAwareProcessor.
	 * @see ServletContextAwareProcessor
	 */
	@Override
	protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
		beanFactory.addBeanPostProcessor(new WebApplicationContextServletContextAwareProcessor(this));
		beanFactory.ignoreDependencyInterface(ServletContextAware.class);
		registerWebApplicationScopes();
	}

这段方法里最重要的是第三句,第一句其实就是设置了webApplicationContext属性,也就是咱们的web容器;不过第三句的重要性被大多数时候忽略,debug下去主要会调用该方法

	/**
	 * Register web-specific scopes ("request", "session", "globalSession", "application")
	 * with the given BeanFactory, as used by the WebApplicationContext.
	 * @param beanFactory the BeanFactory to configure
	 * @param sc the ServletContext that we're running within
	 */
	public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory,
			@Nullable ServletContext sc) {

		//这里给注册了两种web容器的作用域范围:request+session
		beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());
		beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope());
		//这儿此处一定为空
		if (sc != null) {
			ServletContextScope appScope = new ServletContextScope(sc);
			beanFactory.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope);
			// Register as ServletContext attribute, for ContextCleanupListener to detect it.
			sc.setAttribute(ServletContextScope.class.getName(), appScope);
		}

		//这儿牛批了,往容器里直接放了这四种类型的对象,点进去发现,放进去的其实都是ObjectFactory,
		//而ObjectFactory是干嘛用的呢?有个getObject方法,返回对象用的。
		//这样的话,其实在我们自己的应用中就可以直接注入request作为属性使用,
		//底层其实是通过filter,将每次请求的request放入到一个ThreadLocal的变量中,这样每次请求,ObjectFactory的getObject返回的对象都不同
		beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory());
		beanFactory.registerResolvableDependency(ServletResponse.class, new ResponseObjectFactory());
		beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory());
		beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());
		if (jsfPresent) {
			FacesDependencyRegistrar.registerFacesDependencies(beanFactory);
		}
	}

总结

以上就是今天本文的内容,主要讲解了prepareBeanFactory时放入到BeanFactory中的几个重要的对象以及environment,postProcessBeanFactory则因为是通过Springboot启动的原因,进行了web容器的一些初始化,同时可以让我们在自己的应用内直接注入request、response等对象(不过这种方式,貌似有人觉得不符合代码规范)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值