Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
0b117858
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0b117858
编写于
3月 28, 2017
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improve tests for PropertySourcesPlaceholderConfigurer
上级
e06871ef
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
68 addition
and
15 deletion
+68
-15
spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassWithPlaceholderConfigurerBeanTests.java
...ConfigurationClassWithPlaceholderConfigurerBeanTests.java
+68
-15
未找到文件。
spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassWithPlaceholderConfigurerBeanTests.java
浏览文件 @
0b117858
/*
* Copyright 2002-201
6
the original author or authors.
* Copyright 2002-201
7
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.
...
...
@@ -16,7 +16,6 @@
package
org.springframework.context.annotation.configuration
;
import
org.junit.Ignore
;
import
org.junit.Test
;
import
org.springframework.beans.factory.annotation.Value
;
...
...
@@ -31,28 +30,37 @@ import static org.hamcrest.CoreMatchers.*;
import
static
org
.
junit
.
Assert
.*;
/**
* A configuration class that registers a
placeholder configurer @Bean method
*
cannot also have @Value fields. Logically, the config class must be instantiated
* in order to invoke the placeholder configurer bean method, and it is a
* chicken-and-egg problem to process the
@Value
field.
* A configuration class that registers a
non-static placeholder configurer {@code @Bean}
*
method cannot also have {@code @Value} fields. Logically, the config class must be
* in
stantiated in
order to invoke the placeholder configurer bean method, and it is a
* chicken-and-egg problem to process the
{@code @Value}
field.
*
* Therefore, placeholder configurers should be put in separate configuration classes
* as has been done in the test below. Simply said, placeholder configurer @Bean methods
* and @Value fields in the same configuration class are mutually exclusive.
* <p>Therefore, placeholder configurer bean methods should either be {@code static} or
* put in separate configuration classes as has been done in the tests below. Simply said,
* placeholder configurer {@code @Bean} methods and {@code @Value} fields in the same
* configuration class are mutually exclusive unless the placeholder configurer
* {@code @Bean} method is {@code static}.
*
* @author Chris Beams
* @author Juergen Hoeller
* @author Sam Brannen
*/
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.
* The solution is to do as {@link #valueFieldsAreProcessedWhenPlaceholderConfigurerIsSegregated()}
* Test which proves that a non-static property placeholder bean cannot be declared
* in the same configuration class that has a {@code @Value} field in need of
* placeholder replacement. It's an obvious chicken-and-egg issue.
*
* <p>One solution is to do as {@link #valueFieldsAreProcessedWhenPlaceholderConfigurerIsSegregated()}
* does and segregate the two bean definitions across configuration classes.
*
* <p>Another solution is to simply make the {@code @Bean} method for the property
* placeholder {@code static} as in
* {@link #valueFieldsAreProcessedWhenStaticPlaceholderConfigurerIsIntegrated()}.
*/
@Ignore
@Test
@Test
@SuppressWarnings
(
"resource"
)
public
void
valueFieldsAreNotProcessedWhenPlaceholderConfigurerIsIntegrated
()
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
ctx
.
register
(
ConfigWithValueFieldAndPlaceholderConfigurer
.
class
);
...
...
@@ -61,10 +69,25 @@ public class ConfigurationClassWithPlaceholderConfigurerBeanTests {
System
.
clearProperty
(
"test.name"
);
TestBean
testBean
=
ctx
.
getBean
(
TestBean
.
class
);
// Proof that the @Value field did not get set:
assertThat
(
testBean
.
getName
(),
nullValue
());
}
@Test
@SuppressWarnings
(
"resource"
)
public
void
valueFieldsAreProcessedWhenStaticPlaceholderConfigurerIsIntegrated
()
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
ctx
.
register
(
ConfigWithValueFieldAndStaticPlaceholderConfigurer
.
class
);
System
.
setProperty
(
"test.name"
,
"foo"
);
ctx
.
refresh
();
System
.
clearProperty
(
"test.name"
);
TestBean
testBean
=
ctx
.
getBean
(
TestBean
.
class
);
assertThat
(
testBean
.
getName
(),
equalTo
(
"foo"
));
}
@Test
@SuppressWarnings
(
"resource"
)
public
void
valueFieldsAreProcessedWhenPlaceholderConfigurerIsSegregated
()
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
ctx
.
register
(
ConfigWithValueField
.
class
);
...
...
@@ -78,7 +101,8 @@ public class ConfigurationClassWithPlaceholderConfigurerBeanTests {
}
@Test
public
void
valueFieldsResolveToPlaceholderSpecifiedDefaultValue
()
{
@SuppressWarnings
(
"resource"
)
public
void
valueFieldsResolveToPlaceholderSpecifiedDefaultValuesWithPlaceholderConfigurer
()
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
ctx
.
register
(
ConfigWithValueField
.
class
);
ctx
.
register
(
ConfigWithPlaceholderConfigurer
.
class
);
...
...
@@ -88,6 +112,18 @@ public class ConfigurationClassWithPlaceholderConfigurerBeanTests {
assertThat
(
testBean
.
getName
(),
equalTo
(
"bar"
));
}
@Test
@SuppressWarnings
(
"resource"
)
public
void
valueFieldsResolveToPlaceholderSpecifiedDefaultValuesWithoutPlaceholderConfigurer
()
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
ctx
.
register
(
ConfigWithValueField
.
class
);
// ctx.register(ConfigWithPlaceholderConfigurer.class);
ctx
.
refresh
();
TestBean
testBean
=
ctx
.
getBean
(
TestBean
.
class
);
assertThat
(
testBean
.
getName
(),
equalTo
(
"bar"
));
}
@Configuration
static
class
ConfigWithValueField
{
...
...
@@ -129,4 +165,21 @@ public class ConfigurationClassWithPlaceholderConfigurerBeanTests {
}
}
@Configuration
static
class
ConfigWithValueFieldAndStaticPlaceholderConfigurer
{
@Value
(
"${test.name}"
)
private
String
name
;
@Bean
public
ITestBean
testBean
()
{
return
new
TestBean
(
this
.
name
);
}
@Bean
public
static
PropertySourcesPlaceholderConfigurer
ppc
()
{
return
new
PropertySourcesPlaceholderConfigurer
();
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录