Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
dc2d5c10
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,发现更多精彩内容 >>
提交
dc2d5c10
编写于
5月 11, 2011
作者:
C
Chris Beams
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add default-value getProperty convenience variants
Issue: SPR-8322
上级
693204ae
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
62 addition
and
3 deletion
+62
-3
org.springframework.core/src/main/java/org/springframework/core/env/AbstractEnvironment.java
...ava/org/springframework/core/env/AbstractEnvironment.java
+8
-0
org.springframework.core/src/main/java/org/springframework/core/env/PropertyResolver.java
...n/java/org/springframework/core/env/PropertyResolver.java
+28
-1
org.springframework.core/src/main/java/org/springframework/core/env/PropertySourcesPropertyResolver.java
...ngframework/core/env/PropertySourcesPropertyResolver.java
+10
-0
org.springframework.core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java
...mework/core/env/PropertySourcesPropertyResolverTests.java
+16
-2
未找到文件。
org.springframework.core/src/main/java/org/springframework/core/env/AbstractEnvironment.java
浏览文件 @
dc2d5c10
...
...
@@ -192,10 +192,18 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
return
this
.
propertyResolver
.
getProperty
(
key
);
}
public
String
getProperty
(
String
key
,
String
defaultValue
)
{
return
this
.
propertyResolver
.
getProperty
(
key
,
defaultValue
);
}
public
<
T
>
T
getProperty
(
String
key
,
Class
<
T
>
targetType
)
{
return
this
.
propertyResolver
.
getProperty
(
key
,
targetType
);
}
public
<
T
>
T
getProperty
(
String
key
,
Class
<
T
>
targetType
,
T
defaultValue
)
{
return
this
.
propertyResolver
.
getProperty
(
key
,
targetType
,
defaultValue
);
};
public
<
T
>
Class
<
T
>
getPropertyAsClass
(
String
key
,
Class
<
T
>
targetType
)
{
return
this
.
propertyResolver
.
getPropertyAsClass
(
key
,
targetType
);
}
...
...
org.springframework.core/src/main/java/org/springframework/core/env/PropertyResolver.java
浏览文件 @
dc2d5c10
...
...
@@ -30,17 +30,44 @@ public interface PropertyResolver {
boolean
containsProperty
(
String
key
);
/**
* Return the property value associated with the given key.
* Return the property value associated with the given key, or {@code null}
* if the key cannot be resolved.
* @param key the property name to resolve
* @see #getProperty(String, String)
* @see #getProperty(String, Class)
* @see #getRequiredProperty(String)
*/
String
getProperty
(
String
key
);
/**
* Return the property value associated with the given key, or
* {@code defaultValue} if the key cannot be resolved.
* @param key the property name to resolve
* @param defaultValue the default value to return if no value is found
* @see #getRequiredProperty(String)
* @see #getProperty(String, Class)
*/
String
getProperty
(
String
key
,
String
defaultValue
);
/**
* Return the property value associated with the given key, or {@code null}
* if the key cannot be resolved.
* @param key the property name to resolve
* @param T the expected type of the property value
* @see #getRequiredProperty(String, Class)
*/
<
T
>
T
getProperty
(
String
key
,
Class
<
T
>
targetType
);
/**
* Return the property value associated with the given key, or
* {@code defaultValue} if the key cannot be resolved.
* @param key the property name to resolve
* @param T the expected type of the property value
* @param defaultValue the default value to return if no value is found
* @see #getRequiredProperty(String, Class)
*/
<
T
>
T
getProperty
(
String
string
,
Class
<
T
>
targetType
,
T
defaultValue
);
/**
* Convert the property value associated with the given key to a {@code Class}
* of type {@code T} or {@code null} if the key cannot be resolved.
...
...
org.springframework.core/src/main/java/org/springframework/core/env/PropertySourcesPropertyResolver.java
浏览文件 @
dc2d5c10
...
...
@@ -56,6 +56,11 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
return
this
.
getProperty
(
key
,
String
.
class
);
}
public
String
getProperty
(
String
key
,
String
defaultValue
)
{
String
value
=
getProperty
(
key
);
return
value
==
null
?
defaultValue
:
value
;
}
public
<
T
>
T
getProperty
(
String
key
,
Class
<
T
>
targetValueType
)
{
boolean
debugEnabled
=
logger
.
isDebugEnabled
();
if
(
logger
.
isTraceEnabled
())
{
...
...
@@ -89,6 +94,11 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
return
null
;
}
public
<
T
>
T
getProperty
(
String
key
,
Class
<
T
>
targetType
,
T
defaultValue
)
{
T
value
=
getProperty
(
key
,
targetType
);
return
value
==
null
?
defaultValue
:
value
;
};
public
<
T
>
Class
<
T
>
getPropertyAsClass
(
String
key
,
Class
<
T
>
targetValueType
)
{
boolean
debugEnabled
=
logger
.
isDebugEnabled
();
if
(
logger
.
isTraceEnabled
())
{
...
...
org.springframework.core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java
浏览文件 @
dc2d5c10
...
...
@@ -65,6 +65,13 @@ public class PropertySourcesPropertyResolverTests {
assertThat
(
propertyResolver
.
getProperty
(
"foo"
),
is
(
"bar"
));
}
@Test
public
void
getProperty_withDefaultValue
()
{
assertThat
(
propertyResolver
.
getProperty
(
"foo"
,
"myDefault"
),
is
(
"myDefault"
));
testProperties
.
put
(
"foo"
,
"bar"
);
assertThat
(
propertyResolver
.
getProperty
(
"foo"
),
is
(
"bar"
));
}
@Test
public
void
getProperty_propertySourceSearchOrderIsFIFO
()
{
MutablePropertySources
sources
=
new
MutablePropertySources
();
...
...
@@ -86,6 +93,13 @@ public class PropertySourcesPropertyResolverTests {
assertThat
(
propertyResolver
.
getProperty
(
"foo"
),
nullValue
());
}
@Test
public
void
getProperty_withTargetType_andDefaultValue
()
{
assertThat
(
propertyResolver
.
getProperty
(
"foo"
,
Integer
.
class
,
42
),
equalTo
(
42
));
testProperties
.
put
(
"foo"
,
13
);
assertThat
(
propertyResolver
.
getProperty
(
"foo"
,
Integer
.
class
,
42
),
equalTo
(
13
));
}
@Test
public
void
getProperty_withStringArrayConversion
()
{
testProperties
.
put
(
"foo"
,
"bar,baz"
);
...
...
@@ -197,7 +211,7 @@ public class PropertySourcesPropertyResolverTests {
}
@Test
public
void
resolvePlaceholders_withDefault
()
{
public
void
resolvePlaceholders_withDefault
Value
()
{
MutablePropertySources
propertySources
=
new
MutablePropertySources
();
propertySources
.
addFirst
(
new
MockPropertySource
().
withProperty
(
"key"
,
"value"
));
PropertyResolver
resolver
=
new
PropertySourcesPropertyResolver
(
propertySources
);
...
...
@@ -227,7 +241,7 @@ public class PropertySourcesPropertyResolverTests {
}
@Test
public
void
resolveRequiredPlaceholders_withDefault
()
{
public
void
resolveRequiredPlaceholders_withDefault
Value
()
{
MutablePropertySources
propertySources
=
new
MutablePropertySources
();
propertySources
.
addFirst
(
new
MockPropertySource
().
withProperty
(
"key"
,
"value"
));
PropertyResolver
resolver
=
new
PropertySourcesPropertyResolver
(
propertySources
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录