Merge 3.1.0 development branch into trunk
Branch in question is 'env' branch from git://git.springsource.org/sandbox/cbeams.git; merged into
git-svn repository with:
git merge -s recursive -Xtheirs --no-commit env
No merge conflicts, but did need to
git rm spring-build
prior to committing.
With this change, Spring 3.1.0 development is now happening on SVN
trunk. Further commits to the 3.0.x line will happen in an as-yet
uncreated SVN branch. 3.1.0 snapshots will be available
per the usual nightly CI build from trunk.
This commit is contained in:
@@ -87,6 +87,7 @@ import org.springframework.beans.BeansException;
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
* @since 13 April 2001
|
||||
* @see BeanNameAware#setBeanName
|
||||
* @see BeanClassLoaderAware#setBeanClassLoader
|
||||
@@ -113,7 +114,6 @@ public interface BeanFactory {
|
||||
*/
|
||||
String FACTORY_BEAN_PREFIX = "&";
|
||||
|
||||
|
||||
/**
|
||||
* Return an instance, which may be shared or independent, of the specified bean.
|
||||
* <p>This method allows a Spring BeanFactory to be used as a replacement for the
|
||||
@@ -240,7 +240,7 @@ public interface BeanFactory {
|
||||
* @see #getBean
|
||||
* @see #getType
|
||||
*/
|
||||
boolean isTypeMatch(String name, Class targetType) throws NoSuchBeanDefinitionException;
|
||||
boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException;
|
||||
|
||||
/**
|
||||
* Determine the type of the bean with the given name. More specifically,
|
||||
|
||||
@@ -110,7 +110,7 @@ public interface ListableBeanFactory extends BeanFactory {
|
||||
* @see FactoryBean#getObjectType
|
||||
* @see BeanFactoryUtils#beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class)
|
||||
*/
|
||||
String[] getBeanNamesForType(Class type);
|
||||
String[] getBeanNamesForType(Class<?> type);
|
||||
|
||||
/**
|
||||
* Return the names of beans matching the given type (including subclasses),
|
||||
@@ -123,7 +123,7 @@ public interface ListableBeanFactory extends BeanFactory {
|
||||
* FactoryBean doesn't match, the raw FactoryBean itself will be matched against the
|
||||
* type. If "allowEagerInit" is not set, only raw FactoryBeans will be checked
|
||||
* (which doesn't require initialization of each FactoryBean).
|
||||
$ * <p>Does not consider any hierarchy this factory may participate in.
|
||||
* <p>Does not consider any hierarchy this factory may participate in.
|
||||
* Use BeanFactoryUtils' <code>beanNamesForTypeIncludingAncestors</code>
|
||||
* to include beans in ancestor factories too.
|
||||
* <p>Note: Does <i>not</i> ignore singleton beans that have been registered
|
||||
@@ -143,7 +143,7 @@ $ * <p>Does not consider any hierarchy this factory may participate in.
|
||||
* @see FactoryBean#getObjectType
|
||||
* @see BeanFactoryUtils#beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)
|
||||
*/
|
||||
String[] getBeanNamesForType(Class type, boolean includeNonSingletons, boolean allowEagerInit);
|
||||
String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit);
|
||||
|
||||
/**
|
||||
* Return the bean instances that match the given object type (including
|
||||
|
||||
@@ -432,6 +432,7 @@ public class SingletonBeanFactoryLocator implements BeanFactoryLocator {
|
||||
protected BeanFactory createDefinition(String resourceLocation, String factoryKey) {
|
||||
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
|
||||
// TODO SPR-7508: consider whether to allow for setEnvironment() here (where would it come from?)
|
||||
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
|
||||
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
* Copyright 2002-2010 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.beans.factory.config;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.util.PropertyPlaceholderHelper;
|
||||
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
|
||||
import org.springframework.util.StringValueResolver;
|
||||
|
||||
|
||||
/**
|
||||
* TODO SPR-7508: document.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.1
|
||||
*/
|
||||
public abstract class AbstractPropertyPlaceholderConfigurer extends PropertyResourceConfigurer
|
||||
implements BeanNameAware, BeanFactoryAware {
|
||||
|
||||
/** Default placeholder prefix: "${" */
|
||||
public static final String DEFAULT_PLACEHOLDER_PREFIX = "${";
|
||||
/** Default placeholder suffix: "}" */
|
||||
public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";
|
||||
/** Default value separator: ":" */
|
||||
public static final String DEFAULT_VALUE_SEPARATOR = ":";
|
||||
|
||||
protected String placeholderPrefix = DEFAULT_PLACEHOLDER_PREFIX;
|
||||
|
||||
protected String placeholderSuffix = DEFAULT_PLACEHOLDER_SUFFIX;
|
||||
|
||||
protected String valueSeparator = DEFAULT_VALUE_SEPARATOR;
|
||||
|
||||
protected boolean ignoreUnresolvablePlaceholders = false;
|
||||
|
||||
private String nullValue;
|
||||
|
||||
private String beanName;
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
|
||||
protected abstract PlaceholderResolver getPlaceholderResolver(Properties props);
|
||||
|
||||
/**
|
||||
* Set the prefix that a placeholder string starts with.
|
||||
* The default is "${".
|
||||
* @see #DEFAULT_PLACEHOLDER_PREFIX
|
||||
*/
|
||||
public void setPlaceholderPrefix(String placeholderPrefix) {
|
||||
this.placeholderPrefix = placeholderPrefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the suffix that a placeholder string ends with.
|
||||
* The default is "}".
|
||||
* @see #DEFAULT_PLACEHOLDER_SUFFIX
|
||||
*/
|
||||
public void setPlaceholderSuffix(String placeholderSuffix) {
|
||||
this.placeholderSuffix = placeholderSuffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the separating character between the placeholder variable
|
||||
* and the associated default value, or <code>null</code> if no such
|
||||
* special character should be processed as a value separator.
|
||||
* The default is ":".
|
||||
*/
|
||||
public void setValueSeparator(String valueSeparator) {
|
||||
this.valueSeparator = valueSeparator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a value that should be treated as <code>null</code> when
|
||||
* resolved as a placeholder value: e.g. "" (empty String) or "null".
|
||||
* <p>Note that this will only apply to full property values,
|
||||
* not to parts of concatenated values.
|
||||
* <p>By default, no such null value is defined. This means that
|
||||
* there is no way to express <code>null</code> as a property
|
||||
* value unless you explictly map a corresponding value here.
|
||||
*/
|
||||
public void setNullValue(String nullValue) {
|
||||
this.nullValue = nullValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to ignore unresolvable placeholders.
|
||||
* <p>Default is "false": An exception will be thrown if a placeholder fails
|
||||
* to resolve. Switch this flag to "true" in order to preserve the placeholder
|
||||
* String as-is in such a case, leaving it up to other placeholder configurers
|
||||
* to resolve it.
|
||||
*/
|
||||
public void setIgnoreUnresolvablePlaceholders(boolean ignoreUnresolvablePlaceholders) {
|
||||
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only necessary to check that we're not parsing our own bean definition,
|
||||
* to avoid failing on unresolvable placeholders in properties file locations.
|
||||
* The latter case can happen with placeholders for system properties in
|
||||
* resource locations.
|
||||
* @see #setLocations
|
||||
* @see org.springframework.core.io.ResourceEditor
|
||||
*/
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only necessary to check that we're not parsing our own bean definition,
|
||||
* to avoid failing on unresolvable placeholders in properties file locations.
|
||||
* The latter case can happen with placeholders for system properties in
|
||||
* resource locations.
|
||||
* @see #setLocations
|
||||
* @see org.springframework.core.io.ResourceEditor
|
||||
*/
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
|
||||
throws BeansException {
|
||||
|
||||
StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver(props);
|
||||
BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver);
|
||||
|
||||
String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
|
||||
for (String curName : beanNames) {
|
||||
// Check that we're not parsing our own bean definition,
|
||||
// to avoid failing on unresolvable placeholders in properties file locations.
|
||||
if (!(curName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
|
||||
BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(curName);
|
||||
try {
|
||||
visitor.visitBeanDefinition(bd);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
|
||||
beanFactoryToProcess.resolveAliases(valueResolver);
|
||||
|
||||
// New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes.
|
||||
beanFactoryToProcess.addEmbeddedValueResolver(valueResolver);
|
||||
}
|
||||
|
||||
|
||||
private class PlaceholderResolvingStringValueResolver implements StringValueResolver {
|
||||
|
||||
private final PropertyPlaceholderHelper helper;
|
||||
|
||||
private final PlaceholderResolver resolver;
|
||||
|
||||
public PlaceholderResolvingStringValueResolver(Properties props) {
|
||||
this.helper = new PropertyPlaceholderHelper(
|
||||
placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
|
||||
this.resolver = getPlaceholderResolver(props);
|
||||
}
|
||||
|
||||
public String resolveStringValue(String strVal) throws BeansException {
|
||||
String value = this.helper.replacePlaceholders(strVal, this.resolver);
|
||||
return (value.equals(nullValue) ? null : value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -173,7 +173,7 @@ public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, Single
|
||||
* @param requiredType type of the property
|
||||
* @param propertyEditorClass the {@link PropertyEditor} class to register
|
||||
*/
|
||||
void registerCustomEditor(Class requiredType, Class<? extends PropertyEditor> propertyEditorClass);
|
||||
void registerCustomEditor(Class<?> requiredType, Class<? extends PropertyEditor> propertyEditorClass);
|
||||
|
||||
/**
|
||||
* Initialize the given PropertyEditorRegistry with the custom editors
|
||||
|
||||
@@ -44,7 +44,7 @@ public interface ConfigurableListableBeanFactory
|
||||
* for example, String. Default is none.
|
||||
* @param type the dependency type to ignore
|
||||
*/
|
||||
void ignoreDependencyType(Class type);
|
||||
void ignoreDependencyType(Class<?> type);
|
||||
|
||||
/**
|
||||
* Ignore the given dependency interface for autowiring.
|
||||
@@ -57,7 +57,7 @@ public interface ConfigurableListableBeanFactory
|
||||
* @see org.springframework.beans.factory.BeanFactoryAware
|
||||
* @see org.springframework.context.ApplicationContextAware
|
||||
*/
|
||||
void ignoreDependencyInterface(Class ifc);
|
||||
void ignoreDependencyInterface(Class<?> ifc);
|
||||
|
||||
/**
|
||||
* Register a special dependency type with corresponding autowired value.
|
||||
@@ -75,7 +75,7 @@ public interface ConfigurableListableBeanFactory
|
||||
* implementation of the {@link org.springframework.beans.factory.ObjectFactory}
|
||||
* interface, which allows for lazy resolution of the actual target value.
|
||||
*/
|
||||
void registerResolvableDependency(Class dependencyType, Object autowiredValue);
|
||||
void registerResolvableDependency(Class<?> dependencyType, Object autowiredValue);
|
||||
|
||||
/**
|
||||
* Determine whether the specified bean qualifies as an autowire candidate,
|
||||
|
||||
@@ -19,15 +19,12 @@ package org.springframework.beans.factory.config;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
import org.springframework.core.Constants;
|
||||
import org.springframework.util.PropertyPlaceholderHelper;
|
||||
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
|
||||
import org.springframework.util.StringValueResolver;
|
||||
|
||||
|
||||
/**
|
||||
* A property resource configurer that resolves placeholders in bean property values of
|
||||
@@ -81,6 +78,7 @@ import org.springframework.util.StringValueResolver;
|
||||
* be detected and decrypted accordingly before processing them.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
* @since 02.10.2003
|
||||
* @see #setLocations
|
||||
* @see #setProperties
|
||||
@@ -91,19 +89,9 @@ import org.springframework.util.StringValueResolver;
|
||||
* @see #convertPropertyValue
|
||||
* @see PropertyOverrideConfigurer
|
||||
*/
|
||||
public class PropertyPlaceholderConfigurer extends PropertyResourceConfigurer
|
||||
public class PropertyPlaceholderConfigurer extends AbstractPropertyPlaceholderConfigurer
|
||||
implements BeanNameAware, BeanFactoryAware {
|
||||
|
||||
/** Default placeholder prefix: "${" */
|
||||
public static final String DEFAULT_PLACEHOLDER_PREFIX = "${";
|
||||
|
||||
/** Default placeholder suffix: "}" */
|
||||
public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";
|
||||
|
||||
/** Default value separator: ":" */
|
||||
public static final String DEFAULT_VALUE_SEPARATOR = ":";
|
||||
|
||||
|
||||
/** Never check system properties. */
|
||||
public static final int SYSTEM_PROPERTIES_MODE_NEVER = 0;
|
||||
|
||||
@@ -122,52 +110,10 @@ public class PropertyPlaceholderConfigurer extends PropertyResourceConfigurer
|
||||
|
||||
private static final Constants constants = new Constants(PropertyPlaceholderConfigurer.class);
|
||||
|
||||
private String placeholderPrefix = DEFAULT_PLACEHOLDER_PREFIX;
|
||||
|
||||
private String placeholderSuffix = DEFAULT_PLACEHOLDER_SUFFIX;
|
||||
|
||||
private String valueSeparator = DEFAULT_VALUE_SEPARATOR;
|
||||
|
||||
private int systemPropertiesMode = SYSTEM_PROPERTIES_MODE_FALLBACK;
|
||||
|
||||
private boolean searchSystemEnvironment = true;
|
||||
|
||||
private boolean ignoreUnresolvablePlaceholders = false;
|
||||
|
||||
private String nullValue;
|
||||
|
||||
private String beanName;
|
||||
|
||||
private BeanFactory beanFactory;
|
||||
|
||||
|
||||
/**
|
||||
* Set the prefix that a placeholder string starts with.
|
||||
* The default is "${".
|
||||
* @see #DEFAULT_PLACEHOLDER_PREFIX
|
||||
*/
|
||||
public void setPlaceholderPrefix(String placeholderPrefix) {
|
||||
this.placeholderPrefix = placeholderPrefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the suffix that a placeholder string ends with.
|
||||
* The default is "}".
|
||||
* @see #DEFAULT_PLACEHOLDER_SUFFIX
|
||||
*/
|
||||
public void setPlaceholderSuffix(String placeholderSuffix) {
|
||||
this.placeholderSuffix = placeholderSuffix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the separating character between the placeholder variable
|
||||
* and the associated default value, or <code>null</code> if no such
|
||||
* special character should be processed as a value separator.
|
||||
* The default is ":".
|
||||
*/
|
||||
public void setValueSeparator(String valueSeparator) {
|
||||
this.valueSeparator = valueSeparator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the system property mode by the name of the corresponding constant,
|
||||
@@ -218,84 +164,6 @@ public class PropertyPlaceholderConfigurer extends PropertyResourceConfigurer
|
||||
this.searchSystemEnvironment = searchSystemEnvironment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to ignore unresolvable placeholders.
|
||||
* <p>Default is "false": An exception will be thrown if a placeholder fails
|
||||
* to resolve. Switch this flag to "true" in order to preserve the placeholder
|
||||
* String as-is in such a case, leaving it up to other placeholder configurers
|
||||
* to resolve it.
|
||||
*/
|
||||
public void setIgnoreUnresolvablePlaceholders(boolean ignoreUnresolvablePlaceholders) {
|
||||
this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a value that should be treated as <code>null</code> when
|
||||
* resolved as a placeholder value: e.g. "" (empty String) or "null".
|
||||
* <p>Note that this will only apply to full property values,
|
||||
* not to parts of concatenated values.
|
||||
* <p>By default, no such null value is defined. This means that
|
||||
* there is no way to express <code>null</code> as a property
|
||||
* value unless you explictly map a corresponding value here.
|
||||
*/
|
||||
public void setNullValue(String nullValue) {
|
||||
this.nullValue = nullValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only necessary to check that we're not parsing our own bean definition,
|
||||
* to avoid failing on unresolvable placeholders in properties file locations.
|
||||
* The latter case can happen with placeholders for system properties in
|
||||
* resource locations.
|
||||
* @see #setLocations
|
||||
* @see org.springframework.core.io.ResourceEditor
|
||||
*/
|
||||
public void setBeanName(String beanName) {
|
||||
this.beanName = beanName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only necessary to check that we're not parsing our own bean definition,
|
||||
* to avoid failing on unresolvable placeholders in properties file locations.
|
||||
* The latter case can happen with placeholders for system properties in
|
||||
* resource locations.
|
||||
* @see #setLocations
|
||||
* @see org.springframework.core.io.ResourceEditor
|
||||
*/
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
|
||||
throws BeansException {
|
||||
|
||||
StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver(props);
|
||||
BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver);
|
||||
|
||||
String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames();
|
||||
for (String curName : beanNames) {
|
||||
// Check that we're not parsing our own bean definition,
|
||||
// to avoid failing on unresolvable placeholders in properties file locations.
|
||||
if (!(curName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) {
|
||||
BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(curName);
|
||||
try {
|
||||
visitor.visitBeanDefinition(bd);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// New in Spring 2.5: resolve placeholders in alias target names and aliases as well.
|
||||
beanFactoryToProcess.resolveAliases(valueResolver);
|
||||
|
||||
// New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes.
|
||||
beanFactoryToProcess.addEmbeddedValueResolver(valueResolver);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the given placeholder using the given properties, performing
|
||||
* a system properties check according to the given mode.
|
||||
@@ -379,30 +247,16 @@ public class PropertyPlaceholderConfigurer extends PropertyResourceConfigurer
|
||||
* Only retained for compatibility with Spring 2.5 extensions.
|
||||
*/
|
||||
@Deprecated
|
||||
protected String parseStringValue(String strVal, Properties props, Set visitedPlaceholders) {
|
||||
protected String parseStringValue(String strVal, Properties props, Set<?> visitedPlaceholders) {
|
||||
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(
|
||||
placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
|
||||
PlaceholderResolver resolver = new PropertyPlaceholderConfigurerResolver(props);
|
||||
return helper.replacePlaceholders(strVal, resolver);
|
||||
}
|
||||
|
||||
|
||||
private class PlaceholderResolvingStringValueResolver implements StringValueResolver {
|
||||
|
||||
private final PropertyPlaceholderHelper helper;
|
||||
|
||||
private final PlaceholderResolver resolver;
|
||||
|
||||
public PlaceholderResolvingStringValueResolver(Properties props) {
|
||||
this.helper = new PropertyPlaceholderHelper(
|
||||
placeholderPrefix, placeholderSuffix, valueSeparator, ignoreUnresolvablePlaceholders);
|
||||
this.resolver = new PropertyPlaceholderConfigurerResolver(props);
|
||||
}
|
||||
|
||||
public String resolveStringValue(String strVal) throws BeansException {
|
||||
String value = this.helper.replacePlaceholders(strVal, this.resolver);
|
||||
return (value.equals(nullValue) ? null : value);
|
||||
}
|
||||
@Override
|
||||
protected PlaceholderResolver getPlaceholderResolver(Properties props) {
|
||||
return new PropertyPlaceholderConfigurerResolver(props);
|
||||
}
|
||||
|
||||
|
||||
@@ -419,4 +273,5 @@ public class PropertyPlaceholderConfigurer extends PropertyResourceConfigurer
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -166,7 +166,6 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
|
||||
setParentBeanFactory(parentBeanFactory);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the instantiation strategy to use for creating bean instances.
|
||||
* Default is CglibSubclassingInstantiationStrategy.
|
||||
|
||||
@@ -21,8 +21,10 @@ import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.core.env.DefaultEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.EnvironmentCapable;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
@@ -40,7 +42,7 @@ import org.springframework.util.Assert;
|
||||
* @since 11.12.2003
|
||||
* @see BeanDefinitionReaderUtils
|
||||
*/
|
||||
public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader {
|
||||
public abstract class AbstractBeanDefinitionReader implements EnvironmentCapable, BeanDefinitionReader {
|
||||
|
||||
/** Logger available to subclasses */
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
@@ -51,6 +53,8 @@ public abstract class AbstractBeanDefinitionReader implements BeanDefinitionRead
|
||||
|
||||
private ClassLoader beanClassLoader;
|
||||
|
||||
private Environment environment = new DefaultEnvironment();
|
||||
|
||||
private BeanNameGenerator beanNameGenerator = new DefaultBeanNameGenerator();
|
||||
|
||||
|
||||
@@ -62,9 +66,14 @@ public abstract class AbstractBeanDefinitionReader implements BeanDefinitionRead
|
||||
* {@link org.springframework.context.ApplicationContext} implementations.
|
||||
* <p>If given a plain BeanDefinitionRegistry, the default ResourceLoader will be a
|
||||
* {@link org.springframework.core.io.support.PathMatchingResourcePatternResolver}.
|
||||
* <p>If the the passed-in bean factory also implements {@link EnvironmentCapable} its
|
||||
* environment will be used by this reader. Otherwise, the reader will initialize and
|
||||
* use a {@link DefaultEnvironment}. All ApplicationContext implementations are
|
||||
* EnvironmentCapable, while normal BeanFactory implementations are not.
|
||||
* @param registry the BeanFactory to load bean definitions into,
|
||||
* in the form of a BeanDefinitionRegistry
|
||||
* @see #setResourceLoader
|
||||
* @see #setEnvironment
|
||||
*/
|
||||
protected AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) {
|
||||
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
|
||||
@@ -77,6 +86,14 @@ public abstract class AbstractBeanDefinitionReader implements BeanDefinitionRead
|
||||
else {
|
||||
this.resourceLoader = new PathMatchingResourcePatternResolver();
|
||||
}
|
||||
|
||||
// Inherit Environment if possible
|
||||
if (this.registry instanceof EnvironmentCapable) {
|
||||
this.environment = ((EnvironmentCapable)this.registry).getEnvironment();
|
||||
}
|
||||
else {
|
||||
this.environment = new DefaultEnvironment();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -122,6 +139,21 @@ public abstract class AbstractBeanDefinitionReader implements BeanDefinitionRead
|
||||
return this.beanClassLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO SPR-7508: document
|
||||
* @param environment
|
||||
*/
|
||||
public void setEnvironment(Environment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO SPR-7508: document
|
||||
*/
|
||||
public Environment getEnvironment() {
|
||||
return this.environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the BeanNameGenerator to use for anonymous beans
|
||||
* (without explicit bean name specified).
|
||||
|
||||
@@ -68,6 +68,9 @@ import org.springframework.beans.factory.config.Scope;
|
||||
import org.springframework.core.DecoratingClassLoader;
|
||||
import org.springframework.core.NamedThreadLocal;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.DefaultEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
@@ -100,6 +103,7 @@ import org.springframework.util.StringValueResolver;
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Costin Leau
|
||||
* @author Chris Beams
|
||||
* @since 15 April 2001
|
||||
* @see #getBeanDefinition
|
||||
* @see #createBean
|
||||
@@ -166,6 +170,9 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
||||
private final ThreadLocal<Object> prototypesCurrentlyInCreation =
|
||||
new NamedThreadLocal<Object>("Prototype beans currently in creation");
|
||||
|
||||
private ConfigurableEnvironment environment = new DefaultEnvironment();
|
||||
|
||||
|
||||
/**
|
||||
* Create a new AbstractBeanFactory.
|
||||
*/
|
||||
@@ -189,7 +196,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
||||
public Object getBean(String name) throws BeansException {
|
||||
return doGetBean(name, null, null, false);
|
||||
}
|
||||
|
||||
|
||||
public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
|
||||
return doGetBean(name, requiredType, null, false);
|
||||
}
|
||||
@@ -682,7 +689,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
||||
return this.propertyEditorRegistrars;
|
||||
}
|
||||
|
||||
public void registerCustomEditor(Class requiredType, Class<? extends PropertyEditor> propertyEditorClass) {
|
||||
public void registerCustomEditor(Class<?> requiredType, Class<? extends PropertyEditor> propertyEditorClass) {
|
||||
Assert.notNull(requiredType, "Required type must not be null");
|
||||
Assert.isAssignable(PropertyEditor.class, propertyEditorClass);
|
||||
this.customEditors.put(requiredType, propertyEditorClass);
|
||||
@@ -1253,7 +1260,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
||||
throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Class doResolveBeanClass(RootBeanDefinition mbd, Class... typesToMatch) throws ClassNotFoundException {
|
||||
if (!ObjectUtils.isEmpty(typesToMatch)) {
|
||||
ClassLoader tempClassLoader = getTempClassLoader();
|
||||
|
||||
@@ -203,6 +203,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
*/
|
||||
public void setAutowireCandidateResolver(final AutowireCandidateResolver autowireCandidateResolver) {
|
||||
Assert.notNull(autowireCandidateResolver, "AutowireCandidateResolver must not be null");
|
||||
// TODO SPR-7515: should also do EnvironmentAware injection here?
|
||||
if (autowireCandidateResolver instanceof BeanFactoryAware) {
|
||||
if (System.getSecurityManager() != null) {
|
||||
final BeanFactory target = this;
|
||||
@@ -460,7 +461,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
// Implementation of ConfigurableListableBeanFactory interface
|
||||
//---------------------------------------------------------------------
|
||||
|
||||
public void registerResolvableDependency(Class dependencyType, Object autowiredValue) {
|
||||
public void registerResolvableDependency(Class<?> dependencyType, Object autowiredValue) {
|
||||
Assert.notNull(dependencyType, "Type must not be null");
|
||||
if (autowiredValue != null) {
|
||||
Assert.isTrue((autowiredValue instanceof ObjectFactory || dependencyType.isInstance(autowiredValue)),
|
||||
|
||||
@@ -43,6 +43,6 @@ public interface MergedBeanDefinitionPostProcessor extends BeanPostProcessor {
|
||||
* @param beanType the actual type of the managed bean instance
|
||||
* @param beanName the name of the bean
|
||||
*/
|
||||
void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType, String beanName);
|
||||
void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName);
|
||||
|
||||
}
|
||||
|
||||
@@ -33,6 +33,8 @@ import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.SmartFactoryBean;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.DefaultEnvironment;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -59,6 +61,9 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
|
||||
/** Map from bean name to bean instance */
|
||||
private final Map<String, Object> beans = new HashMap<String, Object>();
|
||||
|
||||
/** TODO SPR-7508: document */
|
||||
private ConfigurableEnvironment environment = new DefaultEnvironment();
|
||||
|
||||
|
||||
/**
|
||||
* Add a new singleton bean.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
* Copyright 2002-2010 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.
|
||||
@@ -63,7 +63,7 @@ public abstract class AbstractSingleBeanDefinitionParser extends AbstractBeanDef
|
||||
if (parentName != null) {
|
||||
builder.getRawBeanDefinition().setParentName(parentName);
|
||||
}
|
||||
Class beanClass = getBeanClass(element);
|
||||
Class<?> beanClass = getBeanClass(element);
|
||||
if (beanClass != null) {
|
||||
builder.getRawBeanDefinition().setBeanClass(beanClass);
|
||||
}
|
||||
@@ -111,7 +111,7 @@ public abstract class AbstractSingleBeanDefinitionParser extends AbstractBeanDef
|
||||
* the supplied <code>Element</code>, or <code>null</code> if none
|
||||
* @see #getBeanClassName
|
||||
*/
|
||||
protected Class getBeanClass(Element element) {
|
||||
protected Class<?> getBeanClass(Element element) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2006 the original author or authors.
|
||||
* Copyright 2002-2010 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.
|
||||
@@ -42,7 +42,7 @@ public interface BeanDefinitionParser {
|
||||
/**
|
||||
* Parse the specified {@link Element} and register the resulting
|
||||
* {@link BeanDefinition BeanDefinition(s)} with the
|
||||
* {@link org.springframework.beans.factory.xml.ParserContext#getRegistry()} BeanDefinitionRegistry}
|
||||
* {@link org.springframework.beans.factory.xml.ParserContext#getRegistry() BeanDefinitionRegistry}
|
||||
* embedded in the supplied {@link ParserContext}.
|
||||
* <p>Implementations must return the primary {@link BeanDefinition} that results
|
||||
* from the parse if they will ever be used in a nested fashion (for example as
|
||||
@@ -50,7 +50,8 @@ public interface BeanDefinitionParser {
|
||||
* <code>null</code> if they will <strong>not</strong> be used in a nested fashion.
|
||||
* @param element the element that is to be parsed into one or more {@link BeanDefinition BeanDefinitions}
|
||||
* @param parserContext the object encapsulating the current state of the parsing process;
|
||||
* provides access to a {@link org.springframework.beans.factory.support.BeanDefinitionRegistry}
|
||||
* provides access to a {@link org.springframework.beans.factory.support.BeanDefinitionRegistry
|
||||
* BeanDefinitionRegistry}.
|
||||
* @return the primary {@link BeanDefinition}
|
||||
*/
|
||||
BeanDefinition parse(Element element, ParserContext parserContext);
|
||||
|
||||
@@ -58,6 +58,7 @@ import org.springframework.beans.factory.support.ManagedProperties;
|
||||
import org.springframework.beans.factory.support.ManagedSet;
|
||||
import org.springframework.beans.factory.support.MethodOverrides;
|
||||
import org.springframework.beans.factory.support.ReplaceOverride;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
@@ -92,10 +93,14 @@ public class BeanDefinitionParserDelegate {
|
||||
*/
|
||||
public static final String TRUE_VALUE = "true";
|
||||
|
||||
public static final String FALSE_VALUE = "false";
|
||||
|
||||
public static final String DEFAULT_VALUE = "default";
|
||||
|
||||
public static final String DESCRIPTION_ELEMENT = "description";
|
||||
|
||||
public static final String AUTOWIRE_NO_VALUE = "no";
|
||||
|
||||
public static final String AUTOWIRE_BY_NAME_VALUE = "byName";
|
||||
|
||||
public static final String AUTOWIRE_BY_TYPE_VALUE = "byType";
|
||||
@@ -239,19 +244,25 @@ public class BeanDefinitionParserDelegate {
|
||||
|
||||
private final ParseState parseState = new ParseState();
|
||||
|
||||
private Environment environment;
|
||||
|
||||
/**
|
||||
* Stores all used bean names so we can enforce uniqueness on a per file basis.
|
||||
* Stores all used bean names so we can enforce uniqueness on a per
|
||||
* beans-element basis. Duplicate bean ids/names may not exist within the
|
||||
* same level of beans element nesting, but may be duplicated across levels.
|
||||
*/
|
||||
private final Set<String> usedNames = new HashSet<String>();
|
||||
|
||||
|
||||
/**
|
||||
* Create a new BeanDefinitionParserDelegate associated with the
|
||||
* supplied {@link XmlReaderContext}.
|
||||
* supplied {@link XmlReaderContext} and {@link Environment}.
|
||||
*/
|
||||
public BeanDefinitionParserDelegate(XmlReaderContext readerContext) {
|
||||
public BeanDefinitionParserDelegate(XmlReaderContext readerContext, Environment environment) {
|
||||
Assert.notNull(readerContext, "XmlReaderContext must not be null");
|
||||
Assert.notNull(readerContext, "Environment must not be null");
|
||||
this.readerContext = readerContext;
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -261,6 +272,12 @@ public class BeanDefinitionParserDelegate {
|
||||
return this.readerContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link Environment} associated with this helper instance.
|
||||
*/
|
||||
public final Environment getEnvironment() {
|
||||
return this.environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke the {@link org.springframework.beans.factory.parsing.SourceExtractor} to pull the
|
||||
@@ -294,33 +311,69 @@ public class BeanDefinitionParserDelegate {
|
||||
|
||||
/**
|
||||
* Initialize the default lazy-init, autowire, dependency check settings,
|
||||
* init-method, destroy-method and merge settings.
|
||||
* @see #populateDefaults(DocumentDefaultsDefinition, org.w3c.dom.Element)
|
||||
* init-method, destroy-method and merge settings. Support nested 'beans'
|
||||
* element use cases by falling back to <literal>parent</literal> in case the
|
||||
* defaults are not explicitly set locally.
|
||||
* @see #populateDefaults(DocumentDefaultsDefinition, DocumentDefaultsDefinition, org.w3c.dom.Element)
|
||||
* @see #getDefaults()
|
||||
*/
|
||||
public void initDefaults(Element root) {
|
||||
populateDefaults(this.defaults, root);
|
||||
public void initDefaults(Element root, BeanDefinitionParserDelegate parent) {
|
||||
populateDefaults(this.defaults, (parent != null ? parent.defaults : null), root);
|
||||
this.readerContext.fireDefaultsRegistered(this.defaults);
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate the given DocumentDefaultsDefinition instance with the default lazy-init,
|
||||
* autowire, dependency check settings, init-method, destroy-method and merge settings.
|
||||
* Support nested 'beans' element use cases by falling back to
|
||||
* <literal>parentDefaults</literal> in case the defaults are not explicitly set
|
||||
* locally.
|
||||
* @param defaults the defaults to populate
|
||||
* @param defaults the parent BeanDefinitionParserDelegate (if any) defaults to fall back to
|
||||
* @param root the root element of the current bean definition document (or nested beans element)
|
||||
*/
|
||||
protected void populateDefaults(DocumentDefaultsDefinition defaults, Element root) {
|
||||
defaults.setLazyInit(root.getAttribute(DEFAULT_LAZY_INIT_ATTRIBUTE));
|
||||
defaults.setMerge(root.getAttribute(DEFAULT_MERGE_ATTRIBUTE));
|
||||
defaults.setAutowire(root.getAttribute(DEFAULT_AUTOWIRE_ATTRIBUTE));
|
||||
protected void populateDefaults(DocumentDefaultsDefinition defaults, DocumentDefaultsDefinition parentDefaults, Element root) {
|
||||
String lazyInit = root.getAttribute(DEFAULT_LAZY_INIT_ATTRIBUTE);
|
||||
if (DEFAULT_VALUE.equals(lazyInit)) {
|
||||
lazyInit = parentDefaults != null ? parentDefaults.getLazyInit() : FALSE_VALUE;
|
||||
}
|
||||
defaults.setLazyInit(lazyInit);
|
||||
|
||||
String merge = root.getAttribute(DEFAULT_MERGE_ATTRIBUTE);
|
||||
if (DEFAULT_VALUE.equals(merge)) {
|
||||
merge = parentDefaults != null ? parentDefaults.getMerge() : FALSE_VALUE;
|
||||
}
|
||||
defaults.setMerge(merge);
|
||||
|
||||
String autowire = root.getAttribute(DEFAULT_AUTOWIRE_ATTRIBUTE);
|
||||
if (DEFAULT_VALUE.equals(autowire)) {
|
||||
autowire = parentDefaults != null ? parentDefaults.getAutowire() : AUTOWIRE_NO_VALUE;
|
||||
}
|
||||
defaults.setAutowire(autowire);
|
||||
|
||||
// don't fall back to parentDefaults for dependency-check as it's no
|
||||
// longer supported in <beans> as of 3.0. Therefore, no nested <beans>
|
||||
// would ever need to fall back to it.
|
||||
defaults.setDependencyCheck(root.getAttribute(DEFAULT_DEPENDENCY_CHECK_ATTRIBUTE));
|
||||
|
||||
if (root.hasAttribute(DEFAULT_AUTOWIRE_CANDIDATES_ATTRIBUTE)) {
|
||||
defaults.setAutowireCandidates(root.getAttribute(DEFAULT_AUTOWIRE_CANDIDATES_ATTRIBUTE));
|
||||
} else if (parentDefaults != null) {
|
||||
defaults.setAutowireCandidates(parentDefaults.getAutowireCandidates());
|
||||
}
|
||||
|
||||
if (root.hasAttribute(DEFAULT_INIT_METHOD_ATTRIBUTE)) {
|
||||
defaults.setInitMethod(root.getAttribute(DEFAULT_INIT_METHOD_ATTRIBUTE));
|
||||
} else if (parentDefaults != null) {
|
||||
defaults.setInitMethod(parentDefaults.getInitMethod());
|
||||
}
|
||||
|
||||
if (root.hasAttribute(DEFAULT_DESTROY_METHOD_ATTRIBUTE)) {
|
||||
defaults.setDestroyMethod(root.getAttribute(DEFAULT_DESTROY_METHOD_ATTRIBUTE));
|
||||
} else if (parentDefaults != null) {
|
||||
defaults.setDestroyMethod(parentDefaults.getDestroyMethod());
|
||||
}
|
||||
|
||||
defaults.setSource(this.readerContext.extractSource(root));
|
||||
}
|
||||
|
||||
@@ -431,7 +484,8 @@ public class BeanDefinitionParserDelegate {
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the specified bean name and aliases have not been used already.
|
||||
* Validate that the specified bean name and aliases have not been used already
|
||||
* within the current level of beans element nesting.
|
||||
*/
|
||||
protected void checkNameUniqueness(String beanName, List<String> aliases, Element beanElement) {
|
||||
String foundName = null;
|
||||
@@ -443,7 +497,7 @@ public class BeanDefinitionParserDelegate {
|
||||
foundName = (String) CollectionUtils.findFirstMatch(this.usedNames, aliases);
|
||||
}
|
||||
if (foundName != null) {
|
||||
error("Bean name '" + foundName + "' is already used in this file", beanElement);
|
||||
error("Bean name '" + foundName + "' is already used in this <beans> element", beanElement);
|
||||
}
|
||||
|
||||
this.usedNames.add(beanName);
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.beans.factory.xml;
|
||||
|
||||
import static org.springframework.util.StringUtils.commaDelimitedListToStringArray;
|
||||
import static org.springframework.util.StringUtils.trimAllWhitespace;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.LinkedHashSet;
|
||||
@@ -32,11 +35,11 @@ import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
||||
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.ResourcePatternUtils;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.util.SystemPropertyUtils;
|
||||
|
||||
/**
|
||||
* Default implementation of the {@link BeanDefinitionDocumentReader} interface.
|
||||
@@ -59,6 +62,8 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
|
||||
|
||||
public static final String BEAN_ELEMENT = BeanDefinitionParserDelegate.BEAN_ELEMENT;
|
||||
|
||||
public static final String NESTED_BEANS_ELEMENT = "beans";
|
||||
|
||||
public static final String ALIAS_ELEMENT = "alias";
|
||||
|
||||
public static final String NAME_ATTRIBUTE = "name";
|
||||
@@ -69,14 +74,29 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
|
||||
|
||||
public static final String RESOURCE_ATTRIBUTE = "resource";
|
||||
|
||||
/** @see org.springframework.context.annotation.Profile */
|
||||
public static final String PROFILE_ATTRIBUTE = "profile";
|
||||
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private XmlReaderContext readerContext;
|
||||
|
||||
private Environment environment;
|
||||
|
||||
private BeanDefinitionParserDelegate delegate;
|
||||
|
||||
|
||||
/**
|
||||
* Parses bean definitions according to the "spring-beans" DTD.
|
||||
* TODO SPR-7508: document
|
||||
* @param environment
|
||||
*/
|
||||
public void setEnvironment(Environment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses bean definitions according to the "spring-beans" DTD. TODO SPR-7508 XSD
|
||||
* <p>Opens a DOM Document; then initializes the default settings
|
||||
* specified at <code><beans></code> level; then parses
|
||||
* the contained bean definitions.
|
||||
@@ -87,16 +107,49 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
|
||||
logger.debug("Loading bean definitions");
|
||||
Element root = doc.getDocumentElement();
|
||||
|
||||
BeanDefinitionParserDelegate delegate = createHelper(readerContext, root);
|
||||
|
||||
preProcessXml(root);
|
||||
parseBeanDefinitions(root, delegate);
|
||||
postProcessXml(root);
|
||||
doRegisterBeanDefinitions(root);
|
||||
}
|
||||
|
||||
protected BeanDefinitionParserDelegate createHelper(XmlReaderContext readerContext, Element root) {
|
||||
BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext);
|
||||
delegate.initDefaults(root);
|
||||
protected void doRegisterBeanDefinitions(Element root) {
|
||||
String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
|
||||
boolean isCandidate = false;
|
||||
if (profileSpec == null || profileSpec.equals("")) {
|
||||
isCandidate = true;
|
||||
} else {
|
||||
String[] profiles = commaDelimitedListToStringArray(trimAllWhitespace(profileSpec));
|
||||
for (String profile : profiles) {
|
||||
if (this.environment.getActiveProfiles().contains(profile)) {
|
||||
isCandidate = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!isCandidate) {
|
||||
// TODO SPR-7508 logging
|
||||
// logger.debug(format("XML is targeted for environment [%s], but current environment is [%s]. Skipping", targetEnvironment, environment == null ? null : environment.getName()));
|
||||
return;
|
||||
}
|
||||
|
||||
// any nested <beans> elements will cause recursion in this method. in
|
||||
// order to propagate and preserve <beans> default-* attributes correctly,
|
||||
// keep track of the current (parent) delegate, which may be null. Create
|
||||
// the new (child) delegate with a reference to the parent for fallback purposes,
|
||||
// then ultimately reset this.delegate back to its original (parent) reference.
|
||||
// this behavior emulates a stack of delegates without actually necessitating one.
|
||||
BeanDefinitionParserDelegate parent = this.delegate;
|
||||
this.delegate = createHelper(readerContext, root, parent);
|
||||
|
||||
preProcessXml(root);
|
||||
parseBeanDefinitions(root, this.delegate);
|
||||
postProcessXml(root);
|
||||
|
||||
this.delegate = parent;
|
||||
}
|
||||
|
||||
protected BeanDefinitionParserDelegate createHelper(XmlReaderContext readerContext, Element root, BeanDefinitionParserDelegate parentDelegate) {
|
||||
BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext, environment);
|
||||
delegate.initDefaults(root, parentDelegate);
|
||||
return delegate;
|
||||
}
|
||||
|
||||
@@ -152,6 +205,10 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
|
||||
else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
|
||||
processBeanDefinition(ele, delegate);
|
||||
}
|
||||
else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {
|
||||
// recurse
|
||||
doRegisterBeanDefinitions(ele);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -166,7 +223,7 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
|
||||
}
|
||||
|
||||
// Resolve system properties: e.g. "${user.dir}"
|
||||
location = SystemPropertyUtils.resolvePlaceholders(location);
|
||||
location = environment.resolveRequiredPlaceholders(location);
|
||||
|
||||
Set<Resource> actualResources = new LinkedHashSet<Resource>(4);
|
||||
|
||||
|
||||
@@ -65,6 +65,7 @@ import org.springframework.util.xml.XmlValidationModeDetector;
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Rob Harrop
|
||||
* @author Chris Beams
|
||||
* @since 26.11.2003
|
||||
* @see #setDocumentReaderClass
|
||||
* @see BeanDefinitionDocumentReader
|
||||
@@ -103,7 +104,7 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
|
||||
|
||||
private boolean namespaceAware = false;
|
||||
|
||||
private Class documentReaderClass = DefaultBeanDefinitionDocumentReader.class;
|
||||
private Class<?> documentReaderClass = DefaultBeanDefinitionDocumentReader.class;
|
||||
|
||||
private ProblemReporter problemReporter = new FailFastProblemReporter();
|
||||
|
||||
@@ -134,7 +135,6 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
|
||||
super(registry);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set whether to use XML validation. Default is <code>true</code>.
|
||||
* <p>This method switches namespace awareness on if validation is turned off,
|
||||
@@ -283,7 +283,7 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
|
||||
* <p>The default is {@link DefaultBeanDefinitionDocumentReader}.
|
||||
* @param documentReaderClass the desired BeanDefinitionDocumentReader implementation class
|
||||
*/
|
||||
public void setDocumentReaderClass(Class documentReaderClass) {
|
||||
public void setDocumentReaderClass(Class<?> documentReaderClass) {
|
||||
if (documentReaderClass == null || !BeanDefinitionDocumentReader.class.isAssignableFrom(documentReaderClass)) {
|
||||
throw new IllegalArgumentException(
|
||||
"documentReaderClass must be an implementation of the BeanDefinitionDocumentReader interface");
|
||||
@@ -487,8 +487,12 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
|
||||
* @see BeanDefinitionDocumentReader#registerBeanDefinitions
|
||||
*/
|
||||
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
|
||||
// Read document based on new BeanDefinitionDocumentReader SPI.
|
||||
// Read document based on new BeanDefinitionDocumentReader SPI. // TODO SPR-7508: polish - remove comment
|
||||
BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
|
||||
// TODO SPR-7508: remove ugly cast
|
||||
if (documentReader instanceof DefaultBeanDefinitionDocumentReader) {
|
||||
((DefaultBeanDefinitionDocumentReader)documentReader).setEnvironment(this.getEnvironment());
|
||||
}
|
||||
int countBefore = getRegistry().getBeanDefinitionCount();
|
||||
documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
|
||||
return getRegistry().getBeanDefinitionCount() - countBefore;
|
||||
@@ -500,7 +504,6 @@ public class XmlBeanDefinitionReader extends AbstractBeanDefinitionReader {
|
||||
* <p>The default implementation instantiates the specified "documentReaderClass".
|
||||
* @see #setDocumentReaderClass
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected BeanDefinitionDocumentReader createBeanDefinitionDocumentReader() {
|
||||
return BeanDefinitionDocumentReader.class.cast(BeanUtils.instantiateClass(this.documentReaderClass));
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.beans.factory.xml;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
@@ -34,7 +35,7 @@ import org.springframework.core.io.Resource;
|
||||
* <p>This class registers each bean definition with the {@link DefaultListableBeanFactory}
|
||||
* superclass, and relies on the latter's implementation of the {@link BeanFactory} interface.
|
||||
* It supports singletons, prototypes, and references to either of these kinds of bean.
|
||||
* See "spring-beans-2.0.dtd" for details on options and configuration style.
|
||||
* See "spring-beans-2.0.dtd" for details on options and configuration style. // TODO SPR-7508 polish - s/dtd/xsd/
|
||||
*
|
||||
* <p><b>For advanced needs, consider using a {@link DefaultListableBeanFactory} with
|
||||
* an {@link XmlBeanDefinitionReader}.</b> The latter allows for reading from multiple XML
|
||||
@@ -42,10 +43,14 @@ import org.springframework.core.io.Resource;
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
* @since 15 April 2001
|
||||
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory
|
||||
* @see XmlBeanDefinitionReader
|
||||
* @deprecated as of Spring 3.1 in favor of {@link DefaultListableBeanFactory} and
|
||||
* {@link XmlBeanDefinitionReader}
|
||||
*/
|
||||
@Deprecated
|
||||
public class XmlBeanFactory extends DefaultListableBeanFactory {
|
||||
|
||||
private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);
|
||||
@@ -58,7 +63,7 @@ public class XmlBeanFactory extends DefaultListableBeanFactory {
|
||||
* @throws BeansException in case of loading or parsing errors
|
||||
*/
|
||||
public XmlBeanFactory(Resource resource) throws BeansException {
|
||||
this(resource, null);
|
||||
this(resource, (BeanFactory)null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -34,6 +34,8 @@ import org.springframework.beans.propertyeditors.InputSourceEditor;
|
||||
import org.springframework.beans.propertyeditors.InputStreamEditor;
|
||||
import org.springframework.beans.propertyeditors.URIEditor;
|
||||
import org.springframework.beans.propertyeditors.URLEditor;
|
||||
import org.springframework.core.env.DefaultEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceEditor;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
@@ -53,9 +55,26 @@ import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
*/
|
||||
public class ResourceEditorRegistrar implements PropertyEditorRegistrar {
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
private final ResourceLoader resourceLoader;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new ResourceEditorRegistrar for the given {@link ResourceLoader}
|
||||
* using a {@link DefaultEnvironment}.
|
||||
* @param resourceLoader the ResourceLoader (or ResourcePatternResolver)
|
||||
* to create editors for (usually an ApplicationContext)
|
||||
* @see org.springframework.core.io.support.ResourcePatternResolver
|
||||
* @see org.springframework.context.ApplicationContext
|
||||
* @deprecated as of Spring 3.1 in favor of
|
||||
* {@link ResourceEditorRegistrar#ResourceEditorRegistrar(ResourceLoader, Environment)}
|
||||
*/
|
||||
@Deprecated
|
||||
public ResourceEditorRegistrar(ResourceLoader resourceLoader) {
|
||||
this(resourceLoader, new DefaultEnvironment());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new ResourceEditorRegistrar for the given ResourceLoader
|
||||
* @param resourceLoader the ResourceLoader (or ResourcePatternResolver)
|
||||
@@ -63,8 +82,9 @@ public class ResourceEditorRegistrar implements PropertyEditorRegistrar {
|
||||
* @see org.springframework.core.io.support.ResourcePatternResolver
|
||||
* @see org.springframework.context.ApplicationContext
|
||||
*/
|
||||
public ResourceEditorRegistrar(ResourceLoader resourceLoader) {
|
||||
public ResourceEditorRegistrar(ResourceLoader resourceLoader, Environment environment) {
|
||||
this.resourceLoader = resourceLoader;
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
|
||||
@@ -82,7 +102,7 @@ public class ResourceEditorRegistrar implements PropertyEditorRegistrar {
|
||||
* @see org.springframework.core.io.support.ResourceArrayPropertyEditor
|
||||
*/
|
||||
public void registerCustomEditors(PropertyEditorRegistry registry) {
|
||||
ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader);
|
||||
ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader, this.environment);
|
||||
doRegisterEditor(registry, Resource.class, baseEditor);
|
||||
doRegisterEditor(registry, InputStream.class, new InputStreamEditor(baseEditor));
|
||||
doRegisterEditor(registry, InputSource.class, new InputSourceEditor(baseEditor));
|
||||
@@ -96,7 +116,7 @@ public class ResourceEditorRegistrar implements PropertyEditorRegistrar {
|
||||
|
||||
if (this.resourceLoader instanceof ResourcePatternResolver) {
|
||||
doRegisterEditor(registry, Resource[].class,
|
||||
new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader));
|
||||
new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader, this.environment));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user