Avoid unnecessary creation of default StandardEnvironment instances

Issue: SPR-10568
This commit is contained in:
Juergen Hoeller 2013-05-15 23:41:32 +02:00
parent 7cddeb6db9
commit 7e01578515
7 changed files with 137 additions and 119 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -54,7 +54,7 @@ public abstract class AbstractBeanDefinitionReader implements EnvironmentCapable
private ClassLoader beanClassLoader; private ClassLoader beanClassLoader;
private Environment environment = new StandardEnvironment(); private Environment environment;
private BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator(); private BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator();

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -78,9 +78,12 @@ public class ResourceEditorRegistrar implements PropertyEditorRegistrar {
} }
/** /**
* Create a new ResourceEditorRegistrar for the given ResourceLoader * Create a new ResourceEditorRegistrar for the given {@link ResourceLoader}
* and {@link PropertyResolver}.
* @param resourceLoader the ResourceLoader (or ResourcePatternResolver) * @param resourceLoader the ResourceLoader (or ResourcePatternResolver)
* to create editors for (usually an ApplicationContext) * to create editors for (usually an ApplicationContext)
* @param propertyResolver the PropertyResolver (usually an Environment)
* @see org.springframework.core.env.Environment
* @see org.springframework.core.io.support.ResourcePatternResolver * @see org.springframework.core.io.support.ResourcePatternResolver
* @see org.springframework.context.ApplicationContext * @see org.springframework.context.ApplicationContext
*/ */

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -90,7 +90,7 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
/** /**
* Create a ClassPathScanningCandidateComponentProvider. * Create a ClassPathScanningCandidateComponentProvider with a {@link StandardEnvironment}.
* @param useDefaultFilters whether to register the default filters for the * @param useDefaultFilters whether to register the default filters for the
* {@link Component @Component}, {@link Repository @Repository}, * {@link Component @Component}, {@link Repository @Repository},
* {@link Service @Service}, and {@link Controller @Controller} * {@link Service @Service}, and {@link Controller @Controller}
@ -101,6 +101,15 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
this(useDefaultFilters, new StandardEnvironment()); this(useDefaultFilters, new StandardEnvironment());
} }
/**
* Create a ClassPathScanningCandidateComponentProvider with the given {@link Environment}.
* @param useDefaultFilters whether to register the default filters for the
* {@link Component @Component}, {@link Repository @Repository},
* {@link Service @Service}, and {@link Controller @Controller}
* stereotype annotations
* @param environment the Environment to use
* @see #registerDefaultFilters()
*/
public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, Environment environment) { public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, Environment environment) {
if (useDefaultFilters) { if (useDefaultFilters) {
registerDefaultFilters(); registerDefaultFilters();

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -452,7 +452,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
@Override @Override
public <T> T getProperty(String key, Class<T> targetType, T defaultValue) { public <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
return this.propertyResolver.getProperty(key, targetType, defaultValue); return this.propertyResolver.getProperty(key, targetType, defaultValue);
}; }
@Override @Override
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetType) { public <T> Class<T> getPropertyAsClass(String key, Class<T> targetType) {

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,8 +16,6 @@
package org.springframework.core.env; package org.springframework.core.env;
import static java.lang.String.format;
import org.springframework.core.convert.ConversionException; import org.springframework.core.convert.ConversionException;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
@ -35,6 +33,7 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
private final PropertySources propertySources; private final PropertySources propertySources;
/** /**
* Create a new resolver against the given property sources. * Create a new resolver against the given property sources.
* @param propertySources the set of {@link PropertySource} objects to use * @param propertySources the set of {@link PropertySource} objects to use
@ -43,34 +42,34 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
this.propertySources = propertySources; this.propertySources = propertySources;
} }
@Override @Override
public boolean containsProperty(String key) { public boolean containsProperty(String key) {
if (this.propertySources != null) {
for (PropertySource<?> propertySource : this.propertySources) { for (PropertySource<?> propertySource : this.propertySources) {
if (propertySource.containsProperty(key)) { if (propertySource.containsProperty(key)) {
return true; return true;
} }
} }
}
return false; return false;
} }
@Override @Override
public String getProperty(String key) { public String getProperty(String key) {
if (logger.isTraceEnabled()) { return getProperty(key, String.class);
logger.trace(format("getProperty(\"%s\") (implicit targetType [String])", key));
}
return this.getProperty(key, String.class);
} }
@Override @Override
public <T> T getProperty(String key, Class<T> targetValueType) { public <T> T getProperty(String key, Class<T> targetValueType) {
boolean debugEnabled = logger.isDebugEnabled(); boolean debugEnabled = logger.isDebugEnabled();
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace(format("getProperty(\"%s\", %s)", key, targetValueType.getSimpleName())); logger.trace(String.format("getProperty(\"%s\", %s)", key, targetValueType.getSimpleName()));
} }
if (this.propertySources != null) {
for (PropertySource<?> propertySource : this.propertySources) { for (PropertySource<?> propertySource : this.propertySources) {
if (debugEnabled) { if (debugEnabled) {
logger.debug(format("Searching for key '%s' in [%s]", key, propertySource.getName())); logger.debug(String.format("Searching for key '%s' in [%s]", key, propertySource.getName()));
} }
Object value; Object value;
if ((value = propertySource.getProperty(key)) != null) { if ((value = propertySource.getProperty(key)) != null) {
@ -79,21 +78,20 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
value = this.resolveNestedPlaceholders((String) value); value = this.resolveNestedPlaceholders((String) value);
} }
if (debugEnabled) { if (debugEnabled) {
logger.debug( logger.debug(String.format("Found key '%s' in [%s] with type [%s] and value '%s'",
format("Found key '%s' in [%s] with type [%s] and value '%s'",
key, propertySource.getName(), valueType.getSimpleName(), value)); key, propertySource.getName(), valueType.getSimpleName(), value));
} }
if (!this.conversionService.canConvert(valueType, targetValueType)) { if (!this.conversionService.canConvert(valueType, targetValueType)) {
throw new IllegalArgumentException( throw new IllegalArgumentException(String.format(
format("Cannot convert value [%s] from source type [%s] to target type [%s]", "Cannot convert value [%s] from source type [%s] to target type [%s]",
value, valueType.getSimpleName(), targetValueType.getSimpleName())); value, valueType.getSimpleName(), targetValueType.getSimpleName()));
} }
return conversionService.convert(value, targetValueType); return conversionService.convert(value, targetValueType);
} }
} }
}
if (debugEnabled) { if (debugEnabled) {
logger.debug(format("Could not find key '%s' in any property source. Returning [null]", key)); logger.debug(String.format("Could not find key '%s' in any property source. Returning [null]", key));
} }
return null; return null;
} }
@ -102,34 +100,33 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetValueType) { public <T> Class<T> getPropertyAsClass(String key, Class<T> targetValueType) {
boolean debugEnabled = logger.isDebugEnabled(); boolean debugEnabled = logger.isDebugEnabled();
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace(format("getPropertyAsClass(\"%s\", %s)", key, targetValueType.getSimpleName())); logger.trace(String.format("getPropertyAsClass(\"%s\", %s)", key, targetValueType.getSimpleName()));
} }
if (this.propertySources != null) {
for (PropertySource<?> propertySource : this.propertySources) { for (PropertySource<?> propertySource : this.propertySources) {
if (debugEnabled) { if (debugEnabled) {
logger.debug(format("Searching for key '%s' in [%s]", key, propertySource.getName())); logger.debug(String.format("Searching for key '%s' in [%s]", key, propertySource.getName()));
} }
Object value; Object value = propertySource.getProperty(key);
if ((value = propertySource.getProperty(key)) != null) { if (value != null) {
if (debugEnabled) { if (debugEnabled) {
logger.debug( logger.debug(String.format("Found key '%s' in [%s] with value '%s'", key, propertySource.getName(), value));
format("Found key '%s' in [%s] with value '%s'", key, propertySource.getName(), value));
} }
Class<?> clazz; Class<?> clazz;
if (value instanceof String) { if (value instanceof String) {
try { try {
clazz = ClassUtils.forName((String)value, null); clazz = ClassUtils.forName((String)value, null);
} catch (Exception ex) { }
catch (Exception ex) {
throw new ClassConversionException((String)value, targetValueType, ex); throw new ClassConversionException((String)value, targetValueType, ex);
} }
} }
else if (value instanceof Class) { else if (value instanceof Class) {
clazz = (Class<?>)value; clazz = (Class<?>)value;
} else { }
else {
clazz = value.getClass(); clazz = value.getClass();
} }
if (!targetValueType.isAssignableFrom(clazz)) { if (!targetValueType.isAssignableFrom(clazz)) {
throw new ClassConversionException(clazz, targetValueType); throw new ClassConversionException(clazz, targetValueType);
} }
@ -138,15 +135,17 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
return targetClass; return targetClass;
} }
} }
}
if (debugEnabled) { if (debugEnabled) {
logger.debug(format("Could not find key '%s' in any property source. Returning [null]", key)); logger.debug(String.format("Could not find key '%s' in any property source. Returning [null]", key));
} }
return null; return null;
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
static class ClassConversionException extends ConversionException { private static class ClassConversionException extends ConversionException {
public ClassConversionException(Class<?> actual, Class<?> expected) { public ClassConversionException(Class<?> actual, Class<?> expected) {
super(String.format("Actual type %s is not assignable to expected type %s", actual.getName(), expected.getName())); super(String.format("Actual type %s is not assignable to expected type %s", actual.getName(), expected.getName()));
} }
@ -155,4 +154,5 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
super(String.format("Could not find/load class %s during attempt to convert to %s", actual, expected.getName()), ex); super(String.format("Could not find/load class %s during attempt to convert to %s", actual, expected.getName()), ex);
} }
} }
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,12 +19,11 @@ package org.springframework.core.io;
import java.beans.PropertyEditorSupport; import java.beans.PropertyEditorSupport;
import java.io.IOException; import java.io.IOException;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.env.PropertyResolver; import org.springframework.core.env.PropertyResolver;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
* {@link java.beans.PropertyEditor Editor} for {@link Resource} * {@link java.beans.PropertyEditor Editor} for {@link Resource}
* descriptors, to automatically convert {@code String} locations * descriptors, to automatically convert {@code String} locations
@ -51,7 +50,7 @@ public class ResourceEditor extends PropertyEditorSupport {
private final ResourceLoader resourceLoader; private final ResourceLoader resourceLoader;
private final PropertyResolver propertyResolver; private PropertyResolver propertyResolver;
private final boolean ignoreUnresolvablePlaceholders; private final boolean ignoreUnresolvablePlaceholders;
@ -61,7 +60,7 @@ public class ResourceEditor extends PropertyEditorSupport {
* using a {@link DefaultResourceLoader} and {@link StandardEnvironment}. * using a {@link DefaultResourceLoader} and {@link StandardEnvironment}.
*/ */
public ResourceEditor() { public ResourceEditor() {
this(new DefaultResourceLoader(), new StandardEnvironment()); this(new DefaultResourceLoader(), null);
} }
/** /**
@ -73,7 +72,21 @@ public class ResourceEditor extends PropertyEditorSupport {
*/ */
@Deprecated @Deprecated
public ResourceEditor(ResourceLoader resourceLoader) { public ResourceEditor(ResourceLoader resourceLoader) {
this(resourceLoader, new StandardEnvironment(), true); this(resourceLoader, null, true);
}
/**
* Create a new instance of the {@link ResourceEditor} class
* using the given {@link ResourceLoader}.
* @param resourceLoader the {@code ResourceLoader} to use
* @param ignoreUnresolvablePlaceholders whether to ignore unresolvable placeholders
* if no corresponding property could be found
* @deprecated as of Spring 3.1 in favor of
* {@link #ResourceEditor(ResourceLoader, PropertyResolver, boolean)}
*/
@Deprecated
public ResourceEditor(ResourceLoader resourceLoader, boolean ignoreUnresolvablePlaceholders) {
this(resourceLoader, null, ignoreUnresolvablePlaceholders);
} }
/** /**
@ -86,20 +99,6 @@ public class ResourceEditor extends PropertyEditorSupport {
this(resourceLoader, propertyResolver, true); this(resourceLoader, propertyResolver, true);
} }
/**
* Create a new instance of the {@link ResourceEditor} class
* using the given {@link ResourceLoader}.
* @param resourceLoader the {@code ResourceLoader} to use
* @param ignoreUnresolvablePlaceholders whether to ignore unresolvable placeholders
* if no corresponding property could be found
* @deprecated as of Spring 3.1 in favor of
* {@link #ResourceEditor(ResourceLoader, PropertyResolver, boolean)}
*/
@Deprecated
public ResourceEditor(ResourceLoader resourceLoader, boolean ignoreUnresolvablePlaceholders) {
this(resourceLoader, new StandardEnvironment(), ignoreUnresolvablePlaceholders);
}
/** /**
* Create a new instance of the {@link ResourceEditor} class * Create a new instance of the {@link ResourceEditor} class
* using the given {@link ResourceLoader}. * using the given {@link ResourceLoader}.
@ -110,7 +109,6 @@ public class ResourceEditor extends PropertyEditorSupport {
*/ */
public ResourceEditor(ResourceLoader resourceLoader, PropertyResolver propertyResolver, boolean ignoreUnresolvablePlaceholders) { public ResourceEditor(ResourceLoader resourceLoader, PropertyResolver propertyResolver, boolean ignoreUnresolvablePlaceholders) {
Assert.notNull(resourceLoader, "ResourceLoader must not be null"); Assert.notNull(resourceLoader, "ResourceLoader must not be null");
Assert.notNull(propertyResolver, "PropertyResolver must not be null");
this.resourceLoader = resourceLoader; this.resourceLoader = resourceLoader;
this.propertyResolver = propertyResolver; this.propertyResolver = propertyResolver;
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders; this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
@ -137,9 +135,11 @@ public class ResourceEditor extends PropertyEditorSupport {
* @see PropertyResolver#resolveRequiredPlaceholders * @see PropertyResolver#resolveRequiredPlaceholders
*/ */
protected String resolvePath(String path) { protected String resolvePath(String path) {
return this.ignoreUnresolvablePlaceholders ? if (this.propertyResolver == null) {
this.propertyResolver.resolvePlaceholders(path) : this.propertyResolver = new StandardEnvironment();
this.propertyResolver.resolveRequiredPlaceholders(path); }
return (this.ignoreUnresolvablePlaceholders ? this.propertyResolver.resolvePlaceholders(path) :
this.propertyResolver.resolveRequiredPlaceholders(path));
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,10 +25,12 @@ import java.util.List;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertyResolver; import org.springframework.core.env.PropertyResolver;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
/** /**
* Editor for {@link org.springframework.core.io.Resource} arrays, to * Editor for {@link org.springframework.core.io.Resource} arrays, to
@ -55,10 +57,10 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
private static final Log logger = LogFactory.getLog(ResourceArrayPropertyEditor.class); private static final Log logger = LogFactory.getLog(ResourceArrayPropertyEditor.class);
private final PropertyResolver propertyResolver;
private final ResourcePatternResolver resourcePatternResolver; private final ResourcePatternResolver resourcePatternResolver;
private PropertyResolver propertyResolver;
private final boolean ignoreUnresolvablePlaceholders; private final boolean ignoreUnresolvablePlaceholders;
@ -69,7 +71,7 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
* @see Environment * @see Environment
*/ */
public ResourceArrayPropertyEditor() { public ResourceArrayPropertyEditor() {
this(new PathMatchingResourcePatternResolver(), new StandardEnvironment(), true); this(new PathMatchingResourcePatternResolver(), null, true);
} }
/** /**
@ -80,7 +82,20 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
*/ */
@Deprecated @Deprecated
public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver) { public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver) {
this(resourcePatternResolver, new StandardEnvironment(), true); this(resourcePatternResolver, null, true);
}
/**
* Create a new ResourceArrayPropertyEditor with the given {@link ResourcePatternResolver}
* and a {@link StandardEnvironment}.
* @param resourcePatternResolver the ResourcePatternResolver to use
* @param ignoreUnresolvablePlaceholders whether to ignore unresolvable placeholders
* if no corresponding system property could be found
* @deprecated as of 3.1 in favor of {@link #ResourceArrayPropertyEditor(ResourcePatternResolver, PropertyResolver, boolean)}
*/
@Deprecated
public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver, boolean ignoreUnresolvablePlaceholders) {
this(resourcePatternResolver, null, ignoreUnresolvablePlaceholders);
} }
/** /**
@ -93,19 +108,6 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
this(resourcePatternResolver, propertyResolver, true); this(resourcePatternResolver, propertyResolver, true);
} }
/**
* Create a new ResourceArrayPropertyEditor with the given {@link ResourcePatternResolver}
* and a {@link StandardEnvironment}.
* @param resourcePatternResolver the ResourcePatternResolver to use
* @param ignoreUnresolvablePlaceholders whether to ignore unresolvable placeholders
* if no corresponding system property could be found
* @deprecated as of 3.1 in favor of {@link #ResourceArrayPropertyEditor(ResourcePatternResolver, PropertyResolver, boolean)}
*/
@Deprecated
public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver, boolean ignoreUnresolvablePlaceholders) {
this(resourcePatternResolver, new StandardEnvironment(), ignoreUnresolvablePlaceholders);
}
/** /**
* Create a new ResourceArrayPropertyEditor with the given {@link ResourcePatternResolver} * Create a new ResourceArrayPropertyEditor with the given {@link ResourcePatternResolver}
* and {@link PropertyResolver} (typically an {@link Environment}). * and {@link PropertyResolver} (typically an {@link Environment}).
@ -116,6 +118,8 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
*/ */
public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver, public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver,
PropertyResolver propertyResolver, boolean ignoreUnresolvablePlaceholders) { PropertyResolver propertyResolver, boolean ignoreUnresolvablePlaceholders) {
Assert.notNull(resourcePatternResolver, "ResourcePatternResolver must not be null");
this.resourcePatternResolver = resourcePatternResolver; this.resourcePatternResolver = resourcePatternResolver;
this.propertyResolver = propertyResolver; this.propertyResolver = propertyResolver;
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders; this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
@ -197,9 +201,11 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
* @see PropertyResolver#resolveRequiredPlaceholders(String) * @see PropertyResolver#resolveRequiredPlaceholders(String)
*/ */
protected String resolvePath(String path) { protected String resolvePath(String path) {
return this.ignoreUnresolvablePlaceholders ? if (this.propertyResolver == null) {
this.propertyResolver.resolvePlaceholders(path) : this.propertyResolver = new StandardEnvironment();
this.propertyResolver.resolveRequiredPlaceholders(path); }
return (this.ignoreUnresolvablePlaceholders ? this.propertyResolver.resolvePlaceholders(path) :
this.propertyResolver.resolveRequiredPlaceholders(path));
} }
} }