Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
eb8b5c43
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,发现更多精彩内容 >>
提交
eb8b5c43
编写于
12月 02, 2013
作者:
J
Juergen Hoeller
提交者:
unknown
12月 02, 2013
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polishing
上级
5e02a6dd
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
45 addition
and
46 deletion
+45
-46
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java
...mework/context/annotation/ConfigurationClassEnhancer.java
+45
-46
未找到文件。
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java
浏览文件 @
eb8b5c43
...
...
@@ -59,10 +59,10 @@ import org.springframework.util.ReflectionUtils;
class
ConfigurationClassEnhancer
{
private
static
final
Callback
[]
CALLBACKS
=
new
Callback
[]
{
new
BeanMethodInterceptor
(),
new
DisposableBeanMethodInterceptor
(),
new
BeanFactoryAwareMethodInterceptor
(),
NoOp
.
INSTANCE
new
BeanMethodInterceptor
(),
new
DisposableBeanMethodInterceptor
(),
new
BeanFactoryAwareMethodInterceptor
(),
NoOp
.
INSTANCE
};
private
static
final
ConditionalCallbackFilter
CALLBACK_FILTER
=
new
ConditionalCallbackFilter
(
CALLBACKS
);
...
...
@@ -137,7 +137,6 @@ class ConfigurationClassEnhancer {
}
/**
* Marker interface to be implemented by all @Configuration CGLIB subclasses.
* Facilitates idempotent behavior for {@link ConfigurationClassEnhancer#enhance(Class)}
...
...
@@ -160,6 +159,7 @@ class ConfigurationClassEnhancer {
* @see ConditionalCallbackFilter
*/
private
static
interface
ConditionalCallback
extends
Callback
{
boolean
isMatch
(
Method
candidateMethod
);
}
...
...
@@ -178,24 +178,23 @@ class ConfigurationClassEnhancer {
this
.
callbacks
=
callbacks
;
this
.
callbackTypes
=
new
Class
<?>[
callbacks
.
length
];
for
(
int
i
=
0
;
i
<
callbacks
.
length
;
i
++)
{
callbackTypes
[
i
]
=
callbacks
[
i
].
getClass
();
this
.
callbackTypes
[
i
]
=
callbacks
[
i
].
getClass
();
}
}
@Override
public
int
accept
(
Method
method
)
{
for
(
int
i
=
0
;
i
<
callbacks
.
length
;
i
++)
{
if
(!(
callbacks
[
i
]
instanceof
ConditionalCallback
)
||
((
ConditionalCallback
)
callbacks
[
i
]).
isMatch
(
method
))
{
for
(
int
i
=
0
;
i
<
this
.
callbacks
.
length
;
i
++)
{
if
(!(
this
.
callbacks
[
i
]
instanceof
ConditionalCallback
)
||
((
ConditionalCallback
)
this
.
callbacks
[
i
]).
isMatch
(
method
))
{
return
i
;
}
}
throw
new
IllegalStateException
(
"No callback available for method "
+
method
.
getName
());
throw
new
IllegalStateException
(
"No callback available for method "
+
method
.
getName
());
}
public
Class
<?>[]
getCallbackTypes
()
{
return
callbackTypes
;
return
this
.
callbackTypes
;
}
}
...
...
@@ -208,24 +207,23 @@ class ConfigurationClassEnhancer {
*/
private
static
class
DisposableBeanMethodInterceptor
implements
MethodInterceptor
,
ConditionalCallback
{
@Override
public
boolean
isMatch
(
Method
candidateMethod
)
{
return
candidateMethod
.
getName
().
equals
(
"destroy"
)
&&
candidateMethod
.
getParameterTypes
().
length
==
0
&&
DisposableBean
.
class
.
isAssignableFrom
(
candidateMethod
.
getDeclaringClass
());
}
@Override
public
Object
intercept
(
Object
obj
,
Method
method
,
Object
[]
args
,
MethodProxy
proxy
)
throws
Throwable
{
Enhancer
.
registerStaticCallbacks
(
obj
.
getClass
(),
null
);
//
d
oes the actual (non-CGLIB) superclass actually implement DisposableBean?
//
i
f so, call its dispose() method. If not, just exit.
//
D
oes the actual (non-CGLIB) superclass actually implement DisposableBean?
//
I
f so, call its dispose() method. If not, just exit.
if
(
DisposableBean
.
class
.
isAssignableFrom
(
obj
.
getClass
().
getSuperclass
()))
{
return
proxy
.
invokeSuper
(
obj
,
args
);
}
return
null
;
}
@Override
public
boolean
isMatch
(
Method
candidateMethod
)
{
return
candidateMethod
.
getName
().
equals
(
"destroy"
)
&&
candidateMethod
.
getParameterTypes
().
length
==
0
&&
DisposableBean
.
class
.
isAssignableFrom
(
candidateMethod
.
getDeclaringClass
());
}
}
...
...
@@ -237,27 +235,27 @@ class ConfigurationClassEnhancer {
*/
private
static
class
BeanFactoryAwareMethodInterceptor
implements
MethodInterceptor
,
ConditionalCallback
{
@Override
public
boolean
isMatch
(
Method
candidateMethod
)
{
return
candidateMethod
.
getName
().
equals
(
"setBeanFactory"
)
&&
candidateMethod
.
getParameterTypes
().
length
==
1
&&
candidateMethod
.
getParameterTypes
()[
0
].
equals
(
BeanFactory
.
class
)
&&
BeanFactoryAware
.
class
.
isAssignableFrom
(
candidateMethod
.
getDeclaringClass
());
}
@Override
public
Object
intercept
(
Object
obj
,
Method
method
,
Object
[]
args
,
MethodProxy
proxy
)
throws
Throwable
{
Field
field
=
obj
.
getClass
().
getDeclaredField
(
BEAN_FACTORY_FIELD
);
Assert
.
state
(
field
!=
null
,
"Unable to find generated
bean f
actory field"
);
Assert
.
state
(
field
!=
null
,
"Unable to find generated
BeanF
actory field"
);
field
.
set
(
obj
,
args
[
0
]);
//
d
oes the actual (non-CGLIB) superclass actually implement BeanFactoryAware?
//
i
f so, call its setBeanFactory() method. If not, just exit.
//
D
oes the actual (non-CGLIB) superclass actually implement BeanFactoryAware?
//
I
f so, call its setBeanFactory() method. If not, just exit.
if
(
BeanFactoryAware
.
class
.
isAssignableFrom
(
obj
.
getClass
().
getSuperclass
()))
{
return
proxy
.
invokeSuper
(
obj
,
args
);
}
return
null
;
}
@Override
public
boolean
isMatch
(
Method
candidateMethod
)
{
return
candidateMethod
.
getName
().
equals
(
"setBeanFactory"
)
&&
candidateMethod
.
getParameterTypes
().
length
==
1
&&
candidateMethod
.
getParameterTypes
()[
0
].
equals
(
BeanFactory
.
class
)
&&
BeanFactoryAware
.
class
.
isAssignableFrom
(
candidateMethod
.
getDeclaringClass
());
}
}
...
...
@@ -269,11 +267,6 @@ class ConfigurationClassEnhancer {
*/
private
static
class
BeanMethodInterceptor
implements
MethodInterceptor
,
ConditionalCallback
{
@Override
public
boolean
isMatch
(
Method
candidateMethod
)
{
return
BeanAnnotationHelper
.
isBeanAnnotated
(
candidateMethod
);
}
/**
* Enhance a {@link Bean @Bean} method to check the supplied BeanFactory for the
* existence of this bean object.
...
...
@@ -288,7 +281,7 @@ class ConfigurationClassEnhancer {
ConfigurableBeanFactory
beanFactory
=
getBeanFactory
(
enhancedConfigInstance
);
String
beanName
=
BeanAnnotationHelper
.
determineBeanNameFor
(
beanMethod
);
//
d
etermine whether this bean is a scoped-proxy
//
D
etermine whether this bean is a scoped-proxy
Scope
scope
=
AnnotationUtils
.
findAnnotation
(
beanMethod
,
Scope
.
class
);
if
(
scope
!=
null
&&
scope
.
proxyMode
()
!=
ScopedProxyMode
.
NO
)
{
String
scopedBeanName
=
ScopedProxyCreator
.
getTargetBeanName
(
beanName
);
...
...
@@ -297,28 +290,28 @@ class ConfigurationClassEnhancer {
}
}
//
t
o handle the case of an inter-bean method reference, we must explicitly check the
// container for already cached instances
//
T
o handle the case of an inter-bean method reference, we must explicitly check the
// container for already cached instances
.
//
f
irst, check to see if the requested bean is a FactoryBean. If so, create a subclass
//
F
irst, check to see if the requested bean is a FactoryBean. If so, create a subclass
// proxy that intercepts calls to getObject() and returns any cached bean instance.
//
t
his ensures that the semantics of calling a FactoryBean from within @Bean methods
//
T
his ensures that the semantics of calling a FactoryBean from within @Bean methods
// is the same as that of referring to a FactoryBean within XML. See SPR-6602.
if
(
factoryContainsBean
(
beanFactory
,
BeanFactory
.
FACTORY_BEAN_PREFIX
+
beanName
)
&&
factoryContainsBean
(
beanFactory
,
beanName
))
{
Object
factoryBean
=
beanFactory
.
getBean
(
BeanFactory
.
FACTORY_BEAN_PREFIX
+
beanName
);
if
(
factoryBean
instanceof
ScopedProxyFactoryBean
)
{
//
p
ass through - scoped proxy factory beans are a special case and should not
//
P
ass through - scoped proxy factory beans are a special case and should not
// be further proxied
}
else
{
//
i
t is a candidate FactoryBean - go ahead with enhancement
//
I
t is a candidate FactoryBean - go ahead with enhancement
return
enhanceFactoryBean
(
factoryBean
.
getClass
(),
beanFactory
,
beanName
);
}
}
if
(
isCurrentlyInvokedFactoryMethod
(
beanMethod
)
&&
!
beanFactory
.
containsSingleton
(
beanName
))
{
//
t
he factory is calling the bean method in order to instantiate and register the bean
//
T
he factory is calling the bean method in order to instantiate and register the bean
// (i.e. via a getBean() call) -> invoke the super implementation of the method to actually
// create the bean instance.
if
(
BeanFactoryPostProcessor
.
class
.
isAssignableFrom
(
beanMethod
.
getReturnType
()))
{
...
...
@@ -333,7 +326,7 @@ class ConfigurationClassEnhancer {
return
cglibMethodProxy
.
invokeSuper
(
enhancedConfigInstance
,
beanMethodArgs
);
}
else
{
//
t
he user (i.e. not the factory) is requesting this bean through a
//
T
he user (i.e. not the factory) is requesting this bean through a
// call to the bean method, direct or indirect. The bean may have already been
// marked as 'in creation' in certain autowiring scenarios; if so, temporarily
// set the in-creation status to false in order to avoid an exception.
...
...
@@ -390,6 +383,7 @@ class ConfigurationClassEnhancer {
*/
private
Object
enhanceFactoryBean
(
Class
<?>
fbClass
,
final
ConfigurableBeanFactory
beanFactory
,
final
String
beanName
)
throws
InstantiationException
,
IllegalAccessException
{
Enhancer
enhancer
=
new
Enhancer
();
enhancer
.
setSuperclass
(
fbClass
);
enhancer
.
setUseFactory
(
false
);
...
...
@@ -413,6 +407,11 @@ class ConfigurationClassEnhancer {
Assert
.
state
(
beanFactory
instanceof
ConfigurableBeanFactory
,
"Injected BeanFactory is not a ConfigurableBeanFactory"
);
return
(
ConfigurableBeanFactory
)
beanFactory
;
}
@Override
public
boolean
isMatch
(
Method
candidateMethod
)
{
return
BeanAnnotationHelper
.
isBeanAnnotated
(
candidateMethod
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录