Skip to content

Commit 9e67ba8

Browse files
committed
Consistent support for early placeholder resolution in properties locations
Issue: SPR-10502 (cherry picked from commit 8053fef)
1 parent 2bb3522 commit 9e67ba8

File tree

6 files changed

+85
-8
lines changed

6 files changed

+85
-8
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/xml/UtilNamespaceHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -200,6 +200,7 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
200200

201201
String location = element.getAttribute("location");
202202
if (StringUtils.hasLength(location)) {
203+
location = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(location);
203204
String[] locations = StringUtils.commaDelimitedListToStringArray(location);
204205
builder.addPropertyValue("locations", locations);
205206
}

spring-context/src/main/java/org/springframework/context/config/AbstractPropertyLoadingBeanDefinitionParser.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -21,6 +21,7 @@
2121
import org.springframework.beans.factory.config.BeanDefinition;
2222
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2323
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
24+
import org.springframework.beans.factory.xml.ParserContext;
2425
import org.springframework.util.StringUtils;
2526

2627
/**
@@ -39,9 +40,10 @@ protected boolean shouldGenerateId() {
3940
}
4041

4142
@Override
42-
protected void doParse(Element element, BeanDefinitionBuilder builder) {
43+
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
4344
String location = element.getAttribute("location");
4445
if (StringUtils.hasLength(location)) {
46+
location = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(location);
4547
String[] locations = StringUtils.commaDelimitedListToStringArray(location);
4648
builder.addPropertyValue("locations", locations);
4749
}

spring-context/src/main/java/org/springframework/context/config/PropertyOverrideBeanDefinitionParser.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.beans.factory.config.PropertyOverrideConfigurer;
2222
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
23+
import org.springframework.beans.factory.xml.ParserContext;
2324

2425
/**
2526
* Parser for the <context:property-override/> element.
@@ -36,8 +37,8 @@ protected Class<?> getBeanClass(Element element) {
3637
}
3738

3839
@Override
39-
protected void doParse(Element element, BeanDefinitionBuilder builder) {
40-
super.doParse(element, builder);
40+
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
41+
super.doParse(element, parserContext, builder);
4142

4243
builder.addPropertyValue("ignoreInvalidKeys",
4344
Boolean.valueOf(element.getAttribute("ignore-unresolvable")));

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
2222
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
23+
import org.springframework.beans.factory.xml.ParserContext;
2324
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
2425
import org.springframework.util.StringUtils;
2526

@@ -54,8 +55,8 @@ protected Class<?> getBeanClass(Element element) {
5455
}
5556

5657
@Override
57-
protected void doParse(Element element, BeanDefinitionBuilder builder) {
58-
super.doParse(element, builder);
58+
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
59+
super.doParse(element, parserContext, builder);
5960

6061
builder.addPropertyValue("ignoreUnresolvablePlaceholders",
6162
Boolean.valueOf(element.getAttribute("ignore-unresolvable")));

spring-context/src/test/java/org/springframework/context/config/ContextNamespaceHandlerTests.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
package org.springframework.context.config;
1818

19+
import java.io.FileNotFoundException;
1920
import java.util.Calendar;
2021
import java.util.Date;
2122

2223
import org.junit.After;
2324
import org.junit.Test;
2425

26+
import org.springframework.beans.FatalBeanException;
2527
import org.springframework.context.ApplicationContext;
2628
import org.springframework.context.support.ClassPathXmlApplicationContext;
2729
import org.springframework.context.support.GenericXmlApplicationContext;
@@ -89,6 +91,54 @@ public void propertyPlaceholderLocation() throws Exception {
8991
assertEquals("maps", applicationContext.getBean("spam"));
9092
}
9193

94+
@Test
95+
public void propertyPlaceholderLocationWithSystemPropertyForOneLocation() throws Exception {
96+
System.setProperty("properties",
97+
"classpath*:/org/springframework/context/config/test-*.properties");
98+
try {
99+
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
100+
"contextNamespaceHandlerTests-location-placeholder.xml", getClass());
101+
assertEquals("bar", applicationContext.getBean("foo"));
102+
assertEquals("foo", applicationContext.getBean("bar"));
103+
assertEquals("maps", applicationContext.getBean("spam"));
104+
}
105+
finally {
106+
System.clearProperty("properties");
107+
}
108+
}
109+
110+
@Test
111+
public void propertyPlaceholderLocationWithSystemPropertyForMultipleLocations() throws Exception {
112+
System.setProperty("properties",
113+
"classpath*:/org/springframework/context/config/test-*.properties," +
114+
"classpath*:/org/springframework/context/config/empty-*.properties," +
115+
"classpath*:/org/springframework/context/config/missing-*.properties");
116+
try {
117+
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
118+
"contextNamespaceHandlerTests-location-placeholder.xml", getClass());
119+
assertEquals("bar", applicationContext.getBean("foo"));
120+
assertEquals("foo", applicationContext.getBean("bar"));
121+
assertEquals("maps", applicationContext.getBean("spam"));
122+
}
123+
finally {
124+
System.clearProperty("properties");
125+
}
126+
}
127+
128+
@Test
129+
public void propertyPlaceholderLocationWithSystemPropertyMissing() throws Exception {
130+
try {
131+
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
132+
"contextNamespaceHandlerTests-location-placeholder.xml", getClass());
133+
assertEquals("bar", applicationContext.getBean("foo"));
134+
assertEquals("foo", applicationContext.getBean("bar"));
135+
assertEquals("maps", applicationContext.getBean("spam"));
136+
}
137+
catch (FatalBeanException ex) {
138+
assertTrue(ex.getRootCause() instanceof FileNotFoundException);
139+
}
140+
}
141+
92142
@Test
93143
public void propertyPlaceholderIgnored() throws Exception {
94144
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
6+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
7+
8+
<context:property-placeholder location="${properties}" file-encoding="ISO-8859-1" trim-values="true"/>
9+
10+
<bean id="foo" class="java.lang.String">
11+
<constructor-arg value="${foo}"/>
12+
</bean>
13+
14+
<bean id="bar" class="java.lang.String">
15+
<constructor-arg value="${bar}"/>
16+
</bean>
17+
18+
<bean id="spam" class="java.lang.String">
19+
<constructor-arg value="${spam}"/>
20+
</bean>
21+
22+
</beans>

0 commit comments

Comments
 (0)