1 16 17 package org.springframework.web.context.support; 18 19 import javax.servlet.ServletConfig ; 20 import javax.servlet.ServletContext ; 21 22 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 23 import org.springframework.context.support.AbstractRefreshableApplicationContext; 24 import org.springframework.core.io.Resource; 25 import org.springframework.core.io.support.ResourcePatternResolver; 26 import org.springframework.ui.context.Theme; 27 import org.springframework.ui.context.ThemeSource; 28 import org.springframework.ui.context.support.UiApplicationContextUtils; 29 import org.springframework.util.SystemPropertyUtils; 30 import org.springframework.web.context.ConfigurableWebApplicationContext; 31 import org.springframework.web.context.ServletConfigAware; 32 import org.springframework.web.context.ServletContextAware; 33 import org.springframework.web.context.request.RequestScope; 34 import org.springframework.web.context.request.SessionScope; 35 36 80 public abstract class AbstractRefreshableWebApplicationContext extends AbstractRefreshableApplicationContext 81 implements ConfigurableWebApplicationContext, ThemeSource { 82 83 84 private ServletContext servletContext; 85 86 87 private ServletConfig servletConfig; 88 89 90 private String namespace; 91 92 93 private String [] configLocations; 94 95 96 private ThemeSource themeSource; 97 98 99 public AbstractRefreshableWebApplicationContext() { 100 setDisplayName("Root WebApplicationContext"); 101 } 102 103 104 public void setServletContext(ServletContext servletContext) { 105 this.servletContext = servletContext; 106 } 107 108 public ServletContext getServletContext() { 109 return this.servletContext; 110 } 111 112 public void setServletConfig(ServletConfig servletConfig) { 113 this.servletConfig = servletConfig; 114 if (servletConfig != null && this.servletContext == null) { 115 this.servletContext = servletConfig.getServletContext(); 116 } 117 } 118 119 public ServletConfig getServletConfig() { 120 return this.servletConfig; 121 } 122 123 public void setNamespace(String namespace) { 124 this.namespace = namespace; 125 if (namespace != null) { 126 setDisplayName("WebApplicationContext for namespace '" + namespace + "'"); 127 } 128 } 129 130 public String getNamespace() { 131 return this.namespace; 132 } 133 134 public void setConfigLocations(String [] locations) { 135 this.configLocations = new String [locations.length]; 136 for (int i = 0; i < locations.length; i++) { 137 this.configLocations[i] = resolvePath(locations[i]); 138 } 139 } 140 141 public String [] getConfigLocations() { 142 return (this.configLocations != null ? this.configLocations : getDefaultConfigLocations()); 143 } 144 145 152 protected String [] getDefaultConfigLocations() { 153 return null; 154 } 155 156 157 160 protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { 161 beanFactory.registerScope(SCOPE_REQUEST, new RequestScope()); 162 beanFactory.registerScope(SCOPE_SESSION, new SessionScope(false)); 163 beanFactory.registerScope(SCOPE_GLOBAL_SESSION, new SessionScope(true)); 164 165 beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext, this.servletConfig)); 166 beanFactory.ignoreDependencyInterface(ServletContextAware.class); 167 beanFactory.ignoreDependencyInterface(ServletConfigAware.class); 168 } 169 170 177 protected String resolvePath(String path) { 178 return SystemPropertyUtils.resolvePlaceholders(path); 179 } 180 181 185 protected Resource getResourceByPath(String path) { 186 return new ServletContextResource(this.servletContext, path); 187 } 188 189 193 protected ResourcePatternResolver getResourcePatternResolver() { 194 return new ServletContextResourcePatternResolver(this); 195 } 196 197 200 protected void onRefresh() { 201 this.themeSource = UiApplicationContextUtils.initThemeSource(this); 202 } 203 204 public Theme getTheme(String themeName) { 205 return this.themeSource.getTheme(themeName); 206 } 207 208 } 209 | Popular Tags |