Commit 2169bbbc authored by Dave Syer's avatar Dave Syer

Check Environment matches webEnvironment after it is initialized

After the ApplicationEnvironmentPrepared we now check that the
webEnvironment flag and/or the Environment hasn't changed, in case
the user wanted to switch the context from a web to non-web in a
listener.

Fixes gh-2716
parent 93d12494
......@@ -120,7 +120,8 @@ import org.springframework.web.context.support.StandardServletEnvironment;
* your application, however, any of the following sources can also be used:
*
* <ul>
* <li>{@link Class} - A Java class to be loaded by {@link AnnotatedBeanDefinitionReader}</li>
* <li>{@link Class} - A Java class to be loaded by {@link AnnotatedBeanDefinitionReader}
* </li>
*
* <li>{@link Resource} - An XML resource to be loaded by {@link XmlBeanDefinitionReader},
* or a groovy script to be loaded by {@link GroovyBeanDefinitionReader}</li>
......@@ -235,7 +236,8 @@ public class SpringApplication {
this.sources.addAll(Arrays.asList(sources));
}
this.webEnvironment = deduceWebEnvironment();
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}
......@@ -281,8 +283,8 @@ public class SpringApplication {
context = doRun(listeners, args);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(
getApplicationLog(), stopWatch);
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
return context;
}
......@@ -312,6 +314,7 @@ public class SpringApplication {
printBanner(environment);
}
environment = resetEnvironment(environment);
// Create, load, refresh and run the ApplicationContext
context = createApplicationContext();
if (this.registerShutdownHook) {
......@@ -348,11 +351,43 @@ public class SpringApplication {
return context;
}
private ConfigurableEnvironment resetEnvironment(
ConfigurableEnvironment environment) {
if (this.environment != null && environment != this.environment) {
environment = this.environment;
}
if (environment instanceof StandardServletEnvironment && !this.webEnvironment) {
Set<String> servletSources = new HashSet<>(Arrays.asList(
StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME,
StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME,
StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME));
ConfigurableEnvironment copy = environment;
environment = new StandardEnvironment();
environment.setActiveProfiles(copy.getActiveProfiles());
String current = null;
MutablePropertySources sources = environment.getPropertySources();
for (PropertySource<?> source : copy.getPropertySources()) {
if (!servletSources.contains(source.getName())) {
String name = source.getName();
if (sources.contains(name)) {
sources.replace(name, source);
}
else if (current == null) {
sources.addFirst(source);
}
else {
sources.addAfter(current, source);
}
current = name;
}
}
}
return environment;
}
private void configureHeadlessProperty() {
System.setProperty(
SYSTEM_PROPERTY_JAVA_AWT_HEADLESS,
System.getProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS,
Boolean.toString(this.headless)));
System.setProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, System.getProperty(
SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, Boolean.toString(this.headless)));
}
private SpringApplicationRunListeners getRunListeners(String[] args) {
......@@ -385,8 +420,8 @@ public class SpringApplication {
instances.add(instance);
}
catch (Throwable ex) {
throw new IllegalArgumentException("Cannot instantiate " + type + " : "
+ name, ex);
throw new IllegalArgumentException(
"Cannot instantiate " + type + " : " + name, ex);
}
}
......@@ -416,7 +451,8 @@ public class SpringApplication {
* @see #configureProfiles(ConfigurableEnvironment, String[])
* @see #configurePropertySources(ConfigurableEnvironment, String[])
*/
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
protected void configureEnvironment(ConfigurableEnvironment environment,
String[] args) {
configurePropertySources(environment, args);
configureProfiles(environment, args);
}
......@@ -432,16 +468,16 @@ public class SpringApplication {
String[] args) {
MutablePropertySources sources = environment.getPropertySources();
if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
sources.addLast(new MapPropertySource("defaultProperties",
this.defaultProperties));
sources.addLast(
new MapPropertySource("defaultProperties", this.defaultProperties));
}
if (this.addCommandLineProperties && args.length > 0) {
String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
if (sources.contains(name)) {
PropertySource<?> source = sources.get(name);
CompositePropertySource composite = new CompositePropertySource(name);
composite.addPropertySource(new SimpleCommandLinePropertySource(name
+ "-" + args.hashCode(), args));
composite.addPropertySource(new SimpleCommandLinePropertySource(
name + "-" + args.hashCode(), args));
composite.addPropertySource(source);
sources.replace(name, composite);
}
......@@ -508,14 +544,14 @@ public class SpringApplication {
Class<?> contextClass = this.applicationContextClass;
if (contextClass == null) {
try {
contextClass = Class
.forName(this.webEnvironment ? DEFAULT_WEB_CONTEXT_CLASS
: DEFAULT_CONTEXT_CLASS);
contextClass = Class.forName(this.webEnvironment
? DEFAULT_WEB_CONTEXT_CLASS : DEFAULT_CONTEXT_CLASS);
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Unable create a default ApplicationContext, "
+ "please specify an ApplicationContextClass", ex);
+ "please specify an ApplicationContextClass",
ex);
}
}
return (ConfigurableApplicationContext) BeanUtils.instantiate(contextClass);
......@@ -544,8 +580,8 @@ public class SpringApplication {
.setResourceLoader(this.resourceLoader);
}
if (context instanceof DefaultResourceLoader) {
((DefaultResourceLoader) context).setClassLoader(this.resourceLoader
.getClassLoader());
((DefaultResourceLoader) context)
.setClassLoader(this.resourceLoader.getClassLoader());
}
}
}
......@@ -596,8 +632,8 @@ public class SpringApplication {
*/
protected void load(ApplicationContext context, Object[] sources) {
if (this.log.isDebugEnabled()) {
this.log.debug("Loading source "
+ StringUtils.arrayToCommaDelimitedString(sources));
this.log.debug(
"Loading source " + StringUtils.arrayToCommaDelimitedString(sources));
}
BeanDefinitionLoader loader = createBeanDefinitionLoader(
getBeanDefinitionRegistry(context), sources);
......@@ -1018,8 +1054,8 @@ public class SpringApplication {
try {
List<ExitCodeGenerator> generators = new ArrayList<ExitCodeGenerator>();
generators.addAll(Arrays.asList(exitCodeGenerators));
generators.addAll(context.getBeansOfType(ExitCodeGenerator.class)
.values());
generators
.addAll(context.getBeansOfType(ExitCodeGenerator.class).values());
exitCode = getExitCode(generators);
}
finally {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment