publicstaticThemeSourceinitThemeSource(ApplicationContext context){//判断当前容器中存在这个name的beanif(context.containsLocalBean(THEME_SOURCE_BEAN_NAME)){//根据name和class获取beanThemeSource themeSource = context.getBean(THEME_SOURCE_BEAN_NAME,ThemeSource.class);// Make ThemeSource aware of parent ThemeSource.//判断当前容器的父容器 实现了ThemeSource 并且 themeSource的类型是HierarchicalThemeSourceif(context.getParent()instanceofThemeSource&& themeSource instanceofHierarchicalThemeSource){//强转为HierarchicalThemeSourceHierarchicalThemeSource 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 并且 容器的父主题是ThemeSourceHierarchicalThemeSource themeSource =null;if(context.getParent()instanceofThemeSource){
themeSource =newDelegatingThemeSource();//则当前主题的设置为DelegatingThemeSource
themeSource.setParentThemeSource((ThemeSource) context.getParent());}else{//其他情况 则当前主题设置为ResourceBundleThemeSource
themeSource =newResourceBundleThemeSource();}if(logger.isDebugEnabled()){
logger.debug("Unable to locate ThemeSource with name '"+THEME_SOURCE_BEAN_NAME+"': using default ["+ themeSource +"]");}return themeSource;}}