Relax ConfigurableWebEnvironment signatures
ConfigurableWebEnvironment was introduced in 3.2.0.M1 with SPR-9439 in order to break a cyclic dependency. At the same time, certain signatures such as AbstractRefreshableWebApplicationContext#getEnviroment and GenericWebApplicationContext#getEnvironment were updated to take advantage of covariant return types and return this newer, more narrow type and providing cast-free calls to ConfigurableWebEnvironment methods where necessary. Similar changes were made to HttpServletBean in 3.2.0.M2 with SPR-9763. Narrowing #getEnvironment signatures in this fashion required enforcing at the #setEnvironment level that any Environment instance provided (explicitly or via the EnvironmentAware callback) must be an instance of ConfigurableWebEnvironment. This is a reasonable assertion in typical web application scenarios, but as SPR-10138 demonstrates, there are valid use cases in which one may want or need to inject a non-web ConfigurableEnvironment variant, e.g. during automated unit/integration testing. On review, it was never strictly necessary to narrow #getEnvironment signatures, although doing so did provided convenience and type safety. In order to maintain as flexible and backward-compatible an arrangement as possible, this commit relaxes these #getEnvironment signatures back to their original, pre-3.2 state. Namely, they now return ConfigurableEnvironment as opposed to ConfigurableWebEnvironment, and in accordance, all instanceof assertions have been removed or relaxed to ensure that injected Environment instances are of type ConfigurableEnvironment. These changes have been verified against David Winterfeldt's Spring by Example spring-rest-services project, as described at SPR-10138. Issue: SPR-10138, SPR-9763, SPR-9439
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -71,11 +71,6 @@ public interface ConfigurableWebApplicationContext extends WebApplicationContext
|
||||
*/
|
||||
ServletConfig getServletConfig();
|
||||
|
||||
/**
|
||||
* Return the {@link ConfigurableWebEnvironment} used by this web application context.
|
||||
*/
|
||||
ConfigurableWebEnvironment getEnvironment();
|
||||
|
||||
/**
|
||||
* Set the namespace for this web application context,
|
||||
* to be used for building a default context config location.
|
||||
|
||||
@@ -38,6 +38,7 @@ import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.access.ContextSingletonBeanFactoryLocator;
|
||||
import org.springframework.core.GenericTypeResolver;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -490,7 +491,10 @@ public class ContextLoader {
|
||||
initializerInstances.add(BeanUtils.instantiateClass(initializerClass));
|
||||
}
|
||||
|
||||
applicationContext.getEnvironment().initPropertySources(servletContext, null);
|
||||
ConfigurableEnvironment env = applicationContext.getEnvironment();
|
||||
if (env instanceof ConfigurableWebEnvironment) {
|
||||
((ConfigurableWebEnvironment)env).initPropertySources(servletContext, null);
|
||||
}
|
||||
|
||||
Collections.sort(initializerInstances, new AnnotationAwareOrderComparator());
|
||||
for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : initializerInstances) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -27,7 +27,6 @@ import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
import org.springframework.ui.context.Theme;
|
||||
import org.springframework.ui.context.ThemeSource;
|
||||
import org.springframework.ui.context.support.UiApplicationContextUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.context.ConfigurableWebApplicationContext;
|
||||
import org.springframework.web.context.ConfigurableWebEnvironment;
|
||||
import org.springframework.web.context.ServletConfigAware;
|
||||
@@ -157,15 +156,6 @@ public abstract class AbstractRefreshableWebApplicationContext extends AbstractR
|
||||
return new StandardServletEnvironment();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigurableWebEnvironment getEnvironment() {
|
||||
ConfigurableEnvironment env = super.getEnvironment();
|
||||
Assert.isInstanceOf(ConfigurableWebEnvironment.class, env,
|
||||
"ConfigurableWebApplicationContext environment must be of type " +
|
||||
"ConfigurableWebEnvironment");
|
||||
return (ConfigurableWebEnvironment) env;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register request/session scopes, a {@link ServletContextAwareProcessor}, etc.
|
||||
*/
|
||||
@@ -212,7 +202,11 @@ public abstract class AbstractRefreshableWebApplicationContext extends AbstractR
|
||||
@Override
|
||||
protected void initPropertySources() {
|
||||
super.initPropertySources();
|
||||
this.getEnvironment().initPropertySources(this.servletContext, this.servletConfig);
|
||||
ConfigurableEnvironment env = this.getEnvironment();
|
||||
if (env instanceof ConfigurableWebEnvironment) {
|
||||
((ConfigurableWebEnvironment)env).initPropertySources(
|
||||
this.servletContext, this.servletConfig);
|
||||
}
|
||||
}
|
||||
|
||||
public Theme getTheme(String themeName) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -147,15 +147,6 @@ public class GenericWebApplicationContext extends GenericApplicationContext
|
||||
return new StandardServletEnvironment();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigurableWebEnvironment getEnvironment() {
|
||||
ConfigurableEnvironment env = super.getEnvironment();
|
||||
Assert.isInstanceOf(ConfigurableWebEnvironment.class, env,
|
||||
"ConfigurableWebApplicationContext environment must be of type " +
|
||||
"ConfigurableWebEnvironment");
|
||||
return (ConfigurableWebEnvironment) env;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register ServletContextAwareProcessor.
|
||||
* @see ServletContextAwareProcessor
|
||||
@@ -202,7 +193,11 @@ public class GenericWebApplicationContext extends GenericApplicationContext
|
||||
@Override
|
||||
protected void initPropertySources() {
|
||||
super.initPropertySources();
|
||||
this.getEnvironment().initPropertySources(this.servletContext, null);
|
||||
ConfigurableEnvironment env = this.getEnvironment();
|
||||
if (env instanceof ConfigurableWebEnvironment) {
|
||||
((ConfigurableWebEnvironment)env).initPropertySources(
|
||||
this.servletContext, null);
|
||||
}
|
||||
}
|
||||
|
||||
public Theme getTheme(String themeName) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -27,9 +27,7 @@ import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
import org.springframework.ui.context.Theme;
|
||||
import org.springframework.ui.context.ThemeSource;
|
||||
import org.springframework.ui.context.support.UiApplicationContextUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.context.ConfigurableWebApplicationContext;
|
||||
import org.springframework.web.context.ConfigurableWebEnvironment;
|
||||
import org.springframework.web.context.ServletConfigAware;
|
||||
import org.springframework.web.context.ServletContextAware;
|
||||
|
||||
@@ -169,15 +167,6 @@ public class StaticWebApplicationContext extends StaticApplicationContext
|
||||
return new StandardServletEnvironment();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigurableWebEnvironment getEnvironment() {
|
||||
ConfigurableEnvironment env = super.getEnvironment();
|
||||
Assert.isInstanceOf(ConfigurableWebEnvironment.class, env,
|
||||
"ConfigurableWebApplication environment must be of type " +
|
||||
"ConfigurableWebEnvironment");
|
||||
return (ConfigurableWebEnvironment) env;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the theme capability.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user