Test for placeholder-specified default value within @Value

Issue: SPR-14025
This commit is contained in:
Juergen Hoeller 2016-03-11 15:00:01 +01:00
parent 0b1639d963
commit b4de66ff9a
1 changed files with 50 additions and 36 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2016 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.
@ -41,15 +41,16 @@ import static org.junit.Assert.*;
* and @Value fields in the same configuration class are mutually exclusive.
*
* @author Chris Beams
* @author Juergen Hoeller
*/
public class ConfigurationClassWithPlaceholderConfigurerBeanTests {
/**
* Intentionally ignored test proving that a property placeholder bean
* cannot be declared in the same configuration class that has a @Value
* field in need of placeholder replacement. It's an obvious chicken-and-egg issue.
* field in need of placeholder replacement. It's an obvious chicken-and-egg issue.
* The solution is to do as {@link #valueFieldsAreProcessedWhenPlaceholderConfigurerIsSegregated()}
* does and segragate the two bean definitions across configuration classes.
* does and segregate the two bean definitions across configuration classes.
*/
@Ignore @Test
public void valueFieldsAreNotProcessedWhenPlaceholderConfigurerIsIntegrated() {
@ -75,44 +76,57 @@ public class ConfigurationClassWithPlaceholderConfigurerBeanTests {
TestBean testBean = ctx.getBean(TestBean.class);
assertThat(testBean.getName(), equalTo("foo"));
}
}
@Configuration
class ConfigWithValueField {
@Test
public void valueFieldsResolveToPlaceholderSpecifiedDefaultValue() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ConfigWithValueField.class);
ctx.register(ConfigWithPlaceholderConfigurer.class);
ctx.refresh();
@Value("${test.name}")
private String name;
@Bean
public ITestBean testBean() {
return new TestBean(this.name);
TestBean testBean = ctx.getBean(TestBean.class);
assertThat(testBean.getName(), equalTo("bar"));
}
}
@Configuration
class ConfigWithPlaceholderConfigurer {
@Bean
public PropertySourcesPlaceholderConfigurer ppc() {
return new PropertySourcesPlaceholderConfigurer();
@Configuration
static class ConfigWithValueField {
@Value("${test.name:bar}")
private String name;
@Bean
public ITestBean testBean() {
return new TestBean(this.name);
}
}
@Configuration
static class ConfigWithPlaceholderConfigurer {
@Bean
public PropertySourcesPlaceholderConfigurer ppc() {
return new PropertySourcesPlaceholderConfigurer();
}
}
@Configuration
static class ConfigWithValueFieldAndPlaceholderConfigurer {
@Value("${test.name}")
private String name;
@Bean
public ITestBean testBean() {
return new TestBean(this.name);
}
@Bean
public PropertySourcesPlaceholderConfigurer ppc() {
return new PropertySourcesPlaceholderConfigurer();
}
}
}
@Configuration
class ConfigWithValueFieldAndPlaceholderConfigurer {
@Value("${test.name}")
private String name;
@Bean
public ITestBean testBean() {
return new TestBean(this.name);
}
@Bean
public PropertySourcesPlaceholderConfigurer ppc() {
return new PropertySourcesPlaceholderConfigurer();
}
}