Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
bcf6f232
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,发现更多精彩内容 >>
提交
bcf6f232
编写于
3月 27, 2009
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
declarative destroy-method="..." specifications get validated at bean creation time (SPR-5602)
上级
d14cc0d7
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
98 addition
and
81 deletion
+98
-81
org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java
...s/factory/support/AbstractAutowireCapableBeanFactory.java
+8
-3
org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java
...ramework/beans/factory/support/DisposableBeanAdapter.java
+68
-73
org.springframework.beans/src/test/java/org/springframework/beans/factory/xml/FactoryMethodTests.java
...springframework/beans/factory/xml/FactoryMethodTests.java
+19
-5
org.springframework.beans/src/test/java/org/springframework/beans/factory/xml/factory-methods.xml
...org/springframework/beans/factory/xml/factory-methods.xml
+3
-0
未找到文件。
org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java
浏览文件 @
bcf6f232
...
@@ -512,7 +512,12 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
...
@@ -512,7 +512,12 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
}
}
// Register bean as disposable.
// Register bean as disposable.
try
{
registerDisposableBeanIfNecessary
(
beanName
,
bean
,
mbd
);
registerDisposableBeanIfNecessary
(
beanName
,
bean
,
mbd
);
}
catch
(
BeanDefinitionValidationException
ex
)
{
throw
new
BeanCreationException
(
mbd
.
getResourceDescription
(),
beanName
,
"Invalid destruction signature"
,
ex
);
}
return
exposedObject
;
return
exposedObject
;
}
}
...
@@ -1387,8 +1392,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
...
@@ -1387,8 +1392,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
Method
initMethod
=
BeanUtils
.
findMethod
(
bean
.
getClass
(),
initMethodName
,
null
);
Method
initMethod
=
BeanUtils
.
findMethod
(
bean
.
getClass
(),
initMethodName
,
null
);
if
(
initMethod
==
null
)
{
if
(
initMethod
==
null
)
{
if
(
enforceInitMethod
)
{
if
(
enforceInitMethod
)
{
throw
new
NoSuchMethodException
(
"Couldn't find an init method named '"
+
initMethodName
+
throw
new
BeanDefinitionValidationException
(
"Couldn't find an init method named '"
+
"' on bean with name '"
+
beanName
+
"'"
);
initMethodName
+
"' on bean with name '"
+
beanName
+
"'"
);
}
}
else
{
else
{
if
(
logger
.
isDebugEnabled
())
{
if
(
logger
.
isDebugEnabled
())
{
...
...
org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DisposableBeanAdapter.java
浏览文件 @
bcf6f232
/*
/*
* Copyright 2002-200
8
the original author or authors.
* Copyright 2002-200
9
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -58,9 +58,9 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
...
@@ -58,9 +58,9 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
private
final
boolean
invokeDisposableBean
;
private
final
boolean
invokeDisposableBean
;
private
final
String
destroyMethodName
;
private
String
destroyMethodName
;
private
final
boolean
enforceD
estroyMethod
;
private
transient
Method
d
estroyMethod
;
private
List
<
DestructionAwareBeanPostProcessor
>
beanPostProcessors
;
private
List
<
DestructionAwareBeanPostProcessor
>
beanPostProcessors
;
...
@@ -79,11 +79,37 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
...
@@ -79,11 +79,37 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
Assert
.
notNull
(
bean
,
"Bean must not be null"
);
Assert
.
notNull
(
bean
,
"Bean must not be null"
);
this
.
bean
=
bean
;
this
.
bean
=
bean
;
this
.
beanName
=
beanName
;
this
.
beanName
=
beanName
;
this
.
invokeDisposableBean
=
!
beanDefinition
.
isExternallyManagedDestroyMethod
(
"destroy"
);
this
.
invokeDisposableBean
=
this
.
destroyMethodName
=
(
this
.
bean
instanceof
DisposableBean
&&
!
beanDefinition
.
isExternallyManagedDestroyMethod
(
"destroy"
));
(!
beanDefinition
.
isExternallyManagedDestroyMethod
(
beanDefinition
.
getDestroyMethodName
())
?
String
destroyMethodName
=
beanDefinition
.
getDestroyMethodName
();
beanDefinition
.
getDestroyMethodName
()
:
null
);
if
(
destroyMethodName
!=
null
&&
!(
this
.
invokeDisposableBean
&&
"destroy"
.
equals
(
destroyMethodName
))
&&
this
.
enforceDestroyMethod
=
beanDefinition
.
isEnforceDestroyMethod
();
!
beanDefinition
.
isExternallyManagedDestroyMethod
(
destroyMethodName
))
{
this
.
destroyMethodName
=
destroyMethodName
;
try
{
this
.
destroyMethod
=
BeanUtils
.
findMethodWithMinimalParameters
(
bean
.
getClass
(),
destroyMethodName
);
}
catch
(
IllegalArgumentException
ex
)
{
throw
new
BeanDefinitionValidationException
(
"Couldn't find a unique destroy method on bean with name '"
+
this
.
beanName
+
": "
+
ex
.
getMessage
());
}
if
(
this
.
destroyMethod
==
null
)
{
if
(
beanDefinition
.
isEnforceDestroyMethod
())
{
throw
new
BeanDefinitionValidationException
(
"Couldn't find a destroy method named '"
+
destroyMethodName
+
"' on bean with name '"
+
beanName
+
"'"
);
}
}
else
{
Class
[]
paramTypes
=
this
.
destroyMethod
.
getParameterTypes
();
if
(
paramTypes
.
length
>
1
)
{
throw
new
BeanDefinitionValidationException
(
"Method '"
+
destroyMethodName
+
"' of bean '"
+
beanName
+
"' has more than one parameter - not supported as destroy method"
);
}
else
if
(
paramTypes
.
length
==
1
&&
!
paramTypes
[
0
].
equals
(
boolean
.
class
))
{
throw
new
BeanDefinitionValidationException
(
"Method '"
+
destroyMethodName
+
"' of bean '"
+
beanName
+
"' has a non-boolean parameter - not supported as destroy method"
);
}
}
}
this
.
beanPostProcessors
=
filterPostProcessors
(
postProcessors
);
this
.
beanPostProcessors
=
filterPostProcessors
(
postProcessors
);
}
}
...
@@ -91,23 +117,17 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
...
@@ -91,23 +117,17 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
* Create a new DisposableBeanAdapter for the given bean.
* Create a new DisposableBeanAdapter for the given bean.
* @param bean the bean instance (never <code>null</code>)
* @param bean the bean instance (never <code>null</code>)
* @param beanName the name of the bean
* @param beanName the name of the bean
* @param invokeDisposableBean whether to actually invoke
* @param invokeDisposableBean whether to actually invoke DisposableBean's destroy method here
* DisposableBean's destroy method here
* @param destroyMethodName the name of the custom destroy method (<code>null</code> if there is none)
* @param destroyMethodName the name of the custom destroy method
* (<code>null</code> if there is none)
* @param enforceDestroyMethod whether to the specified custom
* destroy method (if any) has to be present on the bean object
* @param postProcessors the List of DestructionAwareBeanPostProcessors, if any
* @param postProcessors the List of DestructionAwareBeanPostProcessors, if any
*/
*/
private
DisposableBeanAdapter
(
Object
bean
,
String
beanName
,
boolean
invokeDisposableBean
,
private
DisposableBeanAdapter
(
Object
bean
,
String
beanName
,
boolean
invokeDisposableBean
,
String
destroyMethodName
,
boolean
enforceDestroyMethod
,
String
destroyMethodName
,
List
<
DestructionAwareBeanPostProcessor
>
postProcessors
)
{
List
<
DestructionAwareBeanPostProcessor
>
postProcessors
)
{
this
.
bean
=
bean
;
this
.
bean
=
bean
;
this
.
beanName
=
beanName
;
this
.
beanName
=
beanName
;
this
.
invokeDisposableBean
=
invokeDisposableBean
;
this
.
invokeDisposableBean
=
invokeDisposableBean
;
this
.
destroyMethodName
=
destroyMethodName
;
this
.
destroyMethodName
=
destroyMethodName
;
this
.
enforceDestroyMethod
=
enforceDestroyMethod
;
this
.
beanPostProcessors
=
postProcessors
;
this
.
beanPostProcessors
=
postProcessors
;
}
}
...
@@ -142,8 +162,7 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
...
@@ -142,8 +162,7 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
}
}
}
}
boolean
isDisposableBean
=
(
this
.
bean
instanceof
DisposableBean
);
if
(
this
.
invokeDisposableBean
)
{
if
(
isDisposableBean
&&
this
.
invokeDisposableBean
)
{
if
(
logger
.
isDebugEnabled
())
{
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
"Invoking destroy() on bean with name '"
+
this
.
beanName
+
"'"
);
logger
.
debug
(
"Invoking destroy() on bean with name '"
+
this
.
beanName
+
"'"
);
}
}
...
@@ -161,8 +180,13 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
...
@@ -161,8 +180,13 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
}
}
}
}
if
(
this
.
destroyMethodName
!=
null
&&
!(
isDisposableBean
&&
"destroy"
.
equals
(
this
.
destroyMethodName
)))
{
if
(
this
.
destroyMethod
!=
null
)
{
invokeCustomDestroyMethod
();
invokeCustomDestroyMethod
(
this
.
destroyMethod
);
}
else
if
(
this
.
destroyMethodName
!=
null
)
{
Method
destroyMethod
=
BeanUtils
.
findMethodWithMinimalParameters
(
this
.
bean
.
getClass
(),
this
.
destroyMethodName
);
invokeCustomDestroyMethod
(
destroyMethod
);
}
}
}
}
...
@@ -172,29 +196,8 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
...
@@ -172,29 +196,8 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
* for a method with a single boolean argument (passing in "true",
* for a method with a single boolean argument (passing in "true",
* assuming a "force" parameter), else logging an error.
* assuming a "force" parameter), else logging an error.
*/
*/
private
void
invokeCustomDestroyMethod
()
{
private
void
invokeCustomDestroyMethod
(
Method
destroyMethod
)
{
try
{
Method
destroyMethod
=
BeanUtils
.
findMethodWithMinimalParameters
(
this
.
bean
.
getClass
(),
this
.
destroyMethodName
);
if
(
destroyMethod
==
null
)
{
if
(
this
.
enforceDestroyMethod
)
{
logger
.
warn
(
"Couldn't find a destroy method named '"
+
this
.
destroyMethodName
+
"' on bean with name '"
+
this
.
beanName
+
"'"
);
}
}
else
{
Class
[]
paramTypes
=
destroyMethod
.
getParameterTypes
();
Class
[]
paramTypes
=
destroyMethod
.
getParameterTypes
();
if
(
paramTypes
.
length
>
1
)
{
logger
.
error
(
"Method '"
+
this
.
destroyMethodName
+
"' of bean '"
+
this
.
beanName
+
"' has more than one parameter - not supported as destroy method"
);
}
else
if
(
paramTypes
.
length
==
1
&&
!
paramTypes
[
0
].
equals
(
boolean
.
class
))
{
logger
.
error
(
"Method '"
+
this
.
destroyMethodName
+
"' of bean '"
+
this
.
beanName
+
"' has a non-boolean parameter - not supported as destroy method"
);
}
else
{
Object
[]
args
=
new
Object
[
paramTypes
.
length
];
Object
[]
args
=
new
Object
[
paramTypes
.
length
];
if
(
paramTypes
.
length
==
1
)
{
if
(
paramTypes
.
length
==
1
)
{
args
[
0
]
=
Boolean
.
TRUE
;
args
[
0
]
=
Boolean
.
TRUE
;
...
@@ -222,14 +225,6 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
...
@@ -222,14 +225,6 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
"' on bean with name '"
+
this
.
beanName
+
"'"
,
ex
);
"' on bean with name '"
+
this
.
beanName
+
"'"
,
ex
);
}
}
}
}
}
}
catch
(
IllegalArgumentException
ex
)
{
// thrown from findMethodWithMinimalParameters
logger
.
error
(
"Couldn't find a unique destroy method on bean with name '"
+
this
.
beanName
+
": "
+
ex
.
getMessage
());
}
}
/**
/**
...
@@ -247,7 +242,7 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
...
@@ -247,7 +242,7 @@ class DisposableBeanAdapter implements DisposableBean, Runnable, Serializable {
}
}
}
}
return
new
DisposableBeanAdapter
(
this
.
bean
,
this
.
beanName
,
this
.
invokeDisposableBean
,
return
new
DisposableBeanAdapter
(
this
.
bean
,
this
.
beanName
,
this
.
invokeDisposableBean
,
this
.
destroyMethodName
,
this
.
enforceDestroyMethod
,
serializablePostProcessors
);
this
.
destroyMethodName
,
serializablePostProcessors
);
}
}
}
}
org.springframework.beans/src/test/java/org/springframework/beans/factory/xml/FactoryMethodTests.java
浏览文件 @
bcf6f232
/*
/*
* Copyright 2002-200
8
the original author or authors.
* Copyright 2002-200
9
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -16,21 +16,20 @@
...
@@ -16,21 +16,20 @@
package
org.springframework.beans.factory.xml
;
package
org.springframework.beans.factory.xml
;
import
static
org
.
junit
.
Assert
.*;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Properties
;
import
java.util.Properties
;
import
static
org
.
junit
.
Assert
.*;
import
org.junit.Test
;
import
org.junit.Test
;
import
test.beans.TestBean
;
import
org.springframework.beans.factory.BeanCreationException
;
import
org.springframework.beans.factory.BeanCreationException
;
import
org.springframework.beans.factory.BeanDefinitionStoreException
;
import
org.springframework.beans.factory.BeanDefinitionStoreException
;
import
org.springframework.beans.factory.support.DefaultListableBeanFactory
;
import
org.springframework.beans.factory.support.DefaultListableBeanFactory
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.core.io.ClassPathResource
;
import
test.beans.TestBean
;
/**
/**
* @author Juergen Hoeller
* @author Juergen Hoeller
* @author Chris Beams
* @author Chris Beams
...
@@ -71,6 +70,21 @@ public class FactoryMethodTests {
...
@@ -71,6 +70,21 @@ public class FactoryMethodTests {
assertTrue
(
tb
.
wasDestroyed
());
assertTrue
(
tb
.
wasDestroyed
());
}
}
@Test
public
void
testFactoryMethodsWithInvalidDestroyMethod
()
{
DefaultListableBeanFactory
xbf
=
new
DefaultListableBeanFactory
();
XmlBeanDefinitionReader
reader
=
new
XmlBeanDefinitionReader
(
xbf
);
reader
.
loadBeanDefinitions
(
new
ClassPathResource
(
"factory-methods.xml"
,
getClass
()));
try
{
xbf
.
getBean
(
"defaultTestBeanWithInvalidDestroyMethod"
);
fail
(
"Should have thrown BeanCreationException"
);
}
catch
(
BeanCreationException
ex
)
{
// expected
}
}
@Test
@Test
public
void
testFactoryMethodsWithNullInstance
()
{
public
void
testFactoryMethodsWithNullInstance
()
{
DefaultListableBeanFactory
xbf
=
new
DefaultListableBeanFactory
();
DefaultListableBeanFactory
xbf
=
new
DefaultListableBeanFactory
();
...
...
org.springframework.beans/src/test/java/org/springframework/beans/factory/xml/factory-methods.xml
浏览文件 @
bcf6f232
...
@@ -12,6 +12,9 @@
...
@@ -12,6 +12,9 @@
<bean
id=
"defaultTestBean"
factory-bean=
"default"
factory-method=
"getTestBean"
<bean
id=
"defaultTestBean"
factory-bean=
"default"
factory-method=
"getTestBean"
init-method=
"haveBirthday"
destroy-method=
"destroy"
/>
init-method=
"haveBirthday"
destroy-method=
"destroy"
/>
<bean
id=
"defaultTestBeanWithInvalidDestroyMethod"
factory-bean=
"default"
factory-method=
"getTestBean"
init-method=
"haveBirthday"
destroy-method=
"xxx"
lazy-init=
"true"
/>
<bean
id=
"defaultTestBean.protected"
factory-bean=
"default"
factory-method=
"protectedGetTestBean"
<bean
id=
"defaultTestBean.protected"
factory-bean=
"default"
factory-method=
"protectedGetTestBean"
init-method=
"haveBirthday"
destroy-method=
"destroy"
/>
init-method=
"haveBirthday"
destroy-method=
"destroy"
/>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录