Browse Source

Avoid unnecessary creation of default StandardEnvironment instances

Issue: SPR-10568
pull/292/head
Juergen Hoeller 12 years ago
parent
commit
7e01578515
  1. 4
      spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinitionReader.java
  2. 7
      spring-beans/src/main/java/org/springframework/beans/support/ResourceEditorRegistrar.java
  3. 13
      spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java
  4. 4
      spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java
  5. 60
      spring-core/src/main/java/org/springframework/core/env/PropertySourcesPropertyResolver.java
  6. 42
      spring-core/src/main/java/org/springframework/core/io/ResourceEditor.java
  7. 46
      spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java

4
spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinitionReader.java

@ -1,5 +1,5 @@ @@ -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.
@ -54,7 +54,7 @@ public abstract class AbstractBeanDefinitionReader implements EnvironmentCapable @@ -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();

7
spring-beans/src/main/java/org/springframework/beans/support/ResourceEditorRegistrar.java

@ -1,5 +1,5 @@ @@ -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,9 +78,12 @@ public class ResourceEditorRegistrar implements PropertyEditorRegistrar { @@ -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
*/

13
spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java

@ -1,5 +1,5 @@ @@ -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.
@ -90,7 +90,7 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC @@ -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 @@ -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();

4
spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java vendored

@ -1,5 +1,5 @@ @@ -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.
@ -452,7 +452,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment { @@ -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) {

60
spring-core/src/main/java/org/springframework/core/env/PropertySourcesPropertyResolver.java vendored

@ -1,5 +1,5 @@ @@ -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,8 +16,6 @@ @@ -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 { @@ -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,34 +42,34 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver { @@ -43,34 +42,34 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
this.propertySources = propertySources;
}
@Override
public boolean containsProperty(String key) {
if (this.propertySources != null) {
for (PropertySource<?> propertySource : this.propertySources) {
if (propertySource.containsProperty(key)) {
return true;
}
}
}
return false;
}
@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()));
}
if (this.propertySources != null) {
for (PropertySource<?> propertySource : this.propertySources) {
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;
if ((value = propertySource.getProperty(key)) != null) {
@ -79,21 +78,20 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver { @@ -79,21 +78,20 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
value = this.resolveNestedPlaceholders((String) value);
}
if (debugEnabled) {
logger.debug(
format("Found key '%s' in [%s] with type [%s] and value '%s'",
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(
format("Cannot convert value [%s] from source type [%s] to target type [%s]",
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);
}
}
}
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,34 +100,33 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver { @@ -102,34 +100,33 @@ 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()));
}
if (this.propertySources != null) {
for (PropertySource<?> propertySource : this.propertySources) {
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;
if ((value = propertySource.getProperty(key)) != null) {
Object value = propertySource.getProperty(key);
if (value != null) {
if (debugEnabled) {
logger.debug(
format("Found key '%s' in [%s] with value '%s'", key, propertySource.getName(), value));
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) {
}
catch (Exception ex) {
throw new ClassConversionException((String)value, targetValueType, ex);
}
}
else if (value instanceof Class) {
clazz = (Class<?>)value;
} else {
}
else {
clazz = value.getClass();
}
if (!targetValueType.isAssignableFrom(clazz)) {
throw new ClassConversionException(clazz, targetValueType);
}
@ -138,15 +135,17 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver { @@ -138,15 +135,17 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
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 { @@ -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);
}
}
}

42
spring-core/src/main/java/org/springframework/core/io/ResourceEditor.java

@ -1,5 +1,5 @@ @@ -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.
@ -19,12 +19,11 @@ package org.springframework.core.io; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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));
}

46
spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java

@ -1,5 +1,5 @@ @@ -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.
@ -25,10 +25,12 @@ import java.util.List; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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));
}
}

Loading…
Cancel
Save