Skip to content

Commit 2065b78

Browse files
committed
Re-introduce system-properties-mode to spring-context 3.1 XSD
See JIRA issue for complete details. Issue: SPR-8901
1 parent e9da854 commit 2065b78

File tree

4 files changed

+85
-20
lines changed

4 files changed

+85
-20
lines changed

org.springframework.context/src/main/java/org/springframework/context/config/PropertyPlaceholderBeanDefinitionParser.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@
2424
import org.springframework.util.StringUtils;
2525

2626
/**
27-
* Parser for the <context:property-placeholder/> element.
27+
* Parser for the {@code <context:property-placeholder/>} element.
2828
*
2929
* @author Juergen Hoeller
3030
* @author Dave Syer
@@ -33,13 +33,22 @@
3333
*/
3434
class PropertyPlaceholderBeanDefinitionParser extends AbstractPropertyLoadingBeanDefinitionParser {
3535

36+
private static final String SYSTEM_PROPERTIES_MODE_ATTRIB = "system-properties-mode";
37+
private static final String SYSTEM_PROPERTIES_MODE_DEFAULT = "ENVIRONMENT";
38+
3639
@Override
3740
protected Class<?> getBeanClass(Element element) {
38-
if (element.hasAttribute("system-properties-mode")) {
39-
return PropertyPlaceholderConfigurer.class;
41+
// As of Spring 3.1, the default value of system-properties-mode has changed from
42+
// 'FALLBACK' to 'ENVIRONMENT'. This latter value indicates that resolution of
43+
// placeholders against system properties is a function of the Environment and
44+
// its current set of PropertySources
45+
if (element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIB).equals(SYSTEM_PROPERTIES_MODE_DEFAULT)) {
46+
return PropertySourcesPlaceholderConfigurer.class;
4047
}
4148

42-
return PropertySourcesPlaceholderConfigurer.class;
49+
// the user has explicitly specified a value for system-properties-mode. Revert
50+
// to PropertyPlaceholderConfigurer to ensure backward compatibility.
51+
return PropertyPlaceholderConfigurer.class;
4352
}
4453

4554
@Override
@@ -49,8 +58,9 @@ protected void doParse(Element element, BeanDefinitionBuilder builder) {
4958
builder.addPropertyValue("ignoreUnresolvablePlaceholders",
5059
Boolean.valueOf(element.getAttribute("ignore-unresolvable")));
5160

52-
String systemPropertiesModeName = element.getAttribute("system-properties-mode");
53-
if (StringUtils.hasLength(systemPropertiesModeName)) {
61+
String systemPropertiesModeName = element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIB);
62+
if (StringUtils.hasLength(systemPropertiesModeName) &&
63+
!systemPropertiesModeName.equals(SYSTEM_PROPERTIES_MODE_DEFAULT)) {
5464
builder.addPropertyValue("systemPropertiesModeName", "SYSTEM_PROPERTIES_MODE_"+systemPropertiesModeName);
5565
}
5666
}

org.springframework.context/src/main/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,30 @@
3434
import org.springframework.util.StringValueResolver;
3535

3636
/**
37-
* Specialization of {@link org.springframework.beans.factory.config.PlaceholderConfigurerSupport}
37+
* Specialization of {@link org.springframework.beans.factory.config.PlaceholderConfigurerSupport
38+
* PlaceholderConfigurerSupport} that resolves ${...} placeholders within bean definition
39+
* property values and {@code @Value} annotations against the current Spring {@link
40+
* Environment} and its set of {@link PropertySources}.
3841
*
39-
* <p>Local properties are added as a property source in any case. Precedence is based
40-
* on the value of the {@link #setLocalOverride localOverride} property.
42+
* <p>This class is designed as a general replacement for {@code
43+
* PropertyPlaceholderConfigurer} in Spring 3.1 applications. It is used by default to
44+
* support the {@code property-placeholder} element in working against the
45+
* spring-context-3.1 XSD, whereas spring-context versions &lt;= 3.0 default to
46+
* {@code PropertyPlaceholderConfigurer} to ensure backward compatibility. See
47+
* spring-context XSD documentation for complete details.
48+
*
49+
* <p>Any local properties (e.g. those added via {@link #setProperties},
50+
* {@link #setLocations} et al.) are added as a {@code PropertySource}. Search precedence
51+
* of local properties is based on the value of the {@link #setLocalOverride localOverride}
52+
* property, which is by default {@code false} meaning that local properties are to be
53+
* searched last, after all environment property sources.
54+
*
55+
* <p>See {@link org.springframework.core.env.ConfigurableEnvironment ConfigurableEnvironment}
56+
* and related Javadoc for details on manipulating environment property sources.
4157
*
4258
* @author Chris Beams
4359
* @since 3.1
60+
* @see org.springframework.core.env.ConfigurableEnvironment
4461
* @see org.springframework.beans.factory.config.PlaceholderConfigurerSupport
4562
* @see org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
4663
*/

org.springframework.context/src/main/resources/org/springframework/context/config/spring-context-3.1.xsd

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,20 @@
8989
<xsd:element name="property-placeholder">
9090
<xsd:annotation>
9191
<xsd:documentation><![CDATA[
92-
Activates replacement of ${...} placeholders, resolved against the specified properties file or
93-
Properties object (if any). Defines a PropertySourcesPlaceholderConfigurer within the context.
94-
For backward compatibility with versions earlier than Spring 3.1, a PropertyPlaceholderConfigurer will be
95-
registered if the 'system-properties-mode' attribute has been explicitly assigned a value.
92+
Activates replacement of ${...} placeholders by registering a
93+
PropertySourcesPlaceholderConfigurer within the application context. Properties will
94+
be resolved against the specified properties file or Properties object -- so called
95+
"local properties", if any, and against the Spring Environment's current set of
96+
PropertySources.
97+
98+
Note that as of Spring 3.1 the system-properties-mode attribute has been removed in
99+
favor of the more flexible PropertySources mechanism. However, Spring 3.1-based
100+
applications may continue to use the 3.0 (and older) versions of the spring-context
101+
schema in order to preserve system-properties-mode behavior. In this case, the
102+
traditional PropertyPlaceholderConfigurer component will be registered instead of the
103+
new PropertySourcesPlaceholderConfigurer.
104+
105+
See ConfigurableEnvironment Javadoc for more information on using.
96106
]]></xsd:documentation>
97107
<xsd:appinfo>
98108
<tool:annotation>
@@ -103,7 +113,39 @@
103113
</xsd:annotation>
104114
<xsd:complexType>
105115
<xsd:complexContent>
106-
<xsd:extension base="propertyPlaceholder" />
116+
<xsd:extension base="propertyPlaceholder">
117+
<xsd:attribute name="system-properties-mode" default="ENVIRONMENT">
118+
<xsd:annotation>
119+
<xsd:documentation><![CDATA[
120+
Controls how to resolve placeholders against system properties. As of Spring 3.1, this
121+
attribute value defaults to "ENVIRONMENT", indicating that resolution of placeholders
122+
against system properties is handled via PropertySourcesPlaceholderConfigurer and its
123+
delegation to the current Spring Environment object.
124+
125+
For maximum backward compatibility, this attribute is preserved going forward with the
126+
3.1 version of the context schema, and any values other than the default "ENVIRONMENT"
127+
will cause a traditional PropertyPlaceholderConfigurer to be registered instead of the
128+
newer PropertySourcesPlaceholderConfigurer variant. In this case, the Spring Environment
129+
and its property sources are not interrogated when resolving placeholders. Users are
130+
encouraged to consider this attribute deprecated, and to take advantage of
131+
Environment/PropertySource mechanisms. See ConfigurableEnvironment Javadoc for examples.
132+
133+
"ENVIRONMENT" indicates placeholders should be resolved against the current Environment and against any local properties;
134+
"NEVER" indicates placeholders should be resolved only against local properties and never against system properties;
135+
"FALLBACK" indicates placeholders should be resolved against any local properties and then against system properties;
136+
"OVERRIDE" indicates placeholders should be resolved first against system properties and then against any local properties;
137+
]]></xsd:documentation>
138+
</xsd:annotation>
139+
<xsd:simpleType>
140+
<xsd:restriction base="xsd:string">
141+
<xsd:enumeration value="ENVIRONMENT"/>
142+
<xsd:enumeration value="NEVER"/>
143+
<xsd:enumeration value="FALLBACK"/>
144+
<xsd:enumeration value="OVERRIDE"/>
145+
</xsd:restriction>
146+
</xsd:simpleType>
147+
</xsd:attribute>
148+
</xsd:extension>
107149
</xsd:complexContent>
108150
</xsd:complexType>
109151
</xsd:element>
@@ -360,10 +402,6 @@
360402
Signals the current application context to apply dependency injection
361403
to non-managed classes that are instantiated outside of the Spring bean
362404
factory (typically classes annotated with the @Configurable annotation).
363-
364-
See Javadoc for org.springframework.context.annotation.EnableSpringConfigured in the
365-
spring-aspects module for information on code-based alternatives to bootstrapping
366-
this functionality.
367405
]]></xsd:documentation>
368406
</xsd:annotation>
369407
<xsd:simpleType>

org.springframework.context/src/test/resources/org/springframework/context/config/contextNamespaceHandlerTests-system.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
44
xmlns:util="http://www.springframework.org/schema/util"
55
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
6-
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
6+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
77
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
88

99
<util:properties id="placeholderProps">

0 commit comments

Comments
 (0)