Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
16052107
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,体验更适合开发者的 AI 搜索 >>
提交
16052107
编写于
9月 17, 2009
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
avoid caching for expression results even when specified as TypedStringValue (SPR-6115)
上级
8bf1ee5e
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
109 addition
and
6 deletion
+109
-6
org.springframework.beans/src/main/java/org/springframework/beans/factory/config/TypedStringValue.java
...pringframework/beans/factory/config/TypedStringValue.java
+17
-0
org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java
...s/factory/support/AbstractAutowireCapableBeanFactory.java
+2
-1
org.springframework.beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java
...rk/beans/factory/support/BeanDefinitionValueResolver.java
+16
-3
org.springframework.context/src/main/java/org/springframework/context/expression/StandardBeanExpressionResolver.java
...rk/context/expression/StandardBeanExpressionResolver.java
+4
-0
org.springframework.context/src/test/java/org/springframework/context/expression/ApplicationContextExpressionTests.java
...context/expression/ApplicationContextExpressionTests.java
+70
-2
未找到文件。
org.springframework.beans/src/main/java/org/springframework/beans/factory/config/TypedStringValue.java
浏览文件 @
16052107
...
...
@@ -44,6 +44,8 @@ public class TypedStringValue implements BeanMetadataElement {
private
String
specifiedTypeName
;
private
volatile
boolean
dynamic
;
/**
* Create a new {@link TypedStringValue} for the given String value.
...
...
@@ -185,6 +187,21 @@ public class TypedStringValue implements BeanMetadataElement {
return
this
.
specifiedTypeName
;
}
/**
* Mark this value as dynamic, i.e. as containing an expression
* and hence not being subject to caching.
*/
public
void
setDynamic
()
{
this
.
dynamic
=
true
;
}
/**
* Return whether this value has been marked as dynamic.
*/
public
boolean
isDynamic
()
{
return
this
.
dynamic
;
}
@Override
public
boolean
equals
(
Object
other
)
{
...
...
org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java
浏览文件 @
16052107
...
...
@@ -1311,7 +1311,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
}
deepCopy
.
add
(
pv
);
}
else
if
(
originalValue
instanceof
TypedStringValue
&&
convertible
&&
else
if
(
convertible
&&
originalValue
instanceof
TypedStringValue
&&
!((
TypedStringValue
)
originalValue
).
isDynamic
()
&&
!(
convertedValue
instanceof
Collection
||
ObjectUtils
.
isArray
(
convertedValue
)))
{
pv
.
setConvertedValue
(
convertedValue
);
deepCopy
.
add
(
pv
);
...
...
org.springframework.beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionValueResolver.java
浏览文件 @
16052107
...
...
@@ -167,10 +167,10 @@ class BeanDefinitionValueResolver {
Object
propKey
=
propEntry
.
getKey
();
Object
propValue
=
propEntry
.
getValue
();
if
(
propKey
instanceof
TypedStringValue
)
{
propKey
=
evaluate
((
(
TypedStringValue
)
propKey
).
getValue
()
);
propKey
=
evaluate
((
TypedStringValue
)
propKey
);
}
if
(
propValue
instanceof
TypedStringValue
)
{
propValue
=
evaluate
((
(
TypedStringValue
)
propValue
).
getValue
()
);
propValue
=
evaluate
((
TypedStringValue
)
propValue
);
}
copy
.
put
(
propKey
,
propValue
);
}
...
...
@@ -179,7 +179,7 @@ class BeanDefinitionValueResolver {
else
if
(
value
instanceof
TypedStringValue
)
{
// Convert value to target type here.
TypedStringValue
typedStringValue
=
(
TypedStringValue
)
value
;
Object
valueObject
=
evaluate
(
typedStringValue
.
getValue
()
);
Object
valueObject
=
evaluate
(
typedStringValue
);
try
{
Class
<?>
resolvedTargetType
=
resolveTargetType
(
typedStringValue
);
if
(
resolvedTargetType
!=
null
)
{
...
...
@@ -201,6 +201,19 @@ class BeanDefinitionValueResolver {
}
}
/**
* Evaluate the given value as an expression, if necessary.
* @param value the candidate value (may be an expression)
* @return the resolved value
*/
protected
Object
evaluate
(
TypedStringValue
value
)
{
Object
result
=
this
.
beanFactory
.
evaluateBeanDefinitionString
(
value
.
getValue
(),
this
.
beanDefinition
);
if
(
result
!=
value
.
getValue
())
{
value
.
setDynamic
();
}
return
result
;
}
/**
* Evaluate the given value as an expression, if necessary.
* @param value the candidate value (may be an expression)
...
...
org.springframework.context/src/main/java/org/springframework/context/expression/StandardBeanExpressionResolver.java
浏览文件 @
16052107
...
...
@@ -32,6 +32,7 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
import
org.springframework.expression.spel.support.StandardTypeConverter
;
import
org.springframework.expression.spel.support.StandardTypeLocator
;
import
org.springframework.util.Assert
;
import
org.springframework.util.StringUtils
;
/**
* Standard implementation of the
...
...
@@ -109,6 +110,9 @@ public class StandardBeanExpressionResolver implements BeanExpressionResolver {
public
Object
evaluate
(
String
value
,
BeanExpressionContext
evalContext
)
throws
BeansException
{
if
(!
StringUtils
.
hasLength
(
value
))
{
return
value
;
}
try
{
Expression
expr
=
this
.
expressionCache
.
get
(
value
);
if
(
expr
==
null
)
{
...
...
org.springframework.context/src/test/java/org/springframework/context/expression/ApplicationContextExpressionTests.java
浏览文件 @
16052107
...
...
@@ -16,8 +16,8 @@
package
org.springframework.context.expression
;
import
java.util.Properties
;
import
java.io.Serializable
;
import
java.util.Properties
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
...
...
@@ -31,14 +31,15 @@ import org.springframework.beans.factory.annotation.Qualifier;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
;
import
org.springframework.beans.factory.config.Scope
;
import
org.springframework.beans.factory.config.TypedStringValue
;
import
org.springframework.beans.factory.support.AutowireCandidateQualifier
;
import
org.springframework.beans.factory.support.DefaultListableBeanFactory
;
import
org.springframework.beans.factory.support.GenericBeanDefinition
;
import
org.springframework.beans.factory.support.RootBeanDefinition
;
import
org.springframework.context.annotation.AnnotationConfigUtils
;
import
org.springframework.context.support.GenericApplicationContext
;
import
org.springframework.util.StopWatch
;
import
org.springframework.util.SerializationTestUtils
;
import
org.springframework.util.StopWatch
;
/**
* @author Juergen Hoeller
...
...
@@ -170,6 +171,38 @@ public class ApplicationContextExpressionTests {
}
}
@Test
public
void
prototypeCreationReevaluatesExpressions
()
{
GenericApplicationContext
ac
=
new
GenericApplicationContext
();
AnnotationConfigUtils
.
registerAnnotationConfigProcessors
(
ac
);
RootBeanDefinition
rbd
=
new
RootBeanDefinition
(
PrototypeTestBean
.
class
);
rbd
.
setScope
(
RootBeanDefinition
.
SCOPE_PROTOTYPE
);
rbd
.
getPropertyValues
().
addPropertyValue
(
"country"
,
"#{systemProperties.country}"
);
rbd
.
getPropertyValues
().
addPropertyValue
(
"country2"
,
new
TypedStringValue
(
"#{systemProperties.country}"
));
ac
.
registerBeanDefinition
(
"test"
,
rbd
);
ac
.
refresh
();
try
{
System
.
getProperties
().
put
(
"name"
,
"juergen1"
);
System
.
getProperties
().
put
(
"country"
,
"UK1"
);
PrototypeTestBean
tb
=
(
PrototypeTestBean
)
ac
.
getBean
(
"test"
);
assertEquals
(
"juergen1"
,
tb
.
getName
());
assertEquals
(
"UK1"
,
tb
.
getCountry
());
assertEquals
(
"UK1"
,
tb
.
getCountry2
());
System
.
getProperties
().
put
(
"name"
,
"juergen2"
);
System
.
getProperties
().
put
(
"country"
,
"UK2"
);
tb
=
(
PrototypeTestBean
)
ac
.
getBean
(
"test"
);
assertEquals
(
"juergen2"
,
tb
.
getName
());
assertEquals
(
"UK2"
,
tb
.
getCountry
());
assertEquals
(
"UK2"
,
tb
.
getCountry2
());
}
finally
{
System
.
getProperties
().
remove
(
"name"
);
System
.
getProperties
().
remove
(
"country"
);
}
}
@Test
public
void
prototypeCreationIsFastEnough
()
{
if
(
factoryLog
.
isTraceEnabled
()
||
factoryLog
.
isDebugEnabled
())
{
...
...
@@ -302,4 +335,39 @@ public class ApplicationContextExpressionTests {
}
}
public
static
class
PrototypeTestBean
{
public
String
name
;
public
String
country
;
public
String
country2
;
@Value
(
"#{systemProperties.name}"
)
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
String
getName
()
{
return
name
;
}
public
void
setCountry
(
String
country
)
{
this
.
country
=
country
;
}
public
String
getCountry
()
{
return
country
;
}
public
void
setCountry2
(
String
country2
)
{
this
.
country2
=
country2
;
}
public
String
getCountry2
()
{
return
country2
;
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录