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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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. * and @Value fields in the same configuration class are mutually exclusive.
* *
* @author Chris Beams * @author Chris Beams
* @author Juergen Hoeller
*/ */
public class ConfigurationClassWithPlaceholderConfigurerBeanTests { public class ConfigurationClassWithPlaceholderConfigurerBeanTests {
/** /**
* Intentionally ignored test proving that a property placeholder bean * Intentionally ignored test proving that a property placeholder bean
* cannot be declared in the same configuration class that has a @Value * 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()} * 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 @Ignore @Test
public void valueFieldsAreNotProcessedWhenPlaceholderConfigurerIsIntegrated() { public void valueFieldsAreNotProcessedWhenPlaceholderConfigurerIsIntegrated() {
@ -75,44 +76,57 @@ public class ConfigurationClassWithPlaceholderConfigurerBeanTests {
TestBean testBean = ctx.getBean(TestBean.class); TestBean testBean = ctx.getBean(TestBean.class);
assertThat(testBean.getName(), equalTo("foo")); assertThat(testBean.getName(), equalTo("foo"));
} }
}
@Configuration @Test
class ConfigWithValueField { public void valueFieldsResolveToPlaceholderSpecifiedDefaultValue() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ConfigWithValueField.class);
ctx.register(ConfigWithPlaceholderConfigurer.class);
ctx.refresh();
@Value("${test.name}") TestBean testBean = ctx.getBean(TestBean.class);
private String name; assertThat(testBean.getName(), equalTo("bar"));
@Bean
public ITestBean testBean() {
return new TestBean(this.name);
} }
}
@Configuration
class ConfigWithPlaceholderConfigurer {
@Bean @Configuration
public PropertySourcesPlaceholderConfigurer ppc() { static class ConfigWithValueField {
return new PropertySourcesPlaceholderConfigurer();
@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();
}
}