Spring源码学习--AbstractRefreshableWebApplicationContext(五)

3/8/2017来源:ASP.NET技巧人气:3803

applicationContext应用上下文体系如下:

上一篇博客 SPRing源码学习--xmlWebApplicationContext(四)中我们已经简单介绍了一下web容器相关的应用上下文,AbstractRefreshableWebApplicationContext抽象类中并没有太多的操作,更多的是一些get和set操作,主要包括ServletContext(应用上下文就保存其中)和一些主题Theme相关。

完整源码如下:

public abstract class AbstractRefreshableWebApplicationContext extends AbstractRefreshableConfigApplicationContext
		implements ConfigurableWebApplicationContext, ThemeSource {
	//servlet上下文
	private ServletContext servletContext;
	//配置
	private ServletConfig servletConfig;
	/** Namespace of this context, or {@code null} if root */
	private String namespace;
	//主题资源
	private ThemeSource themeSource;
	public AbstractRefreshableWebApplicationContext() {
		setDisplayName("Root WebApplicationContext");
	}
	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
	@Override
	public ServletContext getServletContext() {
		return this.servletContext;
	}
	@Override
	public void setServletConfig(ServletConfig servletConfig) {
		this.servletConfig = servletConfig;
		if (servletConfig != null && this.servletContext == null) {
			setServletContext(servletConfig.getServletContext());
		}
	}
	@Override
	public ServletConfig getServletConfig() {
		return this.servletConfig;
	}
	@Override
	public void setNamespace(String namespace) {
		this.namespace = namespace;
		if (namespace != null) {
			setDisplayName("WebApplicationContext for namespace '" + namespace + "'");
		}
	}
	@Override
	public String getNamespace() {
		return this.namespace;
	}
	@Override
	public String[] getConfigLocations() {
		return super.getConfigLocations();
	}
	@Override
	public String getApplicationName() {
		return (this.servletContext != null ? this.servletContext.getContextPath() : "");
	}
	@Override
	protected ConfigurableEnvironment createEnvironment() {
		return new StandardServletEnvironment();
	}
	@Override
	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);
	}
	@Override
	protected Resource getResourceByPath(String path) {
		return new ServletContextResource(this.servletContext, path);
	}
	@Override
	protected ResourcePatternResolver getResourcePatternResolver() {
		return new ServletContextResourcePatternResolver(this);
	}
	@Override
	protected void onRefresh() {
		this.themeSource = UiApplicationContextUtils.initThemeSource(this);
	}
	@Override
	protected void initPropertySources() {
		ConfigurableEnvironment env = getEnvironment();
		if (env instanceof ConfigurableWebEnvironment) {
			((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig);
		}
	}
	@Override
	public Theme getTheme(String themeName) {
		return this.themeSource.getTheme(themeName);
	}
}