org.springframework.web.context.ConfigurableWebEnvironment Java Examples

The following examples show how to use org.springframework.web.context.ConfigurableWebEnvironment. 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: DispatcherServletTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void environmentOperations() {
	DispatcherServlet servlet = new DispatcherServlet();
	ConfigurableEnvironment defaultEnv = servlet.getEnvironment();
	assertThat(defaultEnv, notNullValue());
	ConfigurableEnvironment env1 = new StandardServletEnvironment();
	servlet.setEnvironment(env1); // should succeed
	assertThat(servlet.getEnvironment(), sameInstance(env1));
	try {
		servlet.setEnvironment(new DummyEnvironment());
		fail("expected IllegalArgumentException for non-configurable Environment");
	}
	catch (IllegalArgumentException ex) {
	}
	class CustomServletEnvironment extends StandardServletEnvironment { }
	@SuppressWarnings("serial")
	DispatcherServlet custom = new DispatcherServlet() {
		@Override
		protected ConfigurableWebEnvironment createEnvironment() {
			return new CustomServletEnvironment();
		}
	};
	assertThat(custom.getEnvironment(), instanceOf(CustomServletEnvironment.class));
}
 
Example #2
Source File: DispatcherServletTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void environmentOperations() {
	DispatcherServlet servlet = new DispatcherServlet();
	ConfigurableEnvironment defaultEnv = servlet.getEnvironment();
	assertThat(defaultEnv, notNullValue());
	ConfigurableEnvironment env1 = new StandardServletEnvironment();
	servlet.setEnvironment(env1); // should succeed
	assertThat(servlet.getEnvironment(), sameInstance(env1));
	try {
		servlet.setEnvironment(new DummyEnvironment());
		fail("expected IllegalArgumentException for non-configurable Environment");
	}
	catch (IllegalArgumentException ex) {
	}
	class CustomServletEnvironment extends StandardServletEnvironment { }
	@SuppressWarnings("serial")
	DispatcherServlet custom = new DispatcherServlet() {
		@Override
		protected ConfigurableWebEnvironment createEnvironment() {
			return new CustomServletEnvironment();
		}
	};
	assertThat(custom.getEnvironment(), instanceOf(CustomServletEnvironment.class));
}
 
Example #3
Source File: DispatcherServletTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void environmentOperations() {
	DispatcherServlet servlet = new DispatcherServlet();
	ConfigurableEnvironment defaultEnv = servlet.getEnvironment();
	assertThat(defaultEnv, notNullValue());
	ConfigurableEnvironment env1 = new StandardServletEnvironment();
	servlet.setEnvironment(env1); // should succeed
	assertThat(servlet.getEnvironment(), sameInstance(env1));
	try {
		servlet.setEnvironment(new DummyEnvironment());
		fail("expected IllegalArgumentException for non-configurable Environment");
	}
	catch (IllegalArgumentException ex) {
	}
	class CustomServletEnvironment extends StandardServletEnvironment { }
	@SuppressWarnings("serial")
	DispatcherServlet custom = new DispatcherServlet() {
		@Override
		protected ConfigurableWebEnvironment createEnvironment() {
			return new CustomServletEnvironment();
		}
	};
	assertThat(custom.getEnvironment(), instanceOf(CustomServletEnvironment.class));
}
 
Example #4
Source File: FunctionExporterInitializer.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
private boolean isExporting(GenericApplicationContext context) {
	Boolean enabled = context.getEnvironment().getProperty("spring.cloud.function.web.export.enabled",
			Boolean.class);
	if (enabled != null) {
		return enabled;
	}
	if (ClassUtils.isPresent("org.springframework.web.context.WebApplicationContext",
			getClass().getClassLoader())) {
		if (context instanceof WebApplicationContext || context instanceof ReactiveWebApplicationContext
				|| context.getEnvironment() instanceof ConfigurableWebEnvironment
				|| context.getEnvironment() instanceof ConfigurableReactiveWebEnvironment) {
			return false;
		}
	}
	return true;
}
 
Example #5
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 #6
Source File: GenericWebApplicationContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>Replace {@code Servlet}-related property sources.
 */
@Override
protected void initPropertySources() {
	ConfigurableEnvironment env = getEnvironment();
	if (env instanceof ConfigurableWebEnvironment) {
		((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, null);
	}
}
 
Example #7
Source File: AbstractRefreshableWebApplicationContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>Replace {@code Servlet}-related property sources.
 */
@Override
protected void initPropertySources() {
	ConfigurableEnvironment env = getEnvironment();
	if (env instanceof ConfigurableWebEnvironment) {
		((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig);
	}
}
 
Example #8
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 #9
Source File: GenericWebApplicationContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>Replace {@code Servlet}-related property sources.
 */
@Override
protected void initPropertySources() {
	ConfigurableEnvironment env = getEnvironment();
	if (env instanceof ConfigurableWebEnvironment) {
		((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, null);
	}
}
 
Example #10
Source File: AbstractRefreshableWebApplicationContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>Replace {@code Servlet}-related property sources.
 */
@Override
protected void initPropertySources() {
	ConfigurableEnvironment env = getEnvironment();
	if (env instanceof ConfigurableWebEnvironment) {
		((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig);
	}
}
 
Example #11
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 #12
Source File: GenericWebApplicationContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>Replace {@code Servlet}-related property sources.
 */
@Override
protected void initPropertySources() {
	ConfigurableEnvironment env = getEnvironment();
	if (env instanceof ConfigurableWebEnvironment) {
		((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, null);
	}
}
 
Example #13
Source File: AbstractRefreshableWebApplicationContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>Replace {@code Servlet}-related property sources.
 */
@Override
protected void initPropertySources() {
	ConfigurableEnvironment env = getEnvironment();
	if (env instanceof ConfigurableWebEnvironment) {
		((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig);
	}
}
 
Example #14
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 #15
Source File: GenericWebApplicationContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>Replace {@code Servlet}-related property sources.
 */
@Override
protected void initPropertySources() {
	ConfigurableEnvironment env = getEnvironment();
	if (env instanceof ConfigurableWebEnvironment) {
		((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, null);
	}
}
 
Example #16
Source File: AbstractRefreshableWebApplicationContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>Replace {@code Servlet}-related property sources.
 */
@Override
protected void initPropertySources() {
	ConfigurableEnvironment env = getEnvironment();
	if (env instanceof ConfigurableWebEnvironment) {
		((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig);
	}
}