@PropertySource gets parsed as early as possible

Other PropertySources and in particular @ComponentScan can benefit from previously declared property sources on the same configuration class.

Issue: SPR-12110
Issue: SPR-12111
This commit is contained in:
Juergen Hoeller
2014-08-21 22:43:08 +02:00
parent 86c5880888
commit 7c6088861f
5 changed files with 138 additions and 66 deletions

View File

@@ -76,6 +76,15 @@ public class PropertySourceAnnotationTests {
assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean"));
}
@Test
public void withTestProfileBeans() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ConfigWithTestProfileBeans.class);
ctx.refresh();
assertTrue(ctx.containsBean("testBean"));
assertTrue(ctx.containsBean("testProfileBean"));
}
/**
* Tests the LIFO behavior of @PropertySource annotaitons.
* The last one registered should 'win'.
@@ -211,6 +220,7 @@ public class PropertySourceAnnotationTests {
@Configuration
@PropertySource(value="classpath:${unresolvable:org/springframework/context/annotation}/p1.properties")
static class ConfigWithUnresolvablePlaceholderAndDefault {
@Inject Environment env;
@Bean
@@ -223,6 +233,7 @@ public class PropertySourceAnnotationTests {
@Configuration
@PropertySource(value="classpath:${path.to.properties}/p1.properties")
static class ConfigWithResolvablePlaceholder {
@Inject Environment env;
@Bean
@@ -232,10 +243,10 @@ public class PropertySourceAnnotationTests {
}
@Configuration
@PropertySource(name="p1", value="classpath:org/springframework/context/annotation/p1.properties")
static class ConfigWithExplicitName {
@Inject Environment env;
@Bean
@@ -248,6 +259,7 @@ public class PropertySourceAnnotationTests {
@Configuration
@PropertySource("classpath:org/springframework/context/annotation/p1.properties")
static class ConfigWithImplicitName {
@Inject Environment env;
@Bean
@@ -257,6 +269,20 @@ public class PropertySourceAnnotationTests {
}
@Configuration
@PropertySource(name="p1", value="classpath:org/springframework/context/annotation/p1.properties")
@ComponentScan("org.springframework.context.annotation.spr12111")
static class ConfigWithTestProfileBeans {
@Inject Environment env;
@Bean @Profile("test")
public TestBean testBean() {
return new TestBean(env.getProperty("testbean.name"));
}
}
@Configuration
@PropertySource("classpath:org/springframework/context/annotation/p2.properties")
static class P2Config {
@@ -287,7 +313,7 @@ public class PropertySourceAnnotationTests {
@Configuration
@PropertySources({
@PropertySource("classpath:org/springframework/context/annotation/p1.properties"),
@PropertySource("classpath:org/springframework/context/annotation/p2.properties"),
@PropertySource("classpath:${base.package}/p2.properties"),
})
static class ConfigWithPropertySources {
}

View File

@@ -1,2 +1,4 @@
testbean.name=p1TestBean
from.p1=p1Value
from.p1=p1Value
base.package=org/springframework/context/annotation
spring.profiles.active=test

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2002-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.context.annotation.spr12111;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
@Component
@Profile("test")
public class TestProfileBean {
}