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.StaticApplicationContext; 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.web.context.ConfigurableWebApplicationContext; 30 import org.springframework.web.context.ServletConfigAware; 31 import org.springframework.web.context.ServletContextAware; 32 import org.springframework.web.context.request.RequestScope; 33 import org.springframework.web.context.request.SessionScope; 34 35 57 public class StaticWebApplicationContext extends StaticApplicationContext 58 implements ConfigurableWebApplicationContext, ThemeSource { 59 60 private ServletContext servletContext; 61 62 private ServletConfig servletConfig; 63 64 private String namespace; 65 66 private ThemeSource themeSource; 67 68 69 public StaticWebApplicationContext() { 70 setDisplayName("Root WebApplicationContext"); 71 } 72 73 76 public void setServletContext(ServletContext servletContext) { 77 this.servletContext = servletContext; 78 } 79 80 public ServletContext getServletContext() { 81 return servletContext; 82 } 83 84 public void setServletConfig(ServletConfig servletConfig) { 85 this.servletConfig = servletConfig; 86 if (servletConfig != null && this.servletContext == null) { 87 this.servletContext = servletConfig.getServletContext(); 88 } 89 } 90 91 public ServletConfig getServletConfig() { 92 return this.servletConfig; 93 } 94 95 public void setNamespace(String namespace) { 96 this.namespace = namespace; 97 if (namespace != null) { 98 setDisplayName("WebApplicationContext for namespace '" + namespace + "'"); 99 } 100 } 101 102 public String getNamespace() { 103 return this.namespace; 104 } 105 106 110 public void setConfigLocations(String [] configLocations) { 111 throw new UnsupportedOperationException ("StaticWebApplicationContext does not support config locations"); 112 } 113 114 public String [] getConfigLocations() { 115 return null; 116 } 117 118 119 122 protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { 123 beanFactory.registerScope(SCOPE_REQUEST, new RequestScope()); 124 beanFactory.registerScope(SCOPE_SESSION, new SessionScope(false)); 125 beanFactory.registerScope(SCOPE_GLOBAL_SESSION, new SessionScope(true)); 126 127 beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext, this.servletConfig)); 128 beanFactory.ignoreDependencyInterface(ServletContextAware.class); 129 beanFactory.ignoreDependencyInterface(ServletConfigAware.class); 130 } 131 132 136 protected Resource getResourceByPath(String path) { 137 return new ServletContextResource(this.servletContext, path); 138 } 139 140 144 protected ResourcePatternResolver getResourcePatternResolver() { 145 return new ServletContextResourcePatternResolver(this); 146 } 147 148 151 protected void onRefresh() { 152 this.themeSource = UiApplicationContextUtils.initThemeSource(this); 153 } 154 155 public Theme getTheme(String themeName) { 156 return this.themeSource.getTheme(themeName); 157 } 158 159 } 160 | Popular Tags |