Java Code Examples for org.springframework.web.context.ConfigurableWebApplicationContext#setServletContext()

The following examples show how to use org.springframework.web.context.ConfigurableWebApplicationContext#setServletContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FrameworkServlet.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
	if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
		// The application context id is still set to its original default value
		// -> assign a more useful id based on available information
		if (this.contextId != null) {
			wac.setId(this.contextId);
		}
		else {
			// Generate default id... 生成默认ID
			wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
					ObjectUtils.getDisplayString(getServletContext().getContextPath()) + '/' + getServletName());
		}
	}

	wac.setServletContext(getServletContext());
	wac.setServletConfig(getServletConfig());
	wac.setNamespace(getNamespace());
	wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));

	// The wac environment's #initPropertySources will be called in any case when the context
	// is refreshed; do it eagerly here to ensure servlet property sources are in place for
	// use in any post-processing or initialization that occurs below prior to #refresh
	ConfigurableEnvironment env = wac.getEnvironment();
	if (env instanceof ConfigurableWebEnvironment) {
		((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig());
	}

	postProcessWebApplicationContext(wac);
	// 遍历 ApplicationContextInitializer,执行 initialize 方法
	applyInitializers(wac);
	// 关键的刷新,加载配置文件及整合 parent 到 wac
	wac.refresh();
}
 
Example 2
Source File: FrameworkServlet.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
	if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
		// The application context id is still set to its original default value
		// -> assign a more useful id based on available information
		if (this.contextId != null) {
			wac.setId(this.contextId);
		}
		else {
			// Generate default id...
			wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
					ObjectUtils.getDisplayString(getServletContext().getContextPath()) + '/' + getServletName());
		}
	}

	wac.setServletContext(getServletContext());
	wac.setServletConfig(getServletConfig());
	wac.setNamespace(getNamespace());
	wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));

	// The wac environment's #initPropertySources will be called in any case when the context
	// is refreshed; do it eagerly here to ensure servlet property sources are in place for
	// use in any post-processing or initialization that occurs below prior to #refresh
	ConfigurableEnvironment env = wac.getEnvironment();
	if (env instanceof ConfigurableWebEnvironment) {
		((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig());
	}

	postProcessWebApplicationContext(wac);
	applyInitializers(wac);
	wac.refresh();
}
 
Example 3
Source File: FrameworkServlet.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
	if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
		// The application context id is still set to its original default value
		// -> assign a more useful id based on available information
		if (this.contextId != null) {
			wac.setId(this.contextId);
		}
		else {
			// Generate default id...
			wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
					ObjectUtils.getDisplayString(getServletContext().getContextPath()) + '/' + getServletName());
		}
	}

	wac.setServletContext(getServletContext());
	wac.setServletConfig(getServletConfig());
	wac.setNamespace(getNamespace());
	wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));

	// The wac environment's #initPropertySources will be called in any case when the context
	// is refreshed; do it eagerly here to ensure servlet property sources are in place for
	// use in any post-processing or initialization that occurs below prior to #refresh
	ConfigurableEnvironment env = wac.getEnvironment();
	if (env instanceof ConfigurableWebEnvironment) {
		((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig());
	}

	postProcessWebApplicationContext(wac);
	applyInitializers(wac);
	wac.refresh();
}
 
Example 4
Source File: FrameworkServlet.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
	if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
		// The application context id is still set to its original default value
		// -> assign a more useful id based on available information
		if (this.contextId != null) {
			wac.setId(this.contextId);
		}
		else {
			// Generate default id...
			wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
					ObjectUtils.getDisplayString(getServletContext().getContextPath()) + "/" + getServletName());
		}
	}

	wac.setServletContext(getServletContext());
	wac.setServletConfig(getServletConfig());
	wac.setNamespace(getNamespace());
	wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));

	// The wac environment's #initPropertySources will be called in any case when the context
	// is refreshed; do it eagerly here to ensure servlet property sources are in place for
	// use in any post-processing or initialization that occurs below prior to #refresh
	ConfigurableEnvironment env = wac.getEnvironment();
	if (env instanceof ConfigurableWebEnvironment) {
		((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig());
	}

	postProcessWebApplicationContext(wac);
	applyInitializers(wac);
	wac.refresh();
}
 
Example 5
Source File: SpringBootServletConfigurationSupport.java    From aws-serverless-java-container with Apache License 2.0 4 votes vote down vote up
public SpringBootServletConfigurationSupport(ConfigurableWebApplicationContext webApplicationContext) {
    super(webApplicationContext);
    webApplicationContext.setServletContext(SpringBootLambdaContainerHandler.getInstance().getServletContext());
}
 
Example 6
Source File: GeomajasContextListener.java    From geomajas-project-server with GNU Affero General Public License v3.0 4 votes vote down vote up
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "REC_CATCH_EXCEPTION")
public void contextInitialized(ServletContextEvent servletContextEvent) {

	ServletContext servletContext = servletContextEvent.getServletContext();

	StringBuilder configLocation = new StringBuilder();
	configLocation.append("classpath:org/geomajas/spring/geomajasContext.xml");
	String additionalLocations = servletContext.getInitParameter(CONFIG_LOCATION_PARAMETER);
	if (null != additionalLocations) {
		for (String onePart : additionalLocations.split("\\s")) {
			String part = onePart.trim();
			if (part.length() > 0) {
				configLocation.append(',');
				int pos = part.indexOf(':');
				if (pos < 0) {
					// no protocol specified, use classpath.
					configLocation.append(GeomajasConstant.CLASSPATH_URL_PREFIX);
				} else if (0 == pos) {
					// location starts with colon, use default application context 
					part = part.substring(1);
				}
				configLocation.append(part);
			}
		}
	}
	ConfigurableWebApplicationContext applicationContext = new XmlWebApplicationContext();

	// Assign the best possible id value.
	String id;
	try {
		String contextPath = (String) ServletContext.class.getMethod("getContextPath").invoke(servletContext);
		id = ObjectUtils.getDisplayString(contextPath);
	} catch (Exception ex) {
		// Servlet <= 2.4: resort to name specified in web.xml, if any.
		String servletContextName = servletContext.getServletContextName();
		id = ObjectUtils.getDisplayString(servletContextName);			
	}

	applicationContext.setId("Geomajas:" + id);
	applicationContext.setServletContext(servletContext);
	applicationContext.setConfigLocation(configLocation.toString());
	applicationContext.refresh();

	ApplicationContextUtil.setApplicationContext(servletContext, applicationContext);
	servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
}