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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
http\://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd
|
||||
http\://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd
|
||||
http\://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd
|
||||
http\://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd
|
||||
http\://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd
|
||||
http\://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd
|
||||
http\://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd
|
||||
http\://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd
|
||||
http\://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd
|
||||
http\://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd
|
||||
http\://www.springframework.org/schema/tool/spring-tool-3.1.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.xsd
|
||||
http\://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.xsd
|
||||
http\://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd
|
||||
http\://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd
|
||||
http\://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd
|
||||
http\://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd
|
||||
http\://www.springframework.org/schema/util/spring-util-3.1.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd
|
||||
http\://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,115 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
|
||||
<xsd:schema xmlns="http://www.springframework.org/schema/tool"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="http://www.springframework.org/schema/tool"
|
||||
elementFormDefault="qualified">
|
||||
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>
|
||||
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Defines the tool support annotations for Spring's configuration namespaces.
|
||||
Used in other namespace XSD files; not intended for direct use in config files.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
|
||||
<xsd:element name="annotation">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence minOccurs="0">
|
||||
<xsd:element name="expected-type" type="typedParameterType" minOccurs="0" maxOccurs="1"/>
|
||||
<xsd:element name="assignable-to" type="assignableToType" minOccurs="0" maxOccurs="1"/>
|
||||
<xsd:element name="exports" type="exportsType" minOccurs="0" maxOccurs="unbounded"/>
|
||||
<xsd:element name="registers-scope" type="registersScopeType" minOccurs="0" maxOccurs="unbounded"/>
|
||||
<xsd:element name="expected-method" type="expectedMethodType" minOccurs="0" maxOccurs="unbounded"/>
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="kind" default="direct">
|
||||
<xsd:simpleType>
|
||||
<xsd:restriction base="xsd:string">
|
||||
<xsd:enumeration value="ref"/>
|
||||
<xsd:enumeration value="direct"/>
|
||||
</xsd:restriction>
|
||||
</xsd:simpleType>
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:complexType name="typedParameterType">
|
||||
<xsd:attribute name="type" type="xsd:string" use="required"/>
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="assignableToType">
|
||||
<xsd:attribute name="type" type="xsd:string"/>
|
||||
<xsd:attribute name="restriction" default="both">
|
||||
<xsd:simpleType>
|
||||
<xsd:restriction base="xsd:NMTOKEN">
|
||||
<xsd:enumeration value="both"/>
|
||||
<xsd:enumeration value="interface-only"/>
|
||||
<xsd:enumeration value="class-only"/>
|
||||
</xsd:restriction>
|
||||
</xsd:simpleType>
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="expectedMethodType">
|
||||
<xsd:attribute name="type" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Defines an XPath query that can be executed against the node annotated with this
|
||||
type to determine the class for which the this method is valid
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="type-ref" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Defines an XPath query that can be executed against the node annotated with this
|
||||
type to determine a referenced bean (by id or alias) for which the given method is valid
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="expression" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Defines an AspectJ method execution pointcut expressions that matches valid methods
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="exportsType">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Indicates that an annotated type exports an application visible component.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:attribute name="type" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
The type of the exported component. May be null if the type is not known until runtime.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="identifier" type="xsd:string" default="@id">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Defines an XPath query that can be executed against the node annotated with this
|
||||
type to determine the identifier of any exported component.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="registersScopeType">
|
||||
<xsd:attribute name="name" type="xsd:string" use="required">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Defines the name of a custom bean scope that the annotated type registers, e.g. "conversation".
|
||||
Such a scope will be available in addition to the standard "singleton" and "prototype" scopes
|
||||
(plus "request", "session" and "globalSession" in a web application environment).
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
|
||||
</xsd:schema>
|
||||
@@ -0,0 +1,212 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
|
||||
<xsd:schema xmlns="http://www.springframework.org/schema/util"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:tool="http://www.springframework.org/schema/tool"
|
||||
targetNamespace="http://www.springframework.org/schema/util"
|
||||
elementFormDefault="qualified"
|
||||
attributeFormDefault="unqualified">
|
||||
|
||||
<xsd:import namespace="http://www.springframework.org/schema/beans" schemaLocation="http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"/>
|
||||
<xsd:import namespace="http://www.springframework.org/schema/tool" schemaLocation="http://www.springframework.org/schema/tool/spring-tool-3.1.xsd"/>
|
||||
|
||||
<xsd:element name="constant">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Reference a public, static field on a type and expose its value as
|
||||
a bean. For example <code><util:constant static-field="java.lang.Integer.MAX_VALUE"/></code>.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="id" type="xsd:string"/>
|
||||
<xsd:attribute name="static-field" type="xsd:string" use="required"/>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="property-path">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Reference a property on a bean (or as a nested value) and expose its values as
|
||||
a bean. For example <util:property-path path="order.customer.name"/>.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="id" type="xsd:string"/>
|
||||
<xsd:attribute name="path" type="xsd:string" use="required"/>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="list">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation source="java:org.springframework.beans.factory.config.ListFactoryBean">
|
||||
Builds a List instance of the specified type, populated with the specified content.
|
||||
</xsd:documentation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation>
|
||||
<tool:exports type="java.util.List"/>
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="beans:listOrSetType">
|
||||
<xsd:attribute name="id" type="xsd:string"/>
|
||||
<xsd:attribute name="list-class" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation>
|
||||
<tool:expected-type type="java.lang.Class"/>
|
||||
<tool:assignable-to type="java.util.List"/>
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="scope" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
The scope of this collection bean: typically "singleton" (one shared instance,
|
||||
which will be returned by all calls to getBean with the given id), or
|
||||
"prototype" (independent instance resulting from each call to getBean).
|
||||
Default is "singleton". Further scopes, such as "request" or "session",
|
||||
might be supported by extended bean factories (e.g. in a web environment).
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:extension>
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="set">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation source="java:org.springframework.beans.factory.config.SetFactoryBean">
|
||||
Builds a Set instance of the specified type, populated with the specified content.
|
||||
</xsd:documentation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation>
|
||||
<tool:exports type="java.util.Set"/>
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="beans:listOrSetType">
|
||||
<xsd:attribute name="id" type="xsd:string"/>
|
||||
<xsd:attribute name="set-class" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation>
|
||||
<tool:expected-type type="java.lang.Class"/>
|
||||
<tool:assignable-to type="java.util.Set"/>
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="scope" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
The scope of this collection bean: typically "singleton" (one shared instance,
|
||||
which will be returned by all calls to getBean with the given id), or
|
||||
"prototype" (independent instance resulting from each call to getBean).
|
||||
Default is "singleton". Further scopes, such as "request" or "session",
|
||||
might be supported by extended bean factories (e.g. in a web environment).
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:extension>
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="map">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation source="java:org.springframework.beans.factory.config.MapFactoryBean">
|
||||
Builds a Map instance of the specified type, populated with the specified content.
|
||||
</xsd:documentation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation>
|
||||
<tool:exports type="java.util.Map"/>
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="beans:mapType">
|
||||
<xsd:attribute name="id" type="xsd:string"/>
|
||||
<xsd:attribute name="map-class" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation>
|
||||
<tool:expected-type type="java.lang.Class"/>
|
||||
<tool:assignable-to type="java.util.Map"/>
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="scope" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
The scope of this collection bean: typically "singleton" (one shared instance,
|
||||
which will be returned by all calls to getBean with the given id), or
|
||||
"prototype" (independent instance resulting from each call to getBean).
|
||||
Default is "singleton". Further scopes, such as "request" or "session",
|
||||
might be supported by extended bean factories (e.g. in a web environment).
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:extension>
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="properties">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation source="java:org.springframework.beans.factory.config.PropertiesFactoryBean">
|
||||
Loads a Properties instance from the resource location specified by the '<code>location</code>' attribute.
|
||||
</xsd:documentation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation>
|
||||
<tool:exports type="java.util.Properties"/>
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:complexContent>
|
||||
<xsd:extension base="beans:propsType">
|
||||
<xsd:attribute name="id" type="xsd:string"/>
|
||||
<xsd:attribute name="location" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
<tool:annotation>
|
||||
<tool:expected-type type="org.springframework.core.io.Resource"/>
|
||||
</tool:annotation>
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="local-override" type="xsd:boolean">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Specifies whether local properties override properties from files.
|
||||
Default is "false": properties from files override local defaults.
|
||||
If set to "true", local properties will override defaults from files.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="scope" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
The scope of this collection bean: typically "singleton" (one shared instance,
|
||||
which will be returned by all calls to getBean with the given id), or
|
||||
"prototype" (independent instance resulting from each call to getBean).
|
||||
Default is "singleton". Further scopes, such as "request" or "session",
|
||||
might be supported by extended bean factories (e.g. in a web environment).
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:extension>
|
||||
</xsd:complexContent>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
</xsd:schema>
|
||||
@@ -1,44 +1,281 @@
|
||||
/*
|
||||
* 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 static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition;
|
||||
import static org.springframework.beans.factory.support.BeanDefinitionReaderUtils.registerWithGeneratedName;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
|
||||
import test.beans.TestBean;
|
||||
|
||||
|
||||
/**
|
||||
* Tests cornering SPR-7547.
|
||||
* Unit tests for {@link EnvironmentAwarePropertyPlaceholderConfigurer}.
|
||||
*
|
||||
* @see PropertyResourceConfigurerTests
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class PropertyPlaceholderConfigurerTests {
|
||||
private static final String P1 = "p1";
|
||||
private static final String P1_LOCAL_PROPS_VAL = "p1LocalPropsVal";
|
||||
private static final String P1_SYSTEM_PROPS_VAL = "p1SystemPropsVal";
|
||||
private static final String P1_SYSTEM_ENV_VAL = "p1SystemEnvVal";
|
||||
|
||||
private DefaultListableBeanFactory bf;
|
||||
private AbstractPropertyPlaceholderConfigurer ppc;
|
||||
private Properties ppcProperties;
|
||||
|
||||
private AbstractBeanDefinition p1BeanDef;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
p1BeanDef = rootBeanDefinition(TestBean.class)
|
||||
.addPropertyValue("name", "${"+P1+"}")
|
||||
.getBeanDefinition();
|
||||
|
||||
bf = new DefaultListableBeanFactory();
|
||||
|
||||
ppcProperties = new Properties();
|
||||
ppcProperties.setProperty(P1, P1_LOCAL_PROPS_VAL);
|
||||
System.setProperty(P1, P1_SYSTEM_PROPS_VAL);
|
||||
getModifiableSystemEnvironment().put(P1, P1_SYSTEM_ENV_VAL);
|
||||
ppc = new PropertyPlaceholderConfigurer();
|
||||
ppc.setProperties(ppcProperties);
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
System.clearProperty(P1);
|
||||
getModifiableSystemEnvironment().remove(P1);
|
||||
}
|
||||
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Tests to ensure backward-compatibility for Environment refactoring
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
public void resolveFromSystemProperties() {
|
||||
getModifiableSystemEnvironment().put("otherKey", "systemValue");
|
||||
p1BeanDef = rootBeanDefinition(TestBean.class)
|
||||
.addPropertyValue("name", "${"+P1+"}")
|
||||
.addPropertyValue("sex", "${otherKey}")
|
||||
.getBeanDefinition();
|
||||
registerWithGeneratedName(p1BeanDef, bf);
|
||||
ppc.postProcessBeanFactory(bf);
|
||||
TestBean bean = bf.getBean(TestBean.class);
|
||||
assertThat(bean.getName(), equalTo(P1_LOCAL_PROPS_VAL));
|
||||
assertThat(bean.getSex(), equalTo("systemValue"));
|
||||
getModifiableSystemEnvironment().remove("otherKey");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveFromLocalProperties() {
|
||||
tearDown(); // eliminate entries from system props/environment
|
||||
registerWithGeneratedName(p1BeanDef, bf);
|
||||
ppc.postProcessBeanFactory(bf);
|
||||
TestBean bean = bf.getBean(TestBean.class);
|
||||
assertThat(bean.getName(), equalTo(P1_LOCAL_PROPS_VAL));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setSystemPropertiesMode_defaultIsFallback() {
|
||||
registerWithGeneratedName(p1BeanDef, bf);
|
||||
ppc.postProcessBeanFactory(bf);
|
||||
TestBean bean = bf.getBean(TestBean.class);
|
||||
assertThat(bean.getName(), equalTo(P1_LOCAL_PROPS_VAL));
|
||||
}
|
||||
|
||||
/*
|
||||
@Test
|
||||
public void setSystemSystemPropertiesMode_toOverride_andResolveFromSystemProperties() {
|
||||
registerWithGeneratedName(p1BeanDef, bf);
|
||||
ppc.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
|
||||
ppc.postProcessBeanFactory(bf);
|
||||
TestBean bean = bf.getBean(TestBean.class);
|
||||
assertThat(bean.getName(), equalTo(P1_SYSTEM_PROPS_VAL));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setSystemSystemPropertiesMode_toOverride_andResolveFromSystemEnvironment() {
|
||||
registerWithGeneratedName(p1BeanDef, bf);
|
||||
System.clearProperty(P1); // will now fall all the way back to system environment
|
||||
ppc.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
|
||||
ppc.postProcessBeanFactory(bf);
|
||||
TestBean bean = bf.getBean(TestBean.class);
|
||||
assertThat(bean.getName(), equalTo(P1_SYSTEM_ENV_VAL));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setSystemSystemPropertiesMode_toOverride_andSetSearchSystemEnvironment_toFalse() {
|
||||
registerWithGeneratedName(p1BeanDef, bf);
|
||||
System.clearProperty(P1); // will now fall all the way back to system environment
|
||||
ppc.setSearchSystemEnvironment(false);
|
||||
ppc.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
|
||||
ppc.postProcessBeanFactory(bf);
|
||||
TestBean bean = bf.getBean(TestBean.class);
|
||||
assertThat(bean.getName(), equalTo(P1_LOCAL_PROPS_VAL)); // has to resort to local props
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Prior to the fix for SPR-7547, the following would throw
|
||||
* IllegalStateException because the PropertiesLoaderSupport base class
|
||||
* assumed ByteArrayResource implements Resource.getFilename(). It does
|
||||
* not, and AbstractResource.getFilename() is called instead, raising the
|
||||
* exception. The following now works, as getFilename() is called in a
|
||||
* try/catch to check whether the resource is actually file-based or not.
|
||||
*
|
||||
* See SPR-7552, which suggests paths to address the root issue rather than
|
||||
* just patching the problem.
|
||||
* Creates a scenario in which two PPCs are configured, each with different
|
||||
* settings regarding resolving properties from the environment.
|
||||
*/
|
||||
@Test
|
||||
public void repro() {
|
||||
public void twoPlacholderConfigurers_withConflictingSettings() {
|
||||
String P2 = "p2";
|
||||
String P2_LOCAL_PROPS_VAL = "p2LocalPropsVal";
|
||||
String P2_SYSTEM_PROPS_VAL = "p2SystemPropsVal";
|
||||
String P2_SYSTEM_ENV_VAL = "p2SystemEnvVal";
|
||||
|
||||
AbstractBeanDefinition p2BeanDef = rootBeanDefinition(TestBean.class)
|
||||
.addPropertyValue("name", "${"+P1+"}")
|
||||
.addPropertyValue("country", "${"+P2+"}")
|
||||
.getBeanDefinition();
|
||||
|
||||
bf.registerBeanDefinition("p1Bean", p1BeanDef);
|
||||
bf.registerBeanDefinition("p2Bean", p2BeanDef);
|
||||
|
||||
ppc.setIgnoreUnresolvablePlaceholders(true);
|
||||
ppc.postProcessBeanFactory(bf);
|
||||
|
||||
System.setProperty(P2, P2_SYSTEM_PROPS_VAL);
|
||||
getModifiableSystemEnvironment().put(P2, P2_SYSTEM_ENV_VAL);
|
||||
Properties ppc2Properties = new Properties();
|
||||
ppc2Properties.put(P2, P2_LOCAL_PROPS_VAL);
|
||||
|
||||
PropertyPlaceholderConfigurer ppc2 = new PropertyPlaceholderConfigurer();
|
||||
ppc2.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
|
||||
ppc2.setProperties(ppc2Properties);
|
||||
|
||||
ppc2Properties = new Properties();
|
||||
ppc2Properties.setProperty(P2, P2_LOCAL_PROPS_VAL);
|
||||
ppc2.postProcessBeanFactory(bf);
|
||||
|
||||
TestBean p1Bean = bf.getBean("p1Bean", TestBean.class);
|
||||
assertThat(p1Bean.getName(), equalTo(P1_LOCAL_PROPS_VAL));
|
||||
|
||||
TestBean p2Bean = bf.getBean("p2Bean", TestBean.class);
|
||||
assertThat(p2Bean.getName(), equalTo(P1_LOCAL_PROPS_VAL));
|
||||
assertThat(p2Bean.getCountry(), equalTo(P2_SYSTEM_PROPS_VAL));
|
||||
|
||||
System.clearProperty(P2);
|
||||
getModifiableSystemEnvironment().remove(P2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customPlaceholderPrefixAndSuffix() {
|
||||
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
|
||||
ppc.setPlaceholderPrefix("@<");
|
||||
ppc.setPlaceholderSuffix(">");
|
||||
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
bf.registerBeanDefinition("testBean",
|
||||
rootBeanDefinition(TestBean.class)
|
||||
.addPropertyValue("name", "${my.name}").getBeanDefinition());
|
||||
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
|
||||
ppc.setLocation(new ByteArrayResource("my.name=Inigo Montoya".getBytes()));
|
||||
ppc.postProcessBeanFactory(bf);
|
||||
.addPropertyValue("name", "@<key1>")
|
||||
.addPropertyValue("sex", "${key2}")
|
||||
.getBeanDefinition());
|
||||
|
||||
TestBean testBean = bf.getBean(TestBean.class);
|
||||
assertThat(testBean.getName(), equalTo("Inigo Montoya"));
|
||||
System.setProperty("key1", "systemKey1Value");
|
||||
System.setProperty("key2", "systemKey2Value");
|
||||
ppc.postProcessBeanFactory(bf);
|
||||
System.clearProperty("key1");
|
||||
System.clearProperty("key2");
|
||||
|
||||
assertThat(bf.getBean(TestBean.class).getName(), is("systemKey1Value"));
|
||||
assertThat(bf.getBean(TestBean.class).getSex(), is("${key2}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nullValueIsPreserved() {
|
||||
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
|
||||
ppc.setNullValue("customNull");
|
||||
getModifiableSystemEnvironment().put("my.name", "customNull");
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
bf.registerBeanDefinition("testBean", rootBeanDefinition(TestBean.class)
|
||||
.addPropertyValue("name", "${my.name}")
|
||||
.getBeanDefinition());
|
||||
ppc.postProcessBeanFactory(bf);
|
||||
assertThat(bf.getBean(TestBean.class).getName(), nullValue());
|
||||
getModifiableSystemEnvironment().remove("my.name");
|
||||
}
|
||||
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Tests for functionality not possible prior to Environment refactoring
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Tests that properties against a BeanFactory's Environment are used by
|
||||
* PropertyPlaceholderConfigurer during placeholder resolution.
|
||||
@Test @SuppressWarnings({ "unchecked", "rawtypes", "serial" })
|
||||
public void replacePlaceholdersFromBeanFactoryEnvironmentPropertySources() {
|
||||
System.setProperty("key1", "systemValue");
|
||||
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
bf.getEnvironment().addPropertySource("psCustom", new HashMap() {{ put("key1", "customValue"); }});
|
||||
bf.registerBeanDefinition("testBean",
|
||||
rootBeanDefinition(TestBean.class).addPropertyValue("name", "${key1}").getBeanDefinition());
|
||||
|
||||
new PropertyPlaceholderConfigurer().postProcessBeanFactory(bf);
|
||||
assertThat(bf.getBean(TestBean.class).getName(), is("customValue"));
|
||||
|
||||
System.clearProperty("key1");
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Utilities
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
// TODO SPR-7508: duplicated from EnvironmentPropertyResolutionSearchTests
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Map<String, String> getModifiableSystemEnvironment() {
|
||||
Class<?>[] classes = Collections.class.getDeclaredClasses();
|
||||
Map<String, String> env = System.getenv();
|
||||
for (Class<?> cl : classes) {
|
||||
if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
|
||||
try {
|
||||
Field field = cl.getDeclaredField("m");
|
||||
field.setAccessible(true);
|
||||
Object obj = field.get(env);
|
||||
return (Map<String, String>) obj;
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,14 @@
|
||||
|
||||
package org.springframework.beans.factory.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition;
|
||||
import static test.util.TestResourceUtils.qualifiedResource;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -23,18 +31,12 @@ import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.prefs.Preferences;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import test.beans.IndexedTestBean;
|
||||
import test.beans.TestBean;
|
||||
import static test.util.TestResourceUtils.*;
|
||||
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.beans.factory.BeanInitializationException;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.*;
|
||||
import org.springframework.beans.factory.support.ChildBeanDefinition;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.ManagedList;
|
||||
@@ -43,11 +45,15 @@ import org.springframework.beans.factory.support.ManagedSet;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import test.beans.IndexedTestBean;
|
||||
import test.beans.TestBean;
|
||||
|
||||
/**
|
||||
* Unit tests for various {@link PropertyResourceConfigurer} implementations including:
|
||||
* {@link PropertyPlaceholderConfigurer}, {@link PropertyOverrideConfigurer} and
|
||||
* {@link PreferencesPlaceholderConfigurer}.
|
||||
*
|
||||
* @see PropertyPlaceholderConfigurerTests
|
||||
* @since 02.10.2003
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
@@ -794,6 +800,39 @@ public final class PropertyResourceConfigurerTests {
|
||||
Preferences.systemRoot().node("mySystemPath/mypath").remove("myName");
|
||||
}
|
||||
|
||||
/* TODO SPR-7508: uncomment after EnvironmentAwarePropertyPlaceholderConfigurer implementation
|
||||
@Test
|
||||
public void testPreferencesPlaceholderConfigurerWithCustomPropertiesInEnvironment() {
|
||||
factory.registerBeanDefinition("tb",
|
||||
genericBeanDefinition(TestBean.class)
|
||||
.addPropertyValue("name", "${mypath/myName}")
|
||||
.addPropertyValue("age", "${myAge}")
|
||||
.addPropertyValue("touchy", "${myotherpath/myTouchy}")
|
||||
.getBeanDefinition());
|
||||
|
||||
Properties props = new Properties();
|
||||
props.put("myAge", "99");
|
||||
factory.getEnvironment().getPropertySources().add(new PropertiesPropertySource("localProps", props));
|
||||
|
||||
PreferencesPlaceholderConfigurer ppc = new PreferencesPlaceholderConfigurer();
|
||||
ppc.setSystemTreePath("mySystemPath");
|
||||
ppc.setUserTreePath("myUserPath");
|
||||
Preferences.systemRoot().node("mySystemPath").node("mypath").put("myName", "myNameValue");
|
||||
Preferences.systemRoot().node("mySystemPath/myotherpath").put("myTouchy", "myTouchyValue");
|
||||
Preferences.userRoot().node("myUserPath/myotherpath").put("myTouchy", "myOtherTouchyValue");
|
||||
ppc.afterPropertiesSet();
|
||||
ppc.postProcessBeanFactory(factory);
|
||||
|
||||
TestBean tb = (TestBean) factory.getBean("tb");
|
||||
assertEquals("myNameValue", tb.getName());
|
||||
assertEquals(99, tb.getAge());
|
||||
assertEquals("myOtherTouchyValue", tb.getTouchy());
|
||||
Preferences.userRoot().node("myUserPath/myotherpath").remove("myTouchy");
|
||||
Preferences.systemRoot().node("mySystemPath/myotherpath").remove("myTouchy");
|
||||
Preferences.systemRoot().node("mySystemPath/mypath").remove("myName");
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
private static class ConvertingOverrideConfigurer extends PropertyOverrideConfigurer {
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
|
||||
|
||||
<bean id="testBean" class="test.beans.TestBean">
|
||||
<property name="name" value="topLevel"/>
|
||||
</bean>
|
||||
|
||||
<beans>
|
||||
<bean id="testBean" class="test.beans.TestBean">
|
||||
<property name="name" value="nested"/>
|
||||
</bean>
|
||||
</beans>
|
||||
</beans>
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
|
||||
|
||||
<bean id="testBean" class="test.beans.TestBean">
|
||||
<property name="name" value="original"/>
|
||||
</bean>
|
||||
|
||||
<!-- STS should raise an error for this duplicate id -->
|
||||
<bean id="testBean" class="test.beans.TestBean">
|
||||
<property name="name" value="duplicate"/>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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.xml;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import test.beans.TestBean;
|
||||
|
||||
|
||||
/**
|
||||
* With Spring 3.1, bean id attributes (and all other id attributes across the
|
||||
* core schemas) are no longer typed as xsd:id, but as xsd:string. This allows
|
||||
* for using the same bean id within nested <beans> elements.
|
||||
*
|
||||
* Duplicate ids *within the same level of nesting* will still be treated as an
|
||||
* error through the ProblemReporter, as this could never be an intended/valid
|
||||
* situation.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @since 3.1
|
||||
* @see org.springframework.beans.factory.xml.XmlBeanFactoryTests#testWithDuplicateName
|
||||
* @see org.springframework.beans.factory.xml.XmlBeanFactoryTests#testWithDuplicateNameInAlias
|
||||
*/
|
||||
public class DuplicateBeanIdTests {
|
||||
|
||||
@Test
|
||||
public void duplicateBeanIdsWithinSameNestingLevelRaisesError() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
|
||||
try {
|
||||
reader.loadBeanDefinitions(new ClassPathResource("DuplicateBeanIdTests-sameLevel-context.xml", this.getClass()));
|
||||
fail("expected parsing exception due to duplicate ids in same nesting level");
|
||||
} catch (Exception ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void duplicateBeanIdsAcrossNestingLevels() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
|
||||
reader.loadBeanDefinitions(new ClassPathResource("DuplicateBeanIdTests-multiLevel-context.xml", this.getClass()));
|
||||
TestBean testBean = bf.getBean(TestBean.class); // there should be only one
|
||||
assertThat(testBean.getName(), equalTo("nested"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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.xml;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.hamcrest.Description;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.junit.Test;
|
||||
import org.junit.internal.matchers.TypeSafeMatcher;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.core.env.DefaultEnvironment;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
* TODO SPR-7508: document
|
||||
*
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class EnvironmentBeansTests {
|
||||
|
||||
private static final String PROD_ELIGIBLE_XML = "environmentBeans-prodProfile.xml";
|
||||
private static final String DEV_ELIGIBLE_XML = "environmentBeans-devProfile.xml";
|
||||
private static final String ALL_ELIGIBLE_XML = "environmentBeans-noProfile.xml";
|
||||
private static final String MULTI_ELIGIBLE_XML = "environmentBeans-multiProfile.xml";
|
||||
private static final String UNKOWN_ELIGIBLE_XML = "environmentBeans-unknownProfile.xml";
|
||||
|
||||
private static final String PROD_ACTIVE = "prod";
|
||||
private static final String DEV_ACTIVE = "dev";
|
||||
private static final String NULL_ACTIVE = null;
|
||||
private static final String UNKNOWN_ACTIVE = "unknown";
|
||||
private static final String[] NONE_ACTIVE = new String[0];
|
||||
private static final String[] MULTI_ACTIVE = new String[] { PROD_ACTIVE, DEV_ACTIVE };
|
||||
|
||||
private static final String TARGET_BEAN = "foo";
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
assertThat(beanFactoryFor(PROD_ELIGIBLE_XML, NONE_ACTIVE), not(containsTargetBean()));
|
||||
assertThat(beanFactoryFor(PROD_ELIGIBLE_XML, NULL_ACTIVE), not(containsTargetBean()));
|
||||
assertThat(beanFactoryFor(PROD_ELIGIBLE_XML, DEV_ACTIVE), not(containsTargetBean()));
|
||||
assertThat(beanFactoryFor(PROD_ELIGIBLE_XML, PROD_ACTIVE), containsTargetBean());
|
||||
assertThat(beanFactoryFor(PROD_ELIGIBLE_XML, MULTI_ACTIVE), containsTargetBean());
|
||||
|
||||
assertThat(beanFactoryFor(DEV_ELIGIBLE_XML, NONE_ACTIVE), not(containsTargetBean()));
|
||||
assertThat(beanFactoryFor(DEV_ELIGIBLE_XML, NULL_ACTIVE), not(containsTargetBean()));
|
||||
assertThat(beanFactoryFor(DEV_ELIGIBLE_XML, PROD_ACTIVE), not(containsTargetBean()));
|
||||
assertThat(beanFactoryFor(DEV_ELIGIBLE_XML, DEV_ACTIVE), containsTargetBean());
|
||||
assertThat(beanFactoryFor(DEV_ELIGIBLE_XML, MULTI_ACTIVE), containsTargetBean());
|
||||
|
||||
assertThat(beanFactoryFor(ALL_ELIGIBLE_XML, NONE_ACTIVE), containsTargetBean());
|
||||
assertThat(beanFactoryFor(ALL_ELIGIBLE_XML, NULL_ACTIVE), containsTargetBean());
|
||||
assertThat(beanFactoryFor(ALL_ELIGIBLE_XML, DEV_ACTIVE), containsTargetBean());
|
||||
assertThat(beanFactoryFor(ALL_ELIGIBLE_XML, PROD_ACTIVE), containsTargetBean());
|
||||
assertThat(beanFactoryFor(ALL_ELIGIBLE_XML, MULTI_ACTIVE), containsTargetBean());
|
||||
|
||||
assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, NONE_ACTIVE), not(containsTargetBean()));
|
||||
assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, NULL_ACTIVE), not(containsTargetBean()));
|
||||
assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, UNKNOWN_ACTIVE), not(containsTargetBean()));
|
||||
assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, DEV_ACTIVE), containsTargetBean());
|
||||
assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, PROD_ACTIVE), containsTargetBean());
|
||||
assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, MULTI_ACTIVE), containsTargetBean());
|
||||
|
||||
assertThat(beanFactoryFor(UNKOWN_ELIGIBLE_XML, MULTI_ACTIVE), not(containsTargetBean()));
|
||||
}
|
||||
|
||||
private BeanDefinitionRegistry beanFactoryFor(String xmlName, String... activeProfileNames) {
|
||||
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
|
||||
DefaultEnvironment env = new DefaultEnvironment();
|
||||
env.setActiveProfiles(activeProfileNames);
|
||||
reader.setEnvironment(env);
|
||||
reader.loadBeanDefinitions(new ClassPathResource(xmlName, getClass()));
|
||||
return beanFactory;
|
||||
}
|
||||
|
||||
|
||||
private static Matcher<BeanDefinitionRegistry> containsBeanDefinition(final String beanName) {
|
||||
return new TypeSafeMatcher<BeanDefinitionRegistry>() {
|
||||
|
||||
public void describeTo(Description desc) {
|
||||
desc.appendText("a BeanDefinitionRegistry containing bean named ")
|
||||
.appendValue(beanName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchesSafely(BeanDefinitionRegistry beanFactory) {
|
||||
return beanFactory.containsBeanDefinition(beanName);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
private static Matcher<BeanDefinitionRegistry> containsTargetBean() {
|
||||
return containsBeanDefinition(TARGET_BEAN);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* 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.xml;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.hamcrest.Matchers.hasItems;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import test.beans.TestBean;
|
||||
|
||||
/**
|
||||
* Tests for propagating enclosing beans element defaults to nested beans elements.
|
||||
*
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class NestedBeansElementAttributeRecursionTests {
|
||||
|
||||
@Test
|
||||
public void defaultLazyInit() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
|
||||
new ClassPathResource("NestedBeansElementAttributeRecursionTests-lazy-context.xml", this.getClass()));
|
||||
|
||||
BeanDefinition foo = bf.getBeanDefinition("foo");
|
||||
BeanDefinition bar = bf.getBeanDefinition("bar");
|
||||
BeanDefinition baz = bf.getBeanDefinition("baz");
|
||||
BeanDefinition biz = bf.getBeanDefinition("biz");
|
||||
BeanDefinition buz = bf.getBeanDefinition("buz");
|
||||
|
||||
assertThat(foo.isLazyInit(), is(false));
|
||||
assertThat(bar.isLazyInit(), is(true));
|
||||
assertThat(baz.isLazyInit(), is(false));
|
||||
assertThat(biz.isLazyInit(), is(true));
|
||||
assertThat(buz.isLazyInit(), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void defaultMerge() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
|
||||
new ClassPathResource("NestedBeansElementAttributeRecursionTests-merge-context.xml", this.getClass()));
|
||||
|
||||
TestBean topLevel = bf.getBean("topLevelConcreteTestBean", TestBean.class);
|
||||
// has the concrete child bean values
|
||||
assertThat(topLevel.getSomeList(), hasItems("charlie", "delta"));
|
||||
// but does not merge the parent values
|
||||
assertThat(topLevel.getSomeList(), not(hasItems("alpha", "bravo")));
|
||||
|
||||
TestBean firstLevel = bf.getBean("firstLevelNestedTestBean", TestBean.class);
|
||||
// merges all values
|
||||
assertThat(firstLevel.getSomeList(),
|
||||
hasItems("charlie", "delta", "echo", "foxtrot"));
|
||||
|
||||
TestBean secondLevel = bf.getBean("secondLevelNestedTestBean", TestBean.class);
|
||||
// merges all values
|
||||
assertThat(secondLevel.getSomeList(),
|
||||
hasItems("charlie", "delta", "echo", "foxtrot", "golf", "hotel"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultAutowireCandidates() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
|
||||
new ClassPathResource("NestedBeansElementAttributeRecursionTests-autowire-candidates-context.xml", this.getClass()));
|
||||
|
||||
assertThat(bf.getBeanDefinition("fooService").isAutowireCandidate(), is(true));
|
||||
assertThat(bf.getBeanDefinition("fooRepository").isAutowireCandidate(), is(true));
|
||||
assertThat(bf.getBeanDefinition("other").isAutowireCandidate(), is(false));
|
||||
|
||||
assertThat(bf.getBeanDefinition("barService").isAutowireCandidate(), is(true));
|
||||
assertThat(bf.getBeanDefinition("fooController").isAutowireCandidate(), is(false));
|
||||
|
||||
assertThat(bf.getBeanDefinition("bizRepository").isAutowireCandidate(), is(true));
|
||||
assertThat(bf.getBeanDefinition("bizService").isAutowireCandidate(), is(false));
|
||||
|
||||
assertThat(bf.getBeanDefinition("bazService").isAutowireCandidate(), is(true));
|
||||
assertThat(bf.getBeanDefinition("random").isAutowireCandidate(), is(false));
|
||||
assertThat(bf.getBeanDefinition("fooComponent").isAutowireCandidate(), is(false));
|
||||
assertThat(bf.getBeanDefinition("fRepository").isAutowireCandidate(), is(false));
|
||||
|
||||
assertThat(bf.getBeanDefinition("aComponent").isAutowireCandidate(), is(true));
|
||||
assertThat(bf.getBeanDefinition("someService").isAutowireCandidate(), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initMethod() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
|
||||
new ClassPathResource("NestedBeansElementAttributeRecursionTests-init-destroy-context.xml", this.getClass()));
|
||||
|
||||
InitDestroyBean beanA = bf.getBean("beanA", InitDestroyBean.class);
|
||||
InitDestroyBean beanB = bf.getBean("beanB", InitDestroyBean.class);
|
||||
InitDestroyBean beanC = bf.getBean("beanC", InitDestroyBean.class);
|
||||
InitDestroyBean beanD = bf.getBean("beanD", InitDestroyBean.class);
|
||||
|
||||
assertThat(beanA.initMethod1Called, is(true));
|
||||
assertThat(beanB.initMethod2Called, is(true));
|
||||
assertThat(beanC.initMethod3Called, is(true));
|
||||
assertThat(beanD.initMethod2Called, is(true));
|
||||
|
||||
bf.destroySingletons();
|
||||
|
||||
assertThat(beanA.destroyMethod1Called, is(true));
|
||||
assertThat(beanB.destroyMethod2Called, is(true));
|
||||
assertThat(beanC.destroyMethod3Called, is(true));
|
||||
assertThat(beanD.destroyMethod2Called, is(true));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class InitDestroyBean {
|
||||
boolean initMethod1Called;
|
||||
boolean initMethod2Called;
|
||||
boolean initMethod3Called;
|
||||
|
||||
boolean destroyMethod1Called;
|
||||
boolean destroyMethod2Called;
|
||||
boolean destroyMethod3Called;
|
||||
|
||||
void initMethod1() { this.initMethod1Called = true; }
|
||||
void initMethod2() { this.initMethod2Called = true; }
|
||||
void initMethod3() { this.initMethod3Called = true; }
|
||||
|
||||
void destroyMethod1() { this.destroyMethod1Called = true; }
|
||||
void destroyMethod2() { this.destroyMethod2Called = true; }
|
||||
void destroyMethod3() { this.destroyMethod3Called = true; }
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.springframework.beans.factory.xml;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.DefaultEnvironment;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* Tests for new nested beans element support in Spring XML
|
||||
*
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class NestedBeansElementTests {
|
||||
private final Resource XML =
|
||||
new ClassPathResource("NestedBeansElementTests-context.xml", this.getClass());
|
||||
|
||||
@Test
|
||||
public void getBean_withoutActiveProfile() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(XML);
|
||||
|
||||
Object foo = bf.getBean("foo");
|
||||
assertThat(foo, instanceOf(String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getBean_withActiveProfile() {
|
||||
ConfigurableEnvironment env = new DefaultEnvironment();
|
||||
env.setActiveProfiles("dev");
|
||||
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
|
||||
reader.setEnvironment(env);
|
||||
reader.loadBeanDefinitions(XML);
|
||||
|
||||
bf.getBean("devOnlyBean"); // should not throw NSBDE
|
||||
|
||||
Object foo = bf.getBean("foo");
|
||||
assertThat(foo, instanceOf(Integer.class));
|
||||
|
||||
bf.getBean("devOnlyBean");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
|
||||
default-autowire-candidates="foo*">
|
||||
|
||||
<bean id="fooRepository" class="java.lang.Object"/>
|
||||
<bean id="fooService" class="java.lang.Object"/>
|
||||
<bean id="other" class="java.lang.Object"/>
|
||||
|
||||
<beans default-autowire-candidates="*Service">
|
||||
<bean id="barService" class="java.lang.Object"/>
|
||||
<bean id="fooController" class="java.lang.Object"/>
|
||||
|
||||
<beans default-autowire-candidates="*Repository">
|
||||
<bean id="bizRepository" class="java.lang.Object"/>
|
||||
<bean id="bizService" class="java.lang.Object"/>
|
||||
</beans>
|
||||
|
||||
<beans> <!-- should inherit enclosing beans' defaults -->
|
||||
<bean id="bazService" class="java.lang.Object"/>
|
||||
<bean id="random" class="java.lang.Object"/>
|
||||
<bean id="fooComponent" class="java.lang.Object"/>
|
||||
<bean id="fRepository" class="java.lang.Object"/>
|
||||
</beans>
|
||||
|
||||
<beans default-autowire-candidates="*Component">
|
||||
<bean id="aComponent" class="java.lang.Object"/>
|
||||
<bean id="someService" class="java.lang.Object"/>
|
||||
</beans>
|
||||
</beans>
|
||||
</beans>
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
|
||||
|
||||
<bean id="foo" class="java.lang.Object"/>
|
||||
|
||||
<beans default-autowire="constructor">
|
||||
<bean id="bar" class="java.lang.Object"/>
|
||||
|
||||
<beans>
|
||||
<bean id="baz" class="java.lang.Object"/>
|
||||
</beans>
|
||||
</beans>
|
||||
</beans>
|
||||
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
|
||||
default-init-method="initMethod1"
|
||||
default-destroy-method="destroyMethod1">
|
||||
|
||||
<bean id="beanA" class="org.springframework.beans.factory.xml.InitDestroyBean"/>
|
||||
|
||||
<beans default-init-method="initMethod2" default-destroy-method="destroyMethod2">
|
||||
<bean id="beanB" class="org.springframework.beans.factory.xml.InitDestroyBean"/>
|
||||
|
||||
<beans default-init-method="initMethod3" default-destroy-method="destroyMethod3">
|
||||
<bean id="beanC" class="org.springframework.beans.factory.xml.InitDestroyBean"/>
|
||||
</beans>
|
||||
|
||||
<beans> <!-- should fall back to outer defaults (initMethod2 and destroyMethod2) -->
|
||||
<bean id="beanD" class="org.springframework.beans.factory.xml.InitDestroyBean"/>
|
||||
</beans>
|
||||
</beans>
|
||||
</beans>
|
||||
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
|
||||
|
||||
<!-- lazy == false (based on default XSD values) -->
|
||||
<bean id="foo" class="java.lang.Object"/>
|
||||
|
||||
<beans default-lazy-init="true">
|
||||
<!-- lazy == true (explicit default specified) -->
|
||||
<bean id="bar" class="java.lang.Object"/>
|
||||
</beans>
|
||||
|
||||
<beans default-lazy-init="false">
|
||||
<!-- lazy == false (explicit default specified) -->
|
||||
<bean id="baz" class="java.lang.Object"/>
|
||||
</beans>
|
||||
|
||||
<beans default-lazy-init="true">
|
||||
<!-- lazy == true (explicit default specified) -->
|
||||
<bean id="biz" class="java.lang.Object"/>
|
||||
|
||||
<beans>
|
||||
<!-- lazy == true (inherit enclosing <beans> defaults -->
|
||||
<bean id="buz" class="java.lang.Object"/>
|
||||
</beans>
|
||||
</beans>
|
||||
</beans>
|
||||
@@ -0,0 +1,47 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
|
||||
default-merge="false">
|
||||
|
||||
<bean id="abstractTestBean" class="test.beans.TestBean" abstract="true">
|
||||
<property name="someList">
|
||||
<list>
|
||||
<value>alpha</value>
|
||||
<value>bravo</value>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="topLevelConcreteTestBean" parent="abstractTestBean">
|
||||
<property name="someList">
|
||||
<list>
|
||||
<value>charlie</value>
|
||||
<value>delta</value>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<beans default-merge="true">
|
||||
<bean id="firstLevelNestedTestBean" parent="topLevelConcreteTestBean">
|
||||
<property name="someList">
|
||||
<list>
|
||||
<value>echo</value>
|
||||
<value>foxtrot</value>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<beans>
|
||||
<bean id="secondLevelNestedTestBean" parent="firstLevelNestedTestBean">
|
||||
<property name="someList">
|
||||
<list>
|
||||
<value>golf</value>
|
||||
<value>hotel</value>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
</beans>
|
||||
</beans>
|
||||
</beans>
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
|
||||
|
||||
<bean id="foo" class="java.lang.String"/>
|
||||
|
||||
<beans profile="dev">
|
||||
<!-- should override the 'foo' bean above when the 'dev' profile is active -->
|
||||
<bean name="foo" class="java.lang.Integer">
|
||||
<constructor-arg value="42"/>
|
||||
</bean>
|
||||
|
||||
<!-- should only be available when the 'dev' profile is active -->
|
||||
<bean name="devOnlyBean" class="java.lang.Object"/>
|
||||
</beans>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
|
||||
profile="dev">
|
||||
|
||||
<bean id="foo" class="java.lang.String"/>
|
||||
</beans>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
|
||||
profile="dev,prod">
|
||||
|
||||
<bean id="foo" class="java.lang.String"/>
|
||||
</beans>
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
|
||||
|
||||
<bean id="foo" class="java.lang.String"/>
|
||||
</beans>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
|
||||
profile="prod">
|
||||
|
||||
<bean id="foo" class="java.lang.String"/>
|
||||
</beans>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
|
||||
profile="unknown">
|
||||
|
||||
<bean id="foo" class="java.lang.String"/>
|
||||
</beans>
|
||||
Reference in New Issue
Block a user