Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
182243d2
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,发现更多精彩内容 >>
提交
182243d2
编写于
7月 04, 2018
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
BeanDefinitionOverrideException in case of overriding not allowed
Issue: SPR-16982
上级
63d62152
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
122 addition
and
28 deletion
+122
-28
spring-beans/src/main/java/org/springframework/beans/factory/BeanDefinitionStoreException.java
...framework/beans/factory/BeanDefinitionStoreException.java
+9
-7
spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionOverrideException.java
...eans/factory/support/BeanDefinitionOverrideException.java
+91
-0
spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionRegistry.java
...amework/beans/factory/support/BeanDefinitionRegistry.java
+4
-3
spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java
...ork/beans/factory/support/DefaultListableBeanFactory.java
+9
-13
spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java
...mework/beans/factory/DefaultListableBeanFactoryTests.java
+9
-5
未找到文件。
spring-beans/src/main/java/org/springframework/beans/factory/BeanDefinitionStoreException.java
浏览文件 @
182243d2
...
...
@@ -84,7 +84,7 @@ public class BeanDefinitionStoreException extends FatalBeanException {
/**
* Create a new BeanDefinitionStoreException.
* @param resourceDescription description of the resource that the bean definition came from
* @param beanName the name of the bean
requested
* @param beanName the name of the bean
* @param msg the detail message (appended to an introductory message that indicates
* the resource and the name of the bean)
*/
...
...
@@ -95,21 +95,23 @@ public class BeanDefinitionStoreException extends FatalBeanException {
/**
* Create a new BeanDefinitionStoreException.
* @param resourceDescription description of the resource that the bean definition came from
* @param beanName the name of the bean
requested
* @param beanName the name of the bean
* @param msg the detail message (appended to an introductory message that indicates
* the resource and the name of the bean)
* @param cause the root cause (may be {@code null})
*/
public
BeanDefinitionStoreException
(
@Nullable
String
resourceDescription
,
String
beanName
,
String
msg
,
@Nullable
Throwable
cause
)
{
super
(
"Invalid bean definition with name '"
+
beanName
+
"' defined in "
+
resourceDescription
+
": "
+
msg
,
cause
);
public
BeanDefinitionStoreException
(
@Nullable
String
resourceDescription
,
String
beanName
,
String
msg
,
@Nullable
Throwable
cause
)
{
super
(
"Invalid bean definition with name '"
+
beanName
+
"' defined in "
+
resourceDescription
+
": "
+
msg
,
cause
);
this
.
resourceDescription
=
resourceDescription
;
this
.
beanName
=
beanName
;
}
/**
* Return the description of the resource that the bean
* definition came from, if any.
* Return the description of the resource that the bean definition came from, if available.
*/
@Nullable
public
String
getResourceDescription
()
{
...
...
@@ -117,7 +119,7 @@ public class BeanDefinitionStoreException extends FatalBeanException {
}
/**
* Return the name of the bean
requested, if any
.
* Return the name of the bean
, if available
.
*/
@Nullable
public
String
getBeanName
()
{
...
...
spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionOverrideException.java
0 → 100644
浏览文件 @
182243d2
/*
* Copyright 2002-2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.springframework.beans.factory.support
;
import
org.springframework.beans.factory.BeanDefinitionStoreException
;
import
org.springframework.beans.factory.config.BeanDefinition
;
import
org.springframework.lang.NonNull
;
/**
* Subclass of {@link BeanDefinitionStoreException} indicating an invalid override
* attempt: typically registering a new definition for the same bean name while
* {@link DefaultListableBeanFactory#isAllowBeanDefinitionOverriding()} is {@code false}.
*
* @author Juergen Hoeller
* @since 5.1
* @see DefaultListableBeanFactory#setAllowBeanDefinitionOverriding
* @see DefaultListableBeanFactory#registerBeanDefinition
*/
public
class
BeanDefinitionOverrideException
extends
BeanDefinitionStoreException
{
private
final
BeanDefinition
beanDefinition
;
private
final
BeanDefinition
existingDefinition
;
/**
* Create a new BeanDefinitionOverrideException for the given new and existing definition.
* @param beanName the name of the bean
* @param beanDefinition the newly registered bean definition
* @param existingDefinition the existing bean definition for the same name
*/
public
BeanDefinitionOverrideException
(
String
beanName
,
BeanDefinition
beanDefinition
,
BeanDefinition
existingDefinition
)
{
super
(
beanDefinition
.
getResourceDescription
(),
beanName
,
"Cannot register bean definition ["
+
beanDefinition
+
"] for bean '"
+
beanName
+
"': There is already ["
+
existingDefinition
+
"] bound."
);
this
.
beanDefinition
=
beanDefinition
;
this
.
existingDefinition
=
existingDefinition
;
}
/**
* Return the description of the resource that the bean definition came from.
*/
@Override
@NonNull
public
String
getResourceDescription
()
{
return
String
.
valueOf
(
super
.
getResourceDescription
());
}
/**
* Return the name of the bean.
*/
@Override
@NonNull
public
String
getBeanName
()
{
return
String
.
valueOf
(
super
.
getBeanName
());
}
/**
* Return the newly registered bean definition.
* @see #getBeanName()
*/
public
BeanDefinition
getBeanDefinition
()
{
return
this
.
beanDefinition
;
}
/**
* Return the existing bean definition for the same name.
* @see #getBeanName()
*/
public
BeanDefinition
getExistingDefinition
()
{
return
this
.
existingDefinition
;
}
}
spring-beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionRegistry.java
浏览文件 @
182243d2
/*
* Copyright 2002-201
7
the original author or authors.
* Copyright 2002-201
8
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.
...
...
@@ -53,8 +53,9 @@ public interface BeanDefinitionRegistry extends AliasRegistry {
* @param beanName the name of the bean instance to register
* @param beanDefinition definition of the bean instance to register
* @throws BeanDefinitionStoreException if the BeanDefinition is invalid
* or if there is already a BeanDefinition for the specified bean name
* (and we are not allowed to override it)
* @throws BeanDefinitionOverrideException if there is already a BeanDefinition
* for the specified bean name and we are not allowed to override it
* @see GenericBeanDefinition
* @see RootBeanDefinition
* @see ChildBeanDefinition
*/
...
...
spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java
浏览文件 @
182243d2
...
...
@@ -802,34 +802,30 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
}
}
BeanDefinition
oldBeanDefinition
;
oldBeanDefinition
=
this
.
beanDefinitionMap
.
get
(
beanName
);
if
(
oldBeanDefinition
!=
null
)
{
BeanDefinition
existingDefinition
=
this
.
beanDefinitionMap
.
get
(
beanName
);
if
(
existingDefinition
!=
null
)
{
if
(!
isAllowBeanDefinitionOverriding
())
{
throw
new
BeanDefinitionStoreException
(
beanDefinition
.
getResourceDescription
(),
beanName
,
"Cannot register bean definition ["
+
beanDefinition
+
"] for bean '"
+
beanName
+
"': There is already ["
+
oldBeanDefinition
+
"] bound."
);
throw
new
BeanDefinitionOverrideException
(
beanName
,
beanDefinition
,
existingDefinition
);
}
else
if
(
oldBean
Definition
.
getRole
()
<
beanDefinition
.
getRole
())
{
else
if
(
existing
Definition
.
getRole
()
<
beanDefinition
.
getRole
())
{
// e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE
if
(
logger
.
isWarnEnabled
())
{
logger
.
warn
(
"Overriding user-defined bean definition for bean '"
+
beanName
+
"' with a framework-generated bean definition: replacing ["
+
oldBean
Definition
+
"] with ["
+
beanDefinition
+
"]"
);
existing
Definition
+
"] with ["
+
beanDefinition
+
"]"
);
}
}
else
if
(!
beanDefinition
.
equals
(
oldBean
Definition
))
{
else
if
(!
beanDefinition
.
equals
(
existing
Definition
))
{
if
(
logger
.
isInfoEnabled
())
{
logger
.
info
(
"Overriding bean definition for bean '"
+
beanName
+
"' with a different definition: replacing ["
+
oldBean
Definition
+
"' with a different definition: replacing ["
+
existing
Definition
+
"] with ["
+
beanDefinition
+
"]"
);
}
}
else
{
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
"Overriding bean definition for bean '"
+
beanName
+
"' with an equivalent definition: replacing ["
+
oldBean
Definition
+
"' with an equivalent definition: replacing ["
+
existing
Definition
+
"] with ["
+
beanDefinition
+
"]"
);
}
}
...
...
@@ -860,7 +856,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
this
.
frozenBeanDefinitionNames
=
null
;
}
if
(
oldBean
Definition
!=
null
||
containsSingleton
(
beanName
))
{
if
(
existing
Definition
!=
null
||
containsSingleton
(
beanName
))
{
resetBeanDefinition
(
beanName
);
}
}
...
...
spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java
浏览文件 @
182243d2
...
...
@@ -68,6 +68,7 @@ import org.springframework.beans.factory.config.TypedStringValue;
import
org.springframework.beans.factory.support.AbstractBeanDefinition
;
import
org.springframework.beans.factory.support.AbstractBeanFactory
;
import
org.springframework.beans.factory.support.BeanDefinitionBuilder
;
import
org.springframework.beans.factory.support.BeanDefinitionOverrideException
;
import
org.springframework.beans.factory.support.ChildBeanDefinition
;
import
org.springframework.beans.factory.support.DefaultListableBeanFactory
;
import
org.springframework.beans.factory.support.ManagedList
;
...
...
@@ -841,14 +842,17 @@ public class DefaultListableBeanFactoryTests {
public
void
testBeanDefinitionOverridingNotAllowed
()
{
DefaultListableBeanFactory
lbf
=
new
DefaultListableBeanFactory
();
lbf
.
setAllowBeanDefinitionOverriding
(
false
);
lbf
.
registerBeanDefinition
(
"test"
,
new
RootBeanDefinition
(
TestBean
.
class
));
BeanDefinition
oldDef
=
new
RootBeanDefinition
(
TestBean
.
class
);
BeanDefinition
newDef
=
new
RootBeanDefinition
(
NestedTestBean
.
class
);
lbf
.
registerBeanDefinition
(
"test"
,
oldDef
);
try
{
lbf
.
registerBeanDefinition
(
"test"
,
new
RootBeanDefinition
(
NestedTestBean
.
class
)
);
fail
(
"Should have thrown BeanDefinition
Stor
eException"
);
lbf
.
registerBeanDefinition
(
"test"
,
new
Def
);
fail
(
"Should have thrown BeanDefinition
Overrid
eException"
);
}
catch
(
BeanDefinition
Stor
eException
ex
)
{
catch
(
BeanDefinition
Overrid
eException
ex
)
{
assertEquals
(
"test"
,
ex
.
getBeanName
());
// expected
assertSame
(
newDef
,
ex
.
getBeanDefinition
());
assertSame
(
oldDef
,
ex
.
getExistingDefinition
());
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录