SpringMVC-mvc context重载AbstractApplicationContext.refresh方法部分

一、postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
AbstractRefreshableWebApplicationContext.postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)

protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
	beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext, this.servletConfig));
	beanFactory.ignoreDependencyInterface(ServletContextAware.class);
	beanFactory.ignoreDependencyInterface(ServletConfigAware.class);

	WebApplicationContextUtils.registerWebApplicationScopes(beanFactory, this.servletContext);
	WebApplicationContextUtils.registerEnvironmentBeans(beanFactory, this.servletContext, this.servletConfig);
}

1、添加ServletContextAwareProcessor BeanPostProcessor,ServletContextAwareProcessor为实现了ServletContextAware的bean注入ServletContext,实现ServletConfigAware接口的bean注入ServletConfig
2、忽略ServletContextAware接口
3、忽略ServletConfigAware接口
4、调用WebApplicationContextUtils.registerWebApplicationScopes
5、调用WebApplicationContextUtils.registerEnvironmentBeans

WebApplicationContextUtils.registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory,@Nullable ServletContext sc)

	public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory,
		@Nullable ServletContext sc) {

	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);
	}

	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);
	}
}

1、注册request作用域的RequestScope
2、注册session作用域的SessionScope
3、如果ServletContext不为null,注册application作用域的ServletContextScope并将ServletContextScope设置为ServletContext属性
4、注册ServletRequest依赖是RequestObjectFactory,ServletResponse依赖是ResponseObjectFactory,HttpSession依赖是SessionObjectFactory,WebRequest依赖是WebRequestObjectFactory。如果jsfPresent为true,注册FacesContext依赖是ObjectFactory,注册ExternalContext依赖是ObjectFactory

WebApplicationContextUtils.registerEnvironmentBeans(ConfigurableListableBeanFactory bf,@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig)

public static void registerEnvironmentBeans(ConfigurableListableBeanFactory bf,
		@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) {

	if (servletContext != null && !bf.containsBean(WebApplicationContext.SERVLET_CONTEXT_BEAN_NAME)) {
		bf.registerSingleton(WebApplicationContext.SERVLET_CONTEXT_BEAN_NAME, servletContext);
	}

	if (servletConfig != null && !bf.containsBean(ConfigurableWebApplicationContext.SERVLET_CONFIG_BEAN_NAME)) {
		bf.registerSingleton(ConfigurableWebApplicationContext.SERVLET_CONFIG_BEAN_NAME, servletConfig);
	}

	if (!bf.containsBean(WebApplicationContext.CONTEXT_PARAMETERS_BEAN_NAME)) {
		Map<String, String> parameterMap = new HashMap<>();
		if (servletContext != null) {
			Enumeration<?> paramNameEnum = servletContext.getInitParameterNames();
			while (paramNameEnum.hasMoreElements()) {
				String paramName = (String) paramNameEnum.nextElement();
				parameterMap.put(paramName, servletContext.getInitParameter(paramName));
			}
		}
		if (servletConfig != null) {
			Enumeration<?> paramNameEnum = servletConfig.getInitParameterNames();
			while (paramNameEnum.hasMoreElements()) {
				String paramName = (String) paramNameEnum.nextElement();
				parameterMap.put(paramName, servletConfig.getInitParameter(paramName));
			}
		}
		bf.registerSingleton(WebApplicationContext.CONTEXT_PARAMETERS_BEAN_NAME,
				Collections.unmodifiableMap(parameterMap));
	}

	if (!bf.containsBean(WebApplicationContext.CONTEXT_ATTRIBUTES_BEAN_NAME)) {
		Map<String, Object> attributeMap = new HashMap<>();
		if (servletContext != null) {
			Enumeration<?> attrNameEnum = servletContext.getAttributeNames();
			while (attrNameEnum.hasMoreElements()) {
				String attrName = (String) attrNameEnum.nextElement();
				attributeMap.put(attrName, servletContext.getAttribute(attrName));
			}
		}
		bf.registerSingleton(WebApplicationContext.CONTEXT_ATTRIBUTES_BEAN_NAME,
				Collections.unmodifiableMap(attributeMap));
	}
}

1、如果ConfigurableListableBeanFactory不存在ServletContext bean则注册到beanFactory
2、如果ConfigurableListableBeanFactory不存在ServletConfig bean则注册到beanFactory
3、如果ConfigurableListableBeanFactory不存在名为contextParameters的bean则从ServletContext,ServletConfig中获取参数并注册到beanFactory
4、如果ConfigurableListableBeanFactory不存在名为contextAttributes的bean则从servletContext获取属性值并注册到beanFactory

**二、AbstractRefreshableWebApplicationContext.onRefresh() **

protected void onRefresh() {
	this.themeSource = UiApplicationContextUtils.initThemeSource(this); // 初始化themeSource
}

public static ThemeSource initThemeSource(ApplicationContext context) {
	if (context.containsLocalBean(THEME_SOURCE_BEAN_NAME)) {
		ThemeSource themeSource = context.getBean(THEME_SOURCE_BEAN_NAME, ThemeSource.class);
		// Make ThemeSource aware of parent ThemeSource.
		if (context.getParent() instanceof ThemeSource && themeSource instanceof HierarchicalThemeSource) {
			HierarchicalThemeSource hts = (HierarchicalThemeSource) themeSource;
			if (hts.getParentThemeSource() == null) {
				// Only set parent context as parent ThemeSource if no parent ThemeSource
				// registered already.
				hts.setParentThemeSource((ThemeSource) context.getParent());
			}
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Using ThemeSource [" + themeSource + "]");
		}
		return themeSource;
	}
	else {
		// Use default ThemeSource to be able to accept getTheme calls, either
		// delegating to parent context's default or to local ResourceBundleThemeSource.
		HierarchicalThemeSource themeSource = null;
		if (context.getParent() instanceof ThemeSource) {
			themeSource = new DelegatingThemeSource();
			themeSource.setParentThemeSource((ThemeSource) context.getParent());
		}
		else {
			themeSource = new ResourceBundleThemeSource();
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Unable to locate ThemeSource with name '" + THEME_SOURCE_BEAN_NAME +
					"': using default [" + themeSource + "]");
		}
		return themeSource;
	}
}

1、ApplicationContext包含名为themeSource的bean则获取bean,且themeSource是HierarchicalThemeSource且context.getParent()是ThemeSource,设置ParentThemeSource
2、判断context.getParent()是ThemeSource则设置themeSource为DelegatingThemeSource否则themeSource设置为ResourceBundleThemeSource

posted @ 2022-11-01 19:44  shigp1  阅读(30)  评论(0编辑  收藏  举报