Skip to content

Commit eb1776e

Browse files
committed
Allow access to applied property sources
Add PropertySourcesPlaceholderConfigurer.getAppliedPropertySources() to allow access to the PropertySources that were actually applied. Issue: SPR-10545
1 parent 0652feb commit eb1776e

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 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.
@@ -31,6 +31,7 @@
3131
import org.springframework.core.env.PropertySource;
3232
import org.springframework.core.env.PropertySources;
3333
import org.springframework.core.env.PropertySourcesPropertyResolver;
34+
import org.springframework.util.Assert;
3435
import org.springframework.util.StringValueResolver;
3536

3637
/**
@@ -79,6 +80,8 @@ public class PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerS
7980

8081
private MutablePropertySources propertySources;
8182

83+
private PropertySources appliedPropertySources;
84+
8285
private Environment environment;
8386

8487

@@ -149,6 +152,7 @@ public String getProperty(String key) {
149152
}
150153

151154
this.processProperties(beanFactory, new PropertySourcesPropertyResolver(this.propertySources));
155+
this.appliedPropertySources = this.propertySources;
152156
}
153157

154158
/**
@@ -186,4 +190,16 @@ protected void processProperties(ConfigurableListableBeanFactory beanFactory, Pr
186190
"Call processProperties(ConfigurableListableBeanFactory, ConfigurablePropertyResolver) instead");
187191
}
188192

193+
/**
194+
* Returns the property sources that were actually applied during
195+
* {@link #postProcessBeanFactory(ConfigurableListableBeanFactory) post-processing}.
196+
* @return the property sources that were applied
197+
* @throws IllegalStateException if the property sources have not yet been applied
198+
* @since 4.0
199+
*/
200+
public PropertySources getAppliedPropertySources() throws IllegalStateException {
201+
Assert.state(this.appliedPropertySources != null, "PropertySources have not get been applied");
202+
return this.appliedPropertySources;
203+
}
204+
189205
}

spring-context/src/test/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurerTests.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,26 @@
1616

1717
package org.springframework.context.support;
1818

19-
import static org.hamcrest.CoreMatchers.equalTo;
20-
import static org.hamcrest.CoreMatchers.is;
21-
import static org.hamcrest.CoreMatchers.nullValue;
22-
import static org.junit.Assert.assertThat;
23-
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition;
24-
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition;
25-
2619
import java.util.Properties;
2720

21+
import org.junit.Rule;
2822
import org.junit.Test;
23+
import org.junit.rules.ExpectedException;
2924
import org.springframework.beans.factory.BeanDefinitionStoreException;
3025
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
31-
import org.springframework.core.env.StandardEnvironment;
3226
import org.springframework.core.env.MutablePropertySources;
3327
import org.springframework.core.env.PropertySource;
28+
import org.springframework.core.env.StandardEnvironment;
3429
import org.springframework.core.io.ClassPathResource;
3530
import org.springframework.core.io.Resource;
3631
import org.springframework.mock.env.MockEnvironment;
3732
import org.springframework.mock.env.MockPropertySource;
38-
3933
import org.springframework.tests.sample.beans.TestBean;
4034

35+
import static org.hamcrest.CoreMatchers.*;
36+
import static org.junit.Assert.*;
37+
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.*;
38+
4139
/**
4240
* Unit tests for {@link PropertySourcesPlaceholderConfigurer}.
4341
*
@@ -46,6 +44,9 @@
4644
*/
4745
public class PropertySourcesPlaceholderConfigurerTests {
4846

47+
@Rule
48+
public ExpectedException thrown = ExpectedException.none();
49+
4950
@Test
5051
public void replacementFromEnvironmentProperties() {
5152
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
@@ -62,6 +63,7 @@ public void replacementFromEnvironmentProperties() {
6263
ppc.setEnvironment(env);
6364
ppc.postProcessBeanFactory(bf);
6465
assertThat(bf.getBean(TestBean.class).getName(), equalTo("myValue"));
66+
assertThat(ppc.getAppliedPropertySources(), not(nullValue()));
6567
}
6668

6769
@Test
@@ -104,6 +106,7 @@ public void explicitPropertySources() {
104106
pc.setPropertySources(propertySources);
105107
pc.postProcessBeanFactory(bf);
106108
assertThat(bf.getBean(TestBean.class).getName(), equalTo("foo"));
109+
assertEquals(pc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
107110
}
108111

109112
@Test
@@ -123,6 +126,7 @@ public void explicitPropertySourcesExcludesEnvironment() {
123126
pc.setIgnoreUnresolvablePlaceholders(true);
124127
pc.postProcessBeanFactory(bf);
125128
assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}"));
129+
assertEquals(pc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
126130
}
127131

128132
@Test
@@ -254,4 +258,11 @@ public void nullValueIsPreserved() {
254258
ppc.postProcessBeanFactory(bf);
255259
assertThat(bf.getBean(TestBean.class).getName(), nullValue());
256260
}
261+
262+
@Test
263+
public void getAppliedPropertySourcesTooEarly() throws Exception {
264+
PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
265+
thrown.expect(IllegalStateException.class);
266+
ppc.getAppliedPropertySources();
267+
}
257268
}

0 commit comments

Comments
 (0)