一、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

原文地址:http://www.cnblogs.com/shigongp/p/16842206.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性