Spring 5 UiApplicationContextUtils 源码分析

UiApplicationContextUtils:用于UI应用程序上下文实现的实用程序类。提供对名为’themeSource’ 的特殊Bean的支持,该Bean类型为 themeSource

/**
 * Utility class for UI application context implementations.
 * Provides support for a special bean named "themeSource",
 * of type {@link org.springframework.ui.context.ThemeSource}.
 * <p>用于UI应用程序上下文实现的实用程序类。提供对名为'themeSource'
 * 的特殊Bean的支持,该Bean类型为 themeSource</p>
 *
 * @author Jean-Pierre Pawlak
 * @author Juergen Hoeller
 * @since 17.06.2003
 */
public abstract class UiApplicationContextUtils {
    
    

	/**
	 * Name of the ThemeSource bean in the factory.
	 * If none is supplied, theme resolution is delegated to the parent.
	 * @see org.springframework.ui.context.ThemeSource
	 */
	public static final String THEME_SOURCE_BEAN_NAME = "themeSource";


	private static final Log logger = LogFactory.getLog(UiApplicationContextUtils.class);


	/**
	 * Initialize the ThemeSource for the given application context,
	 * autodetecting a bean with the name "themeSource". If no such
	 * bean is found, a default (empty) ThemeSource will be used.
	 * <p>初始化给定应用程序上下文的 ThemeSource,自动检查名为 'ThemeSource'的
	 * bean。如果没有找到这样的Bean,将使用默认的'空'主题源</p>
	 * @param context current application context -- 当前应用程序上下文
	 * @return the initialized theme source (will never be {@code null})
	 * 		-- 初始化的主题源文件(永远不会为 null )
	 * @see #THEME_SOURCE_BEAN_NAME
	 */
	public static ThemeSource initThemeSource(ApplicationContext context) {
    
    
		//ThemeSource:接口,由能够解决主题的对象实现。这使得给定'Theme'的消息能够参数化和国际化
		//如果context的BeanFactory包含'themeSource'的bean,忽略父工厂
		if (context.containsLocalBean(THEME_SOURCE_BEAN_NAME)) {
    
    
			//从beanFactory中获取名为 themeSource 的 ThemeSource 类型的Bean对象
			ThemeSource themeSource = context.getBean(THEME_SOURCE_BEAN_NAME, ThemeSource.class);
			// Make ThemeSource aware of parent ThemeSource.
			// 使 ThemeSource 知道 父ThemeSource
			// HierarchicalThemeSource:主题源的子接口,由能够分层解决主题消息的对象实现
			//如果context的父级上下文是ThemeSource对象 && themeSource 是 HierarchicalThemeSource 对象
			if (context.getParent() instanceof ThemeSource && themeSource instanceof HierarchicalThemeSource) {
    
    
				//将themeSource强转为 HierarchicalThemeSource 对象
				HierarchicalThemeSource hts = (HierarchicalThemeSource) themeSource;
				//如果 hts 的父级ThemeSource为null
				if (hts.getParentThemeSource() == null) {
    
    
					// Only set parent context as parent ThemeSource if no parent ThemeSource
					// registered already.
					// 如果没有父ThemeSource已经注册,只能将父上下文设置为父主题资源
					//将父级上下文作为hts的父级ThemeSource
					hts.setParentThemeSource((ThemeSource) context.getParent());
				}
			}
			//如果当前日志级别是调试
			if (logger.isDebugEnabled()) {
    
    
				// 打印调试日志:使用 ThemeSource [ themeSource ]
				logger.debug("Using ThemeSource [" + 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.
			// 使用默认的ThemeSource能够接受getTheme调用,或者委托给父上下文的默认值,或者委托给本地的
			// ResourceBundleThemeSource
			HierarchicalThemeSource themeSource = null;
			//如果context的父级上下文是ThemeSource对象
			if (context.getParent() instanceof ThemeSource) {
    
    
				// DeletgatingThemeSource : 清空委托所有调用父ThemeSource的ThemeSource。如果没有可用的父元
				// 	素,他就不能解决任何 主题
				//新建一个DelegatingThemeSource对象
				themeSource = new DelegatingThemeSource();
				//将父级上线问作为themeSource的父级ThemeSource
				themeSource.setParentThemeSource((ThemeSource) context.getParent());
			}
			else {
    
    
				//ResourceBundleThemeSource:查找个人信息的资源实现 java.util.ResourceBundle 每个主题。主题 名称被解释为
				// 	ResourceBundle basename ,支持所有主题的公共 basename 前缀
				//新建一个 ResourceBundleThemeSource 对象
				themeSource = new ResourceBundleThemeSource();
			}
			//如果当前日志级别是调试
			if (logger.isDebugEnabled()) {
    
    
				//打印调试日志:无法找到带有名称'themeSource'的数据源:使用默认 [ themeSource ]
				logger.debug("Unable to locate ThemeSource with name '" + THEME_SOURCE_BEAN_NAME +
						"': using default [" + themeSource + "]");
			}
			//返回themeSource
			return themeSource;
		}
	}

}

猜你喜欢

转载自blog.csdn.net/qq_30321211/article/details/108326077