提交 7e015785 编写于 作者: J Juergen Hoeller

Avoid unnecessary creation of default StandardEnvironment instances

Issue: SPR-10568
上级 7cddeb6d
/*
* 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.
......@@ -54,7 +54,7 @@ public abstract class AbstractBeanDefinitionReader implements EnvironmentCapable
private ClassLoader beanClassLoader;
private Environment environment = new StandardEnvironment();
private Environment environment;
private BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator();
......@@ -90,7 +90,7 @@ public abstract class AbstractBeanDefinitionReader implements EnvironmentCapable
// Inherit Environment if possible
if (this.registry instanceof EnvironmentCapable) {
this.environment = ((EnvironmentCapable)this.registry).getEnvironment();
this.environment = ((EnvironmentCapable) this.registry).getEnvironment();
}
else {
this.environment = new StandardEnvironment();
......
/*
* 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,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)
* 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.context.ApplicationContext
*/
......
/*
* 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.
......@@ -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
* {@link Component @Component}, {@link Repository @Repository},
* {@link Service @Service}, and {@link Controller @Controller}
......@@ -101,6 +101,15 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
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) {
if (useDefaultFilters) {
registerDefaultFilters();
......
/*
* 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.
......@@ -452,7 +452,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
@Override
public <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
return this.propertyResolver.getProperty(key, targetType, defaultValue);
};
}
@Override
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetType) {
......
/*
* 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,8 +16,6 @@
package org.springframework.core.env;
import static java.lang.String.format;
import org.springframework.core.convert.ConversionException;
import org.springframework.util.ClassUtils;
......@@ -35,6 +33,7 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
private final PropertySources propertySources;
/**
* Create a new resolver against the given property sources.
* @param propertySources the set of {@link PropertySource} objects to use
......@@ -43,11 +42,14 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
this.propertySources = propertySources;
}
@Override
public boolean containsProperty(String key) {
for (PropertySource<?> propertySource : this.propertySources) {
if (propertySource.containsProperty(key)) {
return true;
if (this.propertySources != null) {
for (PropertySource<?> propertySource : this.propertySources) {
if (propertySource.containsProperty(key)) {
return true;
}
}
}
return false;
......@@ -55,45 +57,41 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
@Override
public String getProperty(String key) {
if (logger.isTraceEnabled()) {
logger.trace(format("getProperty(\"%s\") (implicit targetType [String])", key));
}
return this.getProperty(key, String.class);
return getProperty(key, String.class);
}
@Override
public <T> T getProperty(String key, Class<T> targetValueType) {
boolean debugEnabled = logger.isDebugEnabled();
if (logger.isTraceEnabled()) {
logger.trace(format("getProperty(\"%s\", %s)", key, targetValueType.getSimpleName()));
logger.trace(String.format("getProperty(\"%s\", %s)", key, targetValueType.getSimpleName()));
}
for (PropertySource<?> propertySource : this.propertySources) {
if (debugEnabled) {
logger.debug(format("Searching for key '%s' in [%s]", key, propertySource.getName()));
}
Object value;
if ((value = propertySource.getProperty(key)) != null) {
Class<?> valueType = value.getClass();
if (String.class.equals(valueType)) {
value = this.resolveNestedPlaceholders((String) value);
}
if (this.propertySources != null) {
for (PropertySource<?> propertySource : this.propertySources) {
if (debugEnabled) {
logger.debug(
format("Found key '%s' in [%s] with type [%s] and value '%s'",
key, propertySource.getName(), valueType.getSimpleName(), value));
logger.debug(String.format("Searching for key '%s' in [%s]", key, propertySource.getName()));
}
if (!this.conversionService.canConvert(valueType, targetValueType)) {
throw new IllegalArgumentException(
format("Cannot convert value [%s] from source type [%s] to target type [%s]",
value, valueType.getSimpleName(), targetValueType.getSimpleName()));
Object value;
if ((value = propertySource.getProperty(key)) != null) {
Class<?> valueType = value.getClass();
if (String.class.equals(valueType)) {
value = this.resolveNestedPlaceholders((String) value);
}
if (debugEnabled) {
logger.debug(String.format("Found key '%s' in [%s] with type [%s] and value '%s'",
key, propertySource.getName(), valueType.getSimpleName(), value));
}
if (!this.conversionService.canConvert(valueType, targetValueType)) {
throw new IllegalArgumentException(String.format(
"Cannot convert value [%s] from source type [%s] to target type [%s]",
value, valueType.getSimpleName(), targetValueType.getSimpleName()));
}
return conversionService.convert(value, targetValueType);
}
return conversionService.convert(value, targetValueType);
}
}
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;
}
......@@ -102,51 +100,52 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetValueType) {
boolean debugEnabled = logger.isDebugEnabled();
if (logger.isTraceEnabled()) {
logger.trace(format("getPropertyAsClass(\"%s\", %s)", key, targetValueType.getSimpleName()));
logger.trace(String.format("getPropertyAsClass(\"%s\", %s)", key, targetValueType.getSimpleName()));
}
for (PropertySource<?> propertySource : this.propertySources) {
if (debugEnabled) {
logger.debug(format("Searching for key '%s' in [%s]", key, propertySource.getName()));
}
Object value;
if ((value = propertySource.getProperty(key)) != null) {
if (this.propertySources != null) {
for (PropertySource<?> propertySource : this.propertySources) {
if (debugEnabled) {
logger.debug(
format("Found key '%s' in [%s] with value '%s'", key, propertySource.getName(), value));
logger.debug(String.format("Searching for key '%s' in [%s]", key, propertySource.getName()));
}
Class<?> clazz;
if (value instanceof String) {
try {
clazz = ClassUtils.forName((String)value, null);
} catch (Exception ex) {
throw new ClassConversionException((String)value, targetValueType, ex);
Object value = propertySource.getProperty(key);
if (value != null) {
if (debugEnabled) {
logger.debug(String.format("Found key '%s' in [%s] with value '%s'", key, propertySource.getName(), value));
}
Class<?> clazz;
if (value instanceof String) {
try {
clazz = ClassUtils.forName((String)value, null);
}
catch (Exception ex) {
throw new ClassConversionException((String)value, targetValueType, ex);
}
}
else if (value instanceof Class) {
clazz = (Class<?>)value;
}
else {
clazz = value.getClass();
}
if (!targetValueType.isAssignableFrom(clazz)) {
throw new ClassConversionException(clazz, targetValueType);
}
@SuppressWarnings("unchecked")
Class<T> targetClass = (Class<T>) clazz;
return targetClass;
}
else if (value instanceof Class) {
clazz = (Class<?>)value;
} else {
clazz = value.getClass();
}
if (!targetValueType.isAssignableFrom(clazz)) {
throw new ClassConversionException(clazz, targetValueType);
}
@SuppressWarnings("unchecked")
Class<T> targetClass = (Class<T>)clazz;
return targetClass;
}
}
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;
}
@SuppressWarnings("serial")
static class ClassConversionException extends ConversionException {
private static class ClassConversionException extends ConversionException {
public ClassConversionException(Class<?> actual, Class<?> expected) {
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);
}
}
}
/*
* 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.
......@@ -19,12 +19,11 @@ package org.springframework.core.io;
import java.beans.PropertyEditorSupport;
import java.io.IOException;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.env.PropertyResolver;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* {@link java.beans.PropertyEditor Editor} for {@link Resource}
* descriptors, to automatically convert {@code String} locations
......@@ -51,7 +50,7 @@ public class ResourceEditor extends PropertyEditorSupport {
private final ResourceLoader resourceLoader;
private final PropertyResolver propertyResolver;
private PropertyResolver propertyResolver;
private final boolean ignoreUnresolvablePlaceholders;
......@@ -61,7 +60,7 @@ public class ResourceEditor extends PropertyEditorSupport {
* using a {@link DefaultResourceLoader} and {@link StandardEnvironment}.
*/
public ResourceEditor() {
this(new DefaultResourceLoader(), new StandardEnvironment());
this(new DefaultResourceLoader(), null);
}
/**
......@@ -73,17 +72,7 @@ public class ResourceEditor extends PropertyEditorSupport {
*/
@Deprecated
public ResourceEditor(ResourceLoader resourceLoader) {
this(resourceLoader, new StandardEnvironment(), true);
}
/**
* Create a new instance of the {@link ResourceEditor} class
* using the given {@link ResourceLoader} and {@link PropertyResolver}.
* @param resourceLoader the {@code ResourceLoader} to use
* @param propertyResolver the {@code PropertyResolver} to use
*/
public ResourceEditor(ResourceLoader resourceLoader, PropertyResolver propertyResolver) {
this(resourceLoader, propertyResolver, true);
this(resourceLoader, null, true);
}
/**
......@@ -97,7 +86,17 @@ public class ResourceEditor extends PropertyEditorSupport {
*/
@Deprecated
public ResourceEditor(ResourceLoader resourceLoader, boolean ignoreUnresolvablePlaceholders) {
this(resourceLoader, new StandardEnvironment(), ignoreUnresolvablePlaceholders);
this(resourceLoader, null, ignoreUnresolvablePlaceholders);
}
/**
* Create a new instance of the {@link ResourceEditor} class
* using the given {@link ResourceLoader} and {@link PropertyResolver}.
* @param resourceLoader the {@code ResourceLoader} to use
* @param propertyResolver the {@code PropertyResolver} to use
*/
public ResourceEditor(ResourceLoader resourceLoader, PropertyResolver propertyResolver) {
this(resourceLoader, propertyResolver, true);
}
/**
......@@ -110,7 +109,6 @@ public class ResourceEditor extends PropertyEditorSupport {
*/
public ResourceEditor(ResourceLoader resourceLoader, PropertyResolver propertyResolver, boolean ignoreUnresolvablePlaceholders) {
Assert.notNull(resourceLoader, "ResourceLoader must not be null");
Assert.notNull(propertyResolver, "PropertyResolver must not be null");
this.resourceLoader = resourceLoader;
this.propertyResolver = propertyResolver;
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
......@@ -137,9 +135,11 @@ public class ResourceEditor extends PropertyEditorSupport {
* @see PropertyResolver#resolveRequiredPlaceholders
*/
protected String resolvePath(String path) {
return this.ignoreUnresolvablePlaceholders ?
this.propertyResolver.resolvePlaceholders(path) :
this.propertyResolver.resolveRequiredPlaceholders(path);
if (this.propertyResolver == null) {
this.propertyResolver = new StandardEnvironment();
}
return (this.ignoreUnresolvablePlaceholders ? this.propertyResolver.resolvePlaceholders(path) :
this.propertyResolver.resolveRequiredPlaceholders(path));
}
......
/*
* 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.
......@@ -25,10 +25,12 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertyResolver;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
/**
* 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 final PropertyResolver propertyResolver;
private final ResourcePatternResolver resourcePatternResolver;
private PropertyResolver propertyResolver;
private final boolean ignoreUnresolvablePlaceholders;
......@@ -69,7 +71,7 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
* @see Environment
*/
public ResourceArrayPropertyEditor() {
this(new PathMatchingResourcePatternResolver(), new StandardEnvironment(), true);
this(new PathMatchingResourcePatternResolver(), null, true);
}
/**
......@@ -80,17 +82,7 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
*/
@Deprecated
public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver) {
this(resourcePatternResolver, new StandardEnvironment(), true);
}
/**
* Create a new ResourceArrayPropertyEditor with the given {@link ResourcePatternResolver}
* and {@link PropertyResolver} (typically an {@link Environment}).
* @param resourcePatternResolver the ResourcePatternResolver to use
* @param propertyResolver the PropertyResolver to use
*/
public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver, PropertyResolver propertyResolver) {
this(resourcePatternResolver, propertyResolver, true);
this(resourcePatternResolver, null, true);
}
/**
......@@ -103,7 +95,17 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
*/
@Deprecated
public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver, boolean ignoreUnresolvablePlaceholders) {
this(resourcePatternResolver, new StandardEnvironment(), ignoreUnresolvablePlaceholders);
this(resourcePatternResolver, null, ignoreUnresolvablePlaceholders);
}
/**
* Create a new ResourceArrayPropertyEditor with the given {@link ResourcePatternResolver}
* and {@link PropertyResolver} (typically an {@link Environment}).
* @param resourcePatternResolver the ResourcePatternResolver to use
* @param propertyResolver the PropertyResolver to use
*/
public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver, PropertyResolver propertyResolver) {
this(resourcePatternResolver, propertyResolver, true);
}
/**
......@@ -116,6 +118,8 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
*/
public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver,
PropertyResolver propertyResolver, boolean ignoreUnresolvablePlaceholders) {
Assert.notNull(resourcePatternResolver, "ResourcePatternResolver must not be null");
this.resourcePatternResolver = resourcePatternResolver;
this.propertyResolver = propertyResolver;
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
......@@ -197,9 +201,11 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
* @see PropertyResolver#resolveRequiredPlaceholders(String)
*/
protected String resolvePath(String path) {
return this.ignoreUnresolvablePlaceholders ?
this.propertyResolver.resolvePlaceholders(path) :
this.propertyResolver.resolveRequiredPlaceholders(path);
if (this.propertyResolver == null) {
this.propertyResolver = new StandardEnvironment();
}
return (this.ignoreUnresolvablePlaceholders ? this.propertyResolver.resolvePlaceholders(path) :
this.propertyResolver.resolveRequiredPlaceholders(path));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册