十、Spring源码学习之onRefresh方法

本文详细解释了Spring框架中的`onRefresh()`方法在初始化主题时的工作原理,包括如何处理`ThemeSource`bean的查找、注入及默认主题源的使用逻辑。
摘要由CSDN通过智能技术生成

onRefresh()方法

protected void onRefresh() throws BeansException {
		// For subclasses: do nothing by default.
		//提供子类重写
}

AbstractRefreshableWebApplicationContext#onRefresh()方法

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

UiApplicationContextUtils#initThemeSource()方法

public static ThemeSource initThemeSource(ApplicationContext context) {
		//判断当前容器中存在这个name的bean
		if (context.containsLocalBean(THEME_SOURCE_BEAN_NAME)) {
			//根据name和class获取bean
			ThemeSource themeSource = context.getBean(THEME_SOURCE_BEAN_NAME, ThemeSource.class);
			// Make ThemeSource aware of parent ThemeSource.
			//判断当前容器的父容器 实现了ThemeSource 并且 themeSource的类型是HierarchicalThemeSource
			if (context.getParent() instanceof ThemeSource && themeSource instanceof HierarchicalThemeSource) {
				//强转为HierarchicalThemeSource
				HierarchicalThemeSource hts = (HierarchicalThemeSource) themeSource;
				if (hts.getParentThemeSource() == null) {//如果hts父主题类是null
					// Only set parent context as parent ThemeSource if no parent ThemeSource
					// registered already.
					//则将当前的容器的父主题设置到hts中
					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.
			//如果容器中不存在这个name的bean 并且 容器的父主题是ThemeSource
			HierarchicalThemeSource themeSource = null;
			if (context.getParent() instanceof ThemeSource) {
				themeSource = new DelegatingThemeSource();//则当前主题的设置为DelegatingThemeSource
				themeSource.setParentThemeSource((ThemeSource) context.getParent());
			}
			else {//其他情况 则当前主题设置为ResourceBundleThemeSource
				themeSource = new ResourceBundleThemeSource();
			}
			if (logger.isDebugEnabled()) {
				logger.debug("Unable to locate ThemeSource with name '" + THEME_SOURCE_BEAN_NAME +
						"': using default [" + themeSource + "]");
			}
			return themeSource;
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值