From f17f970144f43270c8a34a481a0f391037315740 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Fri, 11 Mar 2011 04:08:10 +0000 Subject: [PATCH] Allow other delimiters in profile XML attribute Previously, only commas could delimit . Now, as with , the profile attribute allows for delimiting by comma, space and/or semicolon. BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS has been added as a constant to reflect the fact this set of delimiters is used in multiple locations throughout the framework. BDPD.BEAN_NAME_DELIMITERS now refers to the above and has been has been preserved but deprecated for backward compat (though use outside the framework is unlikely). Changes originally based on user comment at http://blog.springsource.com/2011/02/11/spring-framework-3-1-m1-released/comment-page-1/#comment-184455 Issue: SPR-8033 --- .../factory/xml/BeanDefinitionParserDelegate.java | 9 ++++++--- .../xml/DefaultBeanDefinitionDocumentReader.java | 5 +---- .../beans/factory/xml/spring-beans-3.1.xsd | 11 ++++++----- .../factory/xml/ProfileXmlBeanDefinitionTests.java | 8 ++++++++ ...leXmlBeanDefinitionTests-spaceDelimitedProfile.xml | 9 +++++++++ 5 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 org.springframework.beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-spaceDelimitedProfile.xml diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java index c515cbae60..3cc7e29abc 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/BeanDefinitionParserDelegate.java @@ -87,7 +87,10 @@ public class BeanDefinitionParserDelegate { public static final String BEANS_NAMESPACE_URI = "http://www.springframework.org/schema/beans"; - public static final String BEAN_NAME_DELIMITERS = ",; "; + public static final String MULTI_VALUE_ATTRIBUTE_DELIMITERS = ",; "; + + /** @deprecated as of Spring 3.1 in favor of {@link #MULTI_VALUE_ATTRIBUTE_DELIMITERS} */ + public static final String BEAN_NAME_DELIMITERS = MULTI_VALUE_ATTRIBUTE_DELIMITERS; /** * Value of a T/F attribute that represents true. @@ -451,7 +454,7 @@ public class BeanDefinitionParserDelegate { List aliases = new ArrayList(); if (StringUtils.hasLength(nameAttr)) { - String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, BEAN_NAME_DELIMITERS); + String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, MULTI_VALUE_ATTRIBUTE_DELIMITERS); aliases.addAll(Arrays.asList(nameArr)); } @@ -624,7 +627,7 @@ public class BeanDefinitionParserDelegate { if (ele.hasAttribute(DEPENDS_ON_ATTRIBUTE)) { String dependsOn = ele.getAttribute(DEPENDS_ON_ATTRIBUTE); - bd.setDependsOn(StringUtils.tokenizeToStringArray(dependsOn, BEAN_NAME_DELIMITERS)); + bd.setDependsOn(StringUtils.tokenizeToStringArray(dependsOn, MULTI_VALUE_ATTRIBUTE_DELIMITERS)); } String autowireCandidate = ele.getAttribute(AUTOWIRE_CANDIDATE_ATTRIBUTE); diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/DefaultBeanDefinitionDocumentReader.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/DefaultBeanDefinitionDocumentReader.java index 1c1419ff94..a0369a7894 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/DefaultBeanDefinitionDocumentReader.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/xml/DefaultBeanDefinitionDocumentReader.java @@ -16,9 +16,6 @@ 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; @@ -124,7 +121,7 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE); if (StringUtils.hasText(profileSpec)) { Assert.state(this.environment != null, "environment property must not be null"); - String[] specifiedProfiles = commaDelimitedListToStringArray(trimAllWhitespace(profileSpec)); + String[] specifiedProfiles = StringUtils.tokenizeToStringArray(profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS); if (!this.environment.acceptsProfiles(specifiedProfiles)) { // TODO SPR-7508: log that this bean is being rejected on profile mismatch return; diff --git a/org.springframework.beans/src/main/resources/org/springframework/beans/factory/xml/spring-beans-3.1.xsd b/org.springframework.beans/src/main/resources/org/springframework/beans/factory/xml/spring-beans-3.1.xsd index 9e148d5302..e5400988e7 100644 --- a/org.springframework.beans/src/main/resources/org/springframework/beans/factory/xml/spring-beans-3.1.xsd +++ b/org.springframework.beans/src/main/resources/org/springframework/beans/factory/xml/spring-beans-3.1.xsd @@ -82,11 +82,12 @@ element may be parsed. May be a comma-delimited - list in the case of multiple profiles. If one or more of the specified profiles are active at time - of parsing, the element will be parsed, and all of its elements registered, - <import> elements followed, etc. If none of the specified profiles are active at time of parsing, - then the entire element and its contents will be ignored. + The set of profiles for which this element may be parsed. Multiple profiles can be + separated by any number of spaces, commas, or semi-colons (or indeed any mixture of the three). + If one or more of the specified profiles are active at time of parsing, the element + will be parsed, and all of its elements registered, <import> elements followed, + etc. If none of the specified profiles are active at time of parsing, then the entire element + and its contents will be ignored. Profiles are activated in one of two ways: Programmatic: diff --git a/org.springframework.beans/src/test/java/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests.java b/org.springframework.beans/src/test/java/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests.java index fcf4d06171..c6cb1e408c 100644 --- a/org.springframework.beans/src/test/java/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests.java +++ b/org.springframework.beans/src/test/java/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests.java @@ -40,6 +40,7 @@ public class ProfileXmlBeanDefinitionTests { private static final String DEV_ELIGIBLE_XML = "ProfileXmlBeanDefinitionTests-devProfile.xml"; private static final String ALL_ELIGIBLE_XML = "ProfileXmlBeanDefinitionTests-noProfile.xml"; private static final String MULTI_ELIGIBLE_XML = "ProfileXmlBeanDefinitionTests-multiProfile.xml"; + private static final String MULTI_ELIGIBLE_SPACE_DELIMITED_XML = "ProfileXmlBeanDefinitionTests-spaceDelimitedProfile.xml"; private static final String UNKOWN_ELIGIBLE_XML = "ProfileXmlBeanDefinitionTests-unknownProfile.xml"; private static final String DEFAULT_ELIGIBLE_XML = "ProfileXmlBeanDefinitionTests-defaultProfile.xml"; private static final String CUSTOM_DEFAULT_ELIGIBLE_XML = "ProfileXmlBeanDefinitionTests-customDefaultProfile.xml"; @@ -81,6 +82,13 @@ public class ProfileXmlBeanDefinitionTests { assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, PROD_ACTIVE), containsTargetBean()); assertThat(beanFactoryFor(MULTI_ELIGIBLE_XML, MULTI_ACTIVE), containsTargetBean()); + assertThat(beanFactoryFor(MULTI_ELIGIBLE_SPACE_DELIMITED_XML, NONE_ACTIVE), not(containsTargetBean())); + assertThat(beanFactoryFor(MULTI_ELIGIBLE_SPACE_DELIMITED_XML, NULL_ACTIVE), not(containsTargetBean())); + assertThat(beanFactoryFor(MULTI_ELIGIBLE_SPACE_DELIMITED_XML, UNKNOWN_ACTIVE), not(containsTargetBean())); + assertThat(beanFactoryFor(MULTI_ELIGIBLE_SPACE_DELIMITED_XML, DEV_ACTIVE), containsTargetBean()); + assertThat(beanFactoryFor(MULTI_ELIGIBLE_SPACE_DELIMITED_XML, PROD_ACTIVE), containsTargetBean()); + assertThat(beanFactoryFor(MULTI_ELIGIBLE_SPACE_DELIMITED_XML, MULTI_ACTIVE), containsTargetBean()); + assertThat(beanFactoryFor(UNKOWN_ELIGIBLE_XML, MULTI_ACTIVE), not(containsTargetBean())); } diff --git a/org.springframework.beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-spaceDelimitedProfile.xml b/org.springframework.beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-spaceDelimitedProfile.xml new file mode 100644 index 0000000000..8bfb9a9813 --- /dev/null +++ b/org.springframework.beans/src/test/resources/org/springframework/beans/factory/xml/ProfileXmlBeanDefinitionTests-spaceDelimitedProfile.xml @@ -0,0 +1,9 @@ + + + + +