Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
978adbda
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,发现更多精彩内容 >>
提交
978adbda
编写于
6月 23, 2019
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Avoid duplicate lookups of @ControllerAdvice annotations
See gh-23163
上级
ed6fbddd
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
33 addition
and
10 deletion
+33
-10
spring-web/src/main/java/org/springframework/web/method/ControllerAdviceBean.java
.../org/springframework/web/method/ControllerAdviceBean.java
+33
-10
未找到文件。
spring-web/src/main/java/org/springframework/web/method/ControllerAdviceBean.java
浏览文件 @
978adbda
...
@@ -85,12 +85,28 @@ public class ControllerAdviceBean implements Ordered {
...
@@ -85,12 +85,28 @@ public class ControllerAdviceBean implements Ordered {
}
}
/**
/**
* Create a {@code ControllerAdviceBean} using the given bean name.
* Create a {@code ControllerAdviceBean} using the given bean name and
* {@code BeanFactory}.
* @param beanName the name of the bean
* @param beanName the name of the bean
* @param beanFactory a {@code BeanFactory} to retrieve the bean type initially
* @param beanFactory a {@code BeanFactory} to retrieve the bean type initially
* and later to resolve the actual bean
* and later to resolve the actual bean
*/
*/
public
ControllerAdviceBean
(
String
beanName
,
BeanFactory
beanFactory
)
{
public
ControllerAdviceBean
(
String
beanName
,
BeanFactory
beanFactory
)
{
this
(
beanName
,
beanFactory
,
null
);
}
/**
* Create a {@code ControllerAdviceBean} using the given bean name,
* {@code BeanFactory}, and {@link ControllerAdvice @ControllerAdvice}
* annotation.
* @param beanName the name of the bean
* @param beanFactory a {@code BeanFactory} to retrieve the bean type initially
* and later to resolve the actual bean
* @param controllerAdvice the {@code @ControllerAdvice} annotation for the
* bean, or {@code null} if not yet retrieved
* @since 5.2
*/
public
ControllerAdviceBean
(
String
beanName
,
BeanFactory
beanFactory
,
@Nullable
ControllerAdvice
controllerAdvice
)
{
Assert
.
hasText
(
beanName
,
"Bean name must contain text"
);
Assert
.
hasText
(
beanName
,
"Bean name must contain text"
);
Assert
.
notNull
(
beanFactory
,
"BeanFactory must not be null"
);
Assert
.
notNull
(
beanFactory
,
"BeanFactory must not be null"
);
Assert
.
isTrue
(
beanFactory
.
containsBean
(
beanName
),
()
->
"BeanFactory ["
+
beanFactory
Assert
.
isTrue
(
beanFactory
.
containsBean
(
beanName
),
()
->
"BeanFactory ["
+
beanFactory
...
@@ -98,7 +114,8 @@ public class ControllerAdviceBean implements Ordered {
...
@@ -98,7 +114,8 @@ public class ControllerAdviceBean implements Ordered {
this
.
beanOrName
=
beanName
;
this
.
beanOrName
=
beanName
;
this
.
beanType
=
getBeanType
(
beanName
,
beanFactory
);
this
.
beanType
=
getBeanType
(
beanName
,
beanFactory
);
this
.
beanTypePredicate
=
createBeanTypePredicate
(
this
.
beanType
);
this
.
beanTypePredicate
=
(
controllerAdvice
!=
null
?
createBeanTypePredicate
(
controllerAdvice
)
:
createBeanTypePredicate
(
this
.
beanType
));
this
.
beanFactory
=
beanFactory
;
this
.
beanFactory
=
beanFactory
;
this
.
order
=
initOrderFromBeanType
(
this
.
beanType
);
this
.
order
=
initOrderFromBeanType
(
this
.
beanType
);
}
}
...
@@ -186,8 +203,11 @@ public class ControllerAdviceBean implements Ordered {
...
@@ -186,8 +203,11 @@ public class ControllerAdviceBean implements Ordered {
public
static
List
<
ControllerAdviceBean
>
findAnnotatedBeans
(
ApplicationContext
context
)
{
public
static
List
<
ControllerAdviceBean
>
findAnnotatedBeans
(
ApplicationContext
context
)
{
List
<
ControllerAdviceBean
>
adviceBeans
=
new
ArrayList
<>();
List
<
ControllerAdviceBean
>
adviceBeans
=
new
ArrayList
<>();
for
(
String
name
:
BeanFactoryUtils
.
beanNamesForTypeIncludingAncestors
(
context
,
Object
.
class
))
{
for
(
String
name
:
BeanFactoryUtils
.
beanNamesForTypeIncludingAncestors
(
context
,
Object
.
class
))
{
if
(
context
.
findAnnotationOnBean
(
name
,
ControllerAdvice
.
class
)
!=
null
)
{
ControllerAdvice
controllerAdvice
=
context
.
findAnnotationOnBean
(
name
,
ControllerAdvice
.
class
);
adviceBeans
.
add
(
new
ControllerAdviceBean
(
name
,
context
));
if
(
controllerAdvice
!=
null
)
{
// Use the @ControllerAdvice annotation found by findAnnotationOnBean()
// in order to avoid a subsequent lookup of the same annotation.
adviceBeans
.
add
(
new
ControllerAdviceBean
(
name
,
context
,
controllerAdvice
));
}
}
}
}
return
adviceBeans
;
return
adviceBeans
;
...
@@ -199,15 +219,18 @@ public class ControllerAdviceBean implements Ordered {
...
@@ -199,15 +219,18 @@ public class ControllerAdviceBean implements Ordered {
}
}
private
static
HandlerTypePredicate
createBeanTypePredicate
(
Class
<?>
beanType
)
{
private
static
HandlerTypePredicate
createBeanTypePredicate
(
Class
<?>
beanType
)
{
ControllerAdvice
annotation
=
(
beanType
!=
null
?
ControllerAdvice
controllerAdvice
=
(
beanType
!=
null
?
AnnotatedElementUtils
.
findMergedAnnotation
(
beanType
,
ControllerAdvice
.
class
)
:
null
);
AnnotatedElementUtils
.
findMergedAnnotation
(
beanType
,
ControllerAdvice
.
class
)
:
null
);
return
createBeanTypePredicate
(
controllerAdvice
);
}
if
(
annotation
!=
null
)
{
private
static
HandlerTypePredicate
createBeanTypePredicate
(
ControllerAdvice
controllerAdvice
)
{
if
(
controllerAdvice
!=
null
)
{
return
HandlerTypePredicate
.
builder
()
return
HandlerTypePredicate
.
builder
()
.
basePackage
(
annotation
.
basePackages
())
.
basePackage
(
controllerAdvice
.
basePackages
())
.
basePackageClass
(
annotation
.
basePackageClasses
())
.
basePackageClass
(
controllerAdvice
.
basePackageClasses
())
.
assignableType
(
annotation
.
assignableTypes
())
.
assignableType
(
controllerAdvice
.
assignableTypes
())
.
annotation
(
annotation
.
annotations
())
.
annotation
(
controllerAdvice
.
annotations
())
.
build
();
.
build
();
}
}
return
HandlerTypePredicate
.
forAnyHandlerType
();
return
HandlerTypePredicate
.
forAnyHandlerType
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录