diff --git a/spring-context/src/main/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.java b/spring-context/src/main/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.java index c831bbb8551..f5ede2006cb 100644 --- a/spring-context/src/main/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.java +++ b/spring-context/src/main/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 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. @@ -31,6 +31,7 @@ import org.springframework.core.env.PropertiesPropertySource; import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySources; import org.springframework.core.env.PropertySourcesPropertyResolver; +import org.springframework.util.Assert; import org.springframework.util.StringValueResolver; /** @@ -79,6 +80,8 @@ public class PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerS private MutablePropertySources propertySources; + private PropertySources appliedPropertySources; + private Environment environment; @@ -149,6 +152,7 @@ public class PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerS } this.processProperties(beanFactory, new PropertySourcesPropertyResolver(this.propertySources)); + this.appliedPropertySources = this.propertySources; } /** @@ -186,4 +190,16 @@ public class PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerS "Call processProperties(ConfigurableListableBeanFactory, ConfigurablePropertyResolver) instead"); } + /** + * Returns the property sources that were actually applied during + * {@link #postProcessBeanFactory(ConfigurableListableBeanFactory) post-processing}. + * @return the property sources that were applied + * @throws IllegalStateException if the property sources have not yet been applied + * @since 4.0 + */ + public PropertySources getAppliedPropertySources() throws IllegalStateException { + Assert.state(this.appliedPropertySources != null, "PropertySources have not get been applied"); + return this.appliedPropertySources; + } + } diff --git a/spring-context/src/test/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurerTests.java b/spring-context/src/test/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurerTests.java index 078b1169790..f8f13285c80 100644 --- a/spring-context/src/test/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurerTests.java +++ b/spring-context/src/test/java/org/springframework/context/support/PropertySourcesPlaceholderConfigurerTests.java @@ -16,28 +16,26 @@ package org.springframework.context.support; -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.genericBeanDefinition; -import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; - import java.util.Properties; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.support.DefaultListableBeanFactory; -import org.springframework.core.env.StandardEnvironment; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; +import org.springframework.core.env.StandardEnvironment; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.env.MockPropertySource; - import org.springframework.tests.sample.beans.TestBean; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import static org.springframework.beans.factory.support.BeanDefinitionBuilder.*; + /** * Unit tests for {@link PropertySourcesPlaceholderConfigurer}. * @@ -46,6 +44,9 @@ import org.springframework.tests.sample.beans.TestBean; */ public class PropertySourcesPlaceholderConfigurerTests { + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Test public void replacementFromEnvironmentProperties() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); @@ -62,6 +63,7 @@ public class PropertySourcesPlaceholderConfigurerTests { ppc.setEnvironment(env); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("myValue")); + assertThat(ppc.getAppliedPropertySources(), not(nullValue())); } @Test @@ -104,6 +106,7 @@ public class PropertySourcesPlaceholderConfigurerTests { pc.setPropertySources(propertySources); pc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("foo")); + assertEquals(pc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next()); } @Test @@ -123,6 +126,7 @@ public class PropertySourcesPlaceholderConfigurerTests { pc.setIgnoreUnresolvablePlaceholders(true); pc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}")); + assertEquals(pc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next()); } @Test @@ -254,4 +258,11 @@ public class PropertySourcesPlaceholderConfigurerTests { ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), nullValue()); } + + @Test + public void getAppliedPropertySourcesTooEarly() throws Exception { + PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); + thrown.expect(IllegalStateException.class); + ppc.getAppliedPropertySources(); + } }