diff --git a/spring-test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java b/spring-test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java index 51c6369d79..8b497ccce4 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java @@ -61,11 +61,12 @@ import org.springframework.util.StringUtils; */ public abstract class AbstractContextLoader implements SmartContextLoader { - private static final Log logger = LogFactory.getLog(AbstractContextLoader.class); - private static final String[] EMPTY_STRING_ARRAY = new String[0]; + private static final String SLASH = "/"; + private static final Log logger = LogFactory.getLog(AbstractContextLoader.class); + // --- SmartContextLoader ----------------------------------------------- @@ -79,23 +80,19 @@ public abstract class AbstractContextLoader implements SmartContextLoader { * processed locations are then * {@link ContextConfigurationAttributes#setLocations(String[]) set} in * the supplied configuration attributes. - * *
Can be overridden in subclasses — for example, to process * annotated classes instead of resource locations. - * * @since 3.1 * @see #processLocations(Class, String...) */ public void processContextConfiguration(ContextConfigurationAttributes configAttributes) { - String[] processedLocations = processLocations(configAttributes.getDeclaringClass(), - configAttributes.getLocations()); + String[] processedLocations = processLocations(configAttributes.getDeclaringClass(), configAttributes.getLocations()); configAttributes.setLocations(processedLocations); } /** * Prepare the {@link ConfigurableApplicationContext} created by this * {@code SmartContextLoader} before bean definitions are read. - * *
The default implementation: *
Any {@code ApplicationContextInitializers} implementing
* {@link org.springframework.core.Ordered Ordered} or marked with {@link
* org.springframework.core.annotation.Order @Order} will be sorted appropriately.
- *
* @param context the newly created application context
* @param mergedConfig the merged context configuration
+ * @since 3.2
* @see ApplicationContextInitializer#initialize(ConfigurableApplicationContext)
* @see #loadContext(MergedContextConfiguration)
* @see ConfigurableApplicationContext#setId
- * @since 3.2
*/
@SuppressWarnings("unchecked")
protected void prepareContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
@@ -129,17 +124,18 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
return;
}
- final List For example, if the supplied class is {@code com.example.MyTest},
* the generated locations will contain a single string with a value of
* "classpath:/com/example/MyTest{@code As of Spring 3.1, the implementation of this method adheres to the
* contract defined in the {@link SmartContextLoader} SPI. Specifically,
* this method will preemptively verify that the generated default
* location actually exists. If it does not exist, this method will log a
* warning and return an empty array.
- *
* Subclasses can override this method to implement a different
* default location generation strategy.
- *
* @param clazz the class for which the default locations are to be generated
* @return an array of default application context resource locations
* @since 2.5
@@ -212,23 +203,22 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
if (classPathResource.exists()) {
if (logger.isInfoEnabled()) {
- logger.info(String.format("Detected default resource location \"%s\" for test class [%s].",
- prefixedResourcePath, clazz.getName()));
+ logger.info(String.format("Detected default resource location \"%s\" for test class [%s]",
+ prefixedResourcePath, clazz.getName()));
}
- return new String[] { prefixedResourcePath };
+ return new String[] {prefixedResourcePath};
}
-
- // else
- if (logger.isInfoEnabled()) {
- logger.info(String.format("Could not detect default resource locations for test class [%s]: "
- + "%s does not exist.", clazz.getName(), classPathResource));
+ else {
+ if (logger.isInfoEnabled()) {
+ logger.info(String.format("Could not detect default resource locations for test class [%s]: " +
+ "%s does not exist", clazz.getName(), classPathResource));
+ }
+ return EMPTY_STRING_ARRAY;
}
- return EMPTY_STRING_ARRAY;
}
/**
* Generate a modified version of the supplied locations array and return it.
- *
* A plain path — for example, "context.xml" — will
* be treated as a classpath resource that is relative to the package in which
* the specified class is defined. A path starting with a slash is treated
@@ -238,10 +228,8 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
* {@link ResourceUtils#CLASSPATH_URL_PREFIX classpath:},
* {@link ResourceUtils#FILE_URL_PREFIX file:}, {@code http:},
* etc.) will be added to the results unchanged.
- *
* Subclasses can override this method to implement a different
* location modification strategy.
- *
* @param clazz the class with which the locations are associated
* @param locations the resource locations to be modified
* @return an array of modified application context resource locations
@@ -253,10 +241,12 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
String path = locations[i];
if (path.startsWith(SLASH)) {
modifiedLocations[i] = ResourceUtils.CLASSPATH_URL_PREFIX + path;
- } else if (!ResourcePatternUtils.isUrl(path)) {
- modifiedLocations[i] = ResourceUtils.CLASSPATH_URL_PREFIX + SLASH
- + StringUtils.cleanPath(ClassUtils.classPackageAsResourcePath(clazz) + SLASH + path);
- } else {
+ }
+ else if (!ResourcePatternUtils.isUrl(path)) {
+ modifiedLocations[i] = ResourceUtils.CLASSPATH_URL_PREFIX + SLASH +
+ StringUtils.cleanPath(ClassUtils.classPackageAsResourcePath(clazz) + SLASH + path);
+ }
+ else {
modifiedLocations[i] = StringUtils.cleanPath(path);
}
}
@@ -267,7 +257,6 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
* Determine whether or not default resource locations should be
* generated if the {@code locations} provided to
* {@link #processLocations(Class, String...)} are {@code null} or empty.
- *
* As of Spring 3.1, the semantics of this method have been overloaded
* to include detection of either default resource locations or default
* configuration classes. Consequently, this method can also be used to
@@ -276,9 +265,7 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
* {@link ContextConfigurationAttributes configuration attributes} supplied
* to {@link #processContextConfiguration(ContextConfigurationAttributes)}
* are {@code null} or empty.
- *
* Can be overridden by subclasses to change the default behavior.
- *
* @return always {@code true} by default
* @since 2.5
*/
@@ -289,9 +276,7 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
/**
* Get the suffix to append to {@link ApplicationContext} resource locations
* when generating default locations.
- *
* Must be implemented by subclasses.
- *
* @return the resource suffix; should not be {@code null} or empty
* @since 2.5
* @see #generateDefaultLocations(Class)
diff --git a/spring-web/src/main/java/org/springframework/web/context/support/AbstractRefreshableWebApplicationContext.java b/spring-web/src/main/java/org/springframework/web/context/support/AbstractRefreshableWebApplicationContext.java
index 92b1f7907a..71d9ae9760 100644
--- a/spring-web/src/main/java/org/springframework/web/context/support/AbstractRefreshableWebApplicationContext.java
+++ b/spring-web/src/main/java/org/springframework/web/context/support/AbstractRefreshableWebApplicationContext.java
@@ -201,11 +201,9 @@ public abstract class AbstractRefreshableWebApplicationContext extends AbstractR
*/
@Override
protected void initPropertySources() {
- super.initPropertySources();
- ConfigurableEnvironment env = this.getEnvironment();
+ ConfigurableEnvironment env = getEnvironment();
if (env instanceof ConfigurableWebEnvironment) {
- ((ConfigurableWebEnvironment)env).initPropertySources(
- this.servletContext, this.servletConfig);
+ ((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig);
}
}
diff --git a/spring-web/src/main/java/org/springframework/web/context/support/GenericWebApplicationContext.java b/spring-web/src/main/java/org/springframework/web/context/support/GenericWebApplicationContext.java
index d30d3aef84..a5dc7951ca 100644
--- a/spring-web/src/main/java/org/springframework/web/context/support/GenericWebApplicationContext.java
+++ b/spring-web/src/main/java/org/springframework/web/context/support/GenericWebApplicationContext.java
@@ -28,7 +28,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.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext;
@@ -192,11 +191,9 @@ public class GenericWebApplicationContext extends GenericApplicationContext
*/
@Override
protected void initPropertySources() {
- super.initPropertySources();
- ConfigurableEnvironment env = this.getEnvironment();
+ ConfigurableEnvironment env = getEnvironment();
if (env instanceof ConfigurableWebEnvironment) {
- ((ConfigurableWebEnvironment)env).initPropertySources(
- this.servletContext, null);
+ ((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, null);
}
}
diff --git a/spring-web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java b/spring-web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java
index b929c4bbe3..21a1ce9cf1 100644
--- a/spring-web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java
+++ b/spring-web/src/main/java/org/springframework/web/context/support/StandardServletEnvironment.java
@@ -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.
@@ -41,8 +41,7 @@ import org.springframework.web.context.ConfigurableWebEnvironment;
* @since 3.1
* @see StandardEnvironment
*/
-public class StandardServletEnvironment extends StandardEnvironment
- implements ConfigurableWebEnvironment {
+public class StandardServletEnvironment extends StandardEnvironment implements ConfigurableWebEnvironment {
/** Servlet context init parameters property source name: {@value} */
public static final String SERVLET_CONTEXT_PROPERTY_SOURCE_NAME = "servletContextInitParams";
@@ -91,8 +90,7 @@ public class StandardServletEnvironment extends StandardEnvironment
}
public void initPropertySources(ServletContext servletContext, ServletConfig servletConfig) {
- WebApplicationContextUtils.initServletPropertySources(
- this.getPropertySources(), servletContext, servletConfig);
+ WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig);
}
}
diff --git a/spring-web/src/main/java/org/springframework/web/context/support/StaticWebApplicationContext.java b/spring-web/src/main/java/org/springframework/web/context/support/StaticWebApplicationContext.java
index 5d26a434e3..14c02bd07c 100644
--- a/spring-web/src/main/java/org/springframework/web/context/support/StaticWebApplicationContext.java
+++ b/spring-web/src/main/java/org/springframework/web/context/support/StaticWebApplicationContext.java
@@ -177,9 +177,8 @@ public class StaticWebApplicationContext extends StaticApplicationContext
@Override
protected void initPropertySources() {
- super.initPropertySources();
- WebApplicationContextUtils.initServletPropertySources(
- this.getEnvironment().getPropertySources(), this.servletContext, this.servletConfig);
+ WebApplicationContextUtils.initServletPropertySources(getEnvironment().getPropertySources(),
+ this.servletContext, this.servletConfig);
}
public Theme getTheme(String themeName) {
diff --git a/spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java b/spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java
index 9091e337a4..16d500e626 100644
--- a/spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java
+++ b/spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java
@@ -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.
@@ -16,15 +16,11 @@
package org.springframework.web.context.support;
-import static org.springframework.web.context.support.StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME;
-import static org.springframework.web.context.support.StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME;
-
import java.io.Serializable;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
-
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletConfig;
@@ -51,11 +47,11 @@ import org.springframework.web.context.request.WebRequest;
/**
* Convenience methods for retrieving the root
* {@link org.springframework.web.context.WebApplicationContext} for a given
- * {@code ServletContext}. This is e.g. useful for accessing a Spring
- * context from within custom web views or Struts actions.
+ * {@code ServletContext}. This is useful for programmatically accessing a
+ * Spring application context from within custom web views or MVC actions.
*
* Note that there are more convenient ways of accessing the root context for
- * many web frameworks, either part of Spring or available as external library.
+ * many web frameworks, either part of Spring or available as an external library.
* This helper class is just the most generic way to access the root context.
*
* @author Juergen Hoeller
@@ -63,7 +59,6 @@ import org.springframework.web.context.request.WebRequest;
* @see org.springframework.web.servlet.FrameworkServlet
* @see org.springframework.web.servlet.DispatcherServlet
* @see org.springframework.web.jsf.FacesContextUtils
- * @see org.springframework.web.jsf.SpringBeanVariableResolver
* @see org.springframework.web.jsf.el.SpringBeanFacesELResolver
*/
public abstract class WebApplicationContextUtils {
@@ -260,16 +255,17 @@ public abstract class WebApplicationContextUtils {
*/
public static void initServletPropertySources(
MutablePropertySources propertySources, ServletContext servletContext, ServletConfig servletConfig) {
+
Assert.notNull(propertySources, "propertySources must not be null");
- if(servletContext != null &&
- propertySources.contains(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) &&
- propertySources.get(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {
- propertySources.replace(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, new ServletContextPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, servletContext));
+ if (servletContext != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) &&
+ propertySources.get(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {
+ propertySources.replace(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME,
+ new ServletContextPropertySource(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, servletContext));
}
- if(servletConfig != null &&
- propertySources.contains(SERVLET_CONFIG_PROPERTY_SOURCE_NAME) &&
- propertySources.get(SERVLET_CONFIG_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {
- propertySources.replace(SERVLET_CONFIG_PROPERTY_SOURCE_NAME, new ServletConfigPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME, servletConfig));
+ if (servletConfig != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME) &&
+ propertySources.get(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {
+ propertySources.replace(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME,
+ new ServletConfigPropertySource(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME, servletConfig));
}
}
diff --git a/spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/context/StaticPortletApplicationContext.java b/spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/context/StaticPortletApplicationContext.java
index d1c1b2ad2a..d368f872a5 100644
--- a/spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/context/StaticPortletApplicationContext.java
+++ b/spring-webmvc-portlet/src/main/java/org/springframework/web/portlet/context/StaticPortletApplicationContext.java
@@ -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.
@@ -78,10 +78,8 @@ public class StaticPortletApplicationContext extends StaticApplicationContext
*/
@Override
protected void initPropertySources() {
- super.initPropertySources();
- PortletApplicationContextUtils.initPortletPropertySources(
- this.getEnvironment().getPropertySources(), this.servletContext,
- this.portletContext, this.portletConfig);
+ PortletApplicationContextUtils.initPortletPropertySources(getEnvironment().getPropertySources(),
+ this.servletContext, this.portletContext, this.portletConfig);
}
/**