diff --git a/spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassWithPlaceholderConfigurerBeanTests.java b/spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassWithPlaceholderConfigurerBeanTests.java index 43c3397bc38..3c8d933d06e 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassWithPlaceholderConfigurerBeanTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassWithPlaceholderConfigurerBeanTests.java @@ -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(); - } - -} \ No newline at end of file