Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
46cbdff5
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 搜索 >>
提交
46cbdff5
编写于
2月 14, 2018
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Consistent ControllerAdvice applicability against user-declared class
Issue: SPR-16496
上级
766e6028
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
63 addition
and
23 deletion
+63
-23
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java
.../method/annotation/ExceptionHandlerExceptionResolver.java
+19
-6
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolverTests.java
...od/annotation/ExceptionHandlerExceptionResolverTests.java
+44
-17
未找到文件。
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java
浏览文件 @
46cbdff5
/*
/*
* 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");
* 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.
...
@@ -17,6 +17,7 @@
...
@@ -17,6 +17,7 @@
package
org.springframework.web.servlet.mvc.method.annotation
;
package
org.springframework.web.servlet.mvc.method.annotation
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Proxy
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.Collections
;
import
java.util.LinkedHashMap
;
import
java.util.LinkedHashMap
;
...
@@ -27,6 +28,7 @@ import java.util.concurrent.ConcurrentHashMap;
...
@@ -27,6 +28,7 @@ import java.util.concurrent.ConcurrentHashMap;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpServletResponse
;
import
org.springframework.aop.support.AopUtils
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContextAware
;
import
org.springframework.context.ApplicationContextAware
;
...
@@ -440,13 +442,18 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
...
@@ -440,13 +442,18 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
* Spring-managed beans were detected.
* Spring-managed beans were detected.
* @param handlerMethod the method where the exception was raised (may be {@code null})
* @param handlerMethod the method where the exception was raised (may be {@code null})
* @param exception the raised exception
* @param exception the raised exception
* @return a method to handle the exception, or {@code null}
* @return a method to handle the exception, or {@code null}
if none
*/
*/
@Nullable
@Nullable
protected
ServletInvocableHandlerMethod
getExceptionHandlerMethod
(
@Nullable
HandlerMethod
handlerMethod
,
Exception
exception
)
{
protected
ServletInvocableHandlerMethod
getExceptionHandlerMethod
(
Class
<?>
handlerType
=
(
handlerMethod
!=
null
?
handlerMethod
.
getBeanType
()
:
null
);
@Nullable
HandlerMethod
handlerMethod
,
Exception
exception
)
{
Class
<?>
handlerType
=
null
;
if
(
handlerMethod
!=
null
)
{
if
(
handlerMethod
!=
null
)
{
// Local exception handler methods on the controller class itself.
// To be invoked through the proxy, even in case of an interface-based proxy.
handlerType
=
handlerMethod
.
getBeanType
();
ExceptionHandlerMethodResolver
resolver
=
this
.
exceptionHandlerCache
.
get
(
handlerType
);
ExceptionHandlerMethodResolver
resolver
=
this
.
exceptionHandlerCache
.
get
(
handlerType
);
if
(
resolver
==
null
)
{
if
(
resolver
==
null
)
{
resolver
=
new
ExceptionHandlerMethodResolver
(
handlerType
);
resolver
=
new
ExceptionHandlerMethodResolver
(
handlerType
);
...
@@ -456,14 +463,20 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
...
@@ -456,14 +463,20 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
if
(
method
!=
null
)
{
if
(
method
!=
null
)
{
return
new
ServletInvocableHandlerMethod
(
handlerMethod
.
getBean
(),
method
);
return
new
ServletInvocableHandlerMethod
(
handlerMethod
.
getBean
(),
method
);
}
}
// For advice applicability check below (involving base packages, assignable types
// and annotation presence), use target class instead of interface-based proxy.
if
(
Proxy
.
isProxyClass
(
handlerType
))
{
handlerType
=
AopUtils
.
getTargetClass
(
handlerMethod
.
getBean
());
}
}
}
for
(
Entry
<
ControllerAdviceBean
,
ExceptionHandlerMethodResolver
>
entry
:
this
.
exceptionHandlerAdviceCache
.
entrySet
())
{
for
(
Entry
<
ControllerAdviceBean
,
ExceptionHandlerMethodResolver
>
entry
:
this
.
exceptionHandlerAdviceCache
.
entrySet
())
{
if
(
entry
.
getKey
().
isApplicableToBeanType
(
handlerType
))
{
ControllerAdviceBean
advice
=
entry
.
getKey
();
if
(
advice
.
isApplicableToBeanType
(
handlerType
))
{
ExceptionHandlerMethodResolver
resolver
=
entry
.
getValue
();
ExceptionHandlerMethodResolver
resolver
=
entry
.
getValue
();
Method
method
=
resolver
.
resolveMethod
(
exception
);
Method
method
=
resolver
.
resolveMethod
(
exception
);
if
(
method
!=
null
)
{
if
(
method
!=
null
)
{
return
new
ServletInvocableHandlerMethod
(
entry
.
getKey
()
.
resolveBean
(),
method
);
return
new
ServletInvocableHandlerMethod
(
advice
.
resolveBean
(),
method
);
}
}
}
}
}
}
...
...
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolverTests.java
浏览文件 @
46cbdff5
/*
/*
* Copyright 2002-201
6
the original author or authors.
* Copyright 2002-201
8
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.
...
@@ -25,6 +25,7 @@ import org.junit.Before;
...
@@ -25,6 +25,7 @@ import org.junit.Before;
import
org.junit.BeforeClass
;
import
org.junit.BeforeClass
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.aop.framework.ProxyFactory
;
import
org.springframework.beans.FatalBeanException
;
import
org.springframework.beans.FatalBeanException
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
...
@@ -38,6 +39,7 @@ import org.springframework.util.ClassUtils;
...
@@ -38,6 +39,7 @@ import org.springframework.util.ClassUtils;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
import
org.springframework.web.context.support.WebApplicationObjectSupport
;
import
org.springframework.web.method.HandlerMethod
;
import
org.springframework.web.method.HandlerMethod
;
import
org.springframework.web.method.annotation.ModelMethodProcessor
;
import
org.springframework.web.method.annotation.ModelMethodProcessor
;
import
org.springframework.web.method.support.HandlerMethodArgumentResolver
;
import
org.springframework.web.method.support.HandlerMethodArgumentResolver
;
...
@@ -213,8 +215,8 @@ public class ExceptionHandlerExceptionResolverTests {
...
@@ -213,8 +215,8 @@ public class ExceptionHandlerExceptionResolverTests {
@Test
@Test
public
void
resolveExceptionGlobalHandler
()
throws
Exception
{
public
void
resolveExceptionGlobalHandler
()
throws
Exception
{
AnnotationConfigApplicationContext
c
xt
=
new
AnnotationConfigApplicationContext
(
MyConfig
.
class
);
AnnotationConfigApplicationContext
c
tx
=
new
AnnotationConfigApplicationContext
(
MyConfig
.
class
);
this
.
resolver
.
setApplicationContext
(
c
xt
);
this
.
resolver
.
setApplicationContext
(
c
tx
);
this
.
resolver
.
afterPropertiesSet
();
this
.
resolver
.
afterPropertiesSet
();
IllegalAccessException
ex
=
new
IllegalAccessException
();
IllegalAccessException
ex
=
new
IllegalAccessException
();
...
@@ -228,8 +230,8 @@ public class ExceptionHandlerExceptionResolverTests {
...
@@ -228,8 +230,8 @@ public class ExceptionHandlerExceptionResolverTests {
@Test
@Test
public
void
resolveExceptionGlobalHandlerOrdered
()
throws
Exception
{
public
void
resolveExceptionGlobalHandlerOrdered
()
throws
Exception
{
AnnotationConfigApplicationContext
c
xt
=
new
AnnotationConfigApplicationContext
(
MyConfig
.
class
);
AnnotationConfigApplicationContext
c
tx
=
new
AnnotationConfigApplicationContext
(
MyConfig
.
class
);
this
.
resolver
.
setApplicationContext
(
c
xt
);
this
.
resolver
.
setApplicationContext
(
c
tx
);
this
.
resolver
.
afterPropertiesSet
();
this
.
resolver
.
afterPropertiesSet
();
IllegalStateException
ex
=
new
IllegalStateException
();
IllegalStateException
ex
=
new
IllegalStateException
();
...
@@ -243,8 +245,8 @@ public class ExceptionHandlerExceptionResolverTests {
...
@@ -243,8 +245,8 @@ public class ExceptionHandlerExceptionResolverTests {
@Test
// SPR-12605
@Test
// SPR-12605
public
void
resolveExceptionWithHandlerMethodArg
()
throws
Exception
{
public
void
resolveExceptionWithHandlerMethodArg
()
throws
Exception
{
AnnotationConfigApplicationContext
c
xt
=
new
AnnotationConfigApplicationContext
(
MyConfig
.
class
);
AnnotationConfigApplicationContext
c
tx
=
new
AnnotationConfigApplicationContext
(
MyConfig
.
class
);
this
.
resolver
.
setApplicationContext
(
c
xt
);
this
.
resolver
.
setApplicationContext
(
c
tx
);
this
.
resolver
.
afterPropertiesSet
();
this
.
resolver
.
afterPropertiesSet
();
ArrayIndexOutOfBoundsException
ex
=
new
ArrayIndexOutOfBoundsException
();
ArrayIndexOutOfBoundsException
ex
=
new
ArrayIndexOutOfBoundsException
();
...
@@ -258,8 +260,8 @@ public class ExceptionHandlerExceptionResolverTests {
...
@@ -258,8 +260,8 @@ public class ExceptionHandlerExceptionResolverTests {
@Test
@Test
public
void
resolveExceptionWithAssertionError
()
throws
Exception
{
public
void
resolveExceptionWithAssertionError
()
throws
Exception
{
AnnotationConfigApplicationContext
c
xt
=
new
AnnotationConfigApplicationContext
(
MyConfig
.
class
);
AnnotationConfigApplicationContext
c
tx
=
new
AnnotationConfigApplicationContext
(
MyConfig
.
class
);
this
.
resolver
.
setApplicationContext
(
c
xt
);
this
.
resolver
.
setApplicationContext
(
c
tx
);
this
.
resolver
.
afterPropertiesSet
();
this
.
resolver
.
afterPropertiesSet
();
AssertionError
err
=
new
AssertionError
(
"argh"
);
AssertionError
err
=
new
AssertionError
(
"argh"
);
...
@@ -274,8 +276,8 @@ public class ExceptionHandlerExceptionResolverTests {
...
@@ -274,8 +276,8 @@ public class ExceptionHandlerExceptionResolverTests {
@Test
@Test
public
void
resolveExceptionWithAssertionErrorAsRootCause
()
throws
Exception
{
public
void
resolveExceptionWithAssertionErrorAsRootCause
()
throws
Exception
{
AnnotationConfigApplicationContext
c
xt
=
new
AnnotationConfigApplicationContext
(
MyConfig
.
class
);
AnnotationConfigApplicationContext
c
tx
=
new
AnnotationConfigApplicationContext
(
MyConfig
.
class
);
this
.
resolver
.
setApplicationContext
(
c
xt
);
this
.
resolver
.
setApplicationContext
(
c
tx
);
this
.
resolver
.
afterPropertiesSet
();
this
.
resolver
.
afterPropertiesSet
();
AssertionError
err
=
new
AssertionError
(
"argh"
);
AssertionError
err
=
new
AssertionError
(
"argh"
);
...
@@ -290,8 +292,8 @@ public class ExceptionHandlerExceptionResolverTests {
...
@@ -290,8 +292,8 @@ public class ExceptionHandlerExceptionResolverTests {
@Test
@Test
public
void
resolveExceptionControllerAdviceHandler
()
throws
Exception
{
public
void
resolveExceptionControllerAdviceHandler
()
throws
Exception
{
AnnotationConfigApplicationContext
c
xt
=
new
AnnotationConfigApplicationContext
(
MyControllerAdviceConfig
.
class
);
AnnotationConfigApplicationContext
c
tx
=
new
AnnotationConfigApplicationContext
(
MyControllerAdviceConfig
.
class
);
this
.
resolver
.
setApplicationContext
(
c
xt
);
this
.
resolver
.
setApplicationContext
(
c
tx
);
this
.
resolver
.
afterPropertiesSet
();
this
.
resolver
.
afterPropertiesSet
();
IllegalStateException
ex
=
new
IllegalStateException
();
IllegalStateException
ex
=
new
IllegalStateException
();
...
@@ -305,8 +307,8 @@ public class ExceptionHandlerExceptionResolverTests {
...
@@ -305,8 +307,8 @@ public class ExceptionHandlerExceptionResolverTests {
@Test
@Test
public
void
resolveExceptionControllerAdviceNoHandler
()
throws
Exception
{
public
void
resolveExceptionControllerAdviceNoHandler
()
throws
Exception
{
AnnotationConfigApplicationContext
c
xt
=
new
AnnotationConfigApplicationContext
(
MyControllerAdviceConfig
.
class
);
AnnotationConfigApplicationContext
c
tx
=
new
AnnotationConfigApplicationContext
(
MyControllerAdviceConfig
.
class
);
this
.
resolver
.
setApplicationContext
(
c
xt
);
this
.
resolver
.
setApplicationContext
(
c
tx
);
this
.
resolver
.
afterPropertiesSet
();
this
.
resolver
.
afterPropertiesSet
();
IllegalStateException
ex
=
new
IllegalStateException
();
IllegalStateException
ex
=
new
IllegalStateException
();
...
@@ -317,6 +319,21 @@ public class ExceptionHandlerExceptionResolverTests {
...
@@ -317,6 +319,21 @@ public class ExceptionHandlerExceptionResolverTests {
assertEquals
(
"DefaultTestExceptionResolver: IllegalStateException"
,
this
.
response
.
getContentAsString
());
assertEquals
(
"DefaultTestExceptionResolver: IllegalStateException"
,
this
.
response
.
getContentAsString
());
}
}
@Test
// SPR-16496
public
void
resolveExceptionControllerAdviceAgainstProxy
()
throws
Exception
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
(
MyControllerAdviceConfig
.
class
);
this
.
resolver
.
setApplicationContext
(
ctx
);
this
.
resolver
.
afterPropertiesSet
();
IllegalStateException
ex
=
new
IllegalStateException
();
HandlerMethod
handlerMethod
=
new
HandlerMethod
(
new
ProxyFactory
(
new
ResponseBodyController
()).
getProxy
(),
"handle"
);
ModelAndView
mav
=
this
.
resolver
.
resolveException
(
this
.
request
,
this
.
response
,
handlerMethod
,
ex
);
assertNotNull
(
"Exception was not handled"
,
mav
);
assertTrue
(
mav
.
isEmpty
());
assertEquals
(
"BasePackageTestExceptionResolver: IllegalStateException"
,
this
.
response
.
getContentAsString
());
}
private
void
assertMethodProcessorCount
(
int
resolverCount
,
int
handlerCount
)
{
private
void
assertMethodProcessorCount
(
int
resolverCount
,
int
handlerCount
)
{
assertEquals
(
resolverCount
,
this
.
resolver
.
getArgumentResolvers
().
getResolvers
().
size
());
assertEquals
(
resolverCount
,
this
.
resolver
.
getArgumentResolvers
().
getResolvers
().
size
());
...
@@ -348,8 +365,18 @@ public class ExceptionHandlerExceptionResolverTests {
...
@@ -348,8 +365,18 @@ public class ExceptionHandlerExceptionResolverTests {
}
}
interface
ResponseBodyInterface
{
void
handle
();
@ExceptionHandler
@ResponseBody
String
handleException
(
IllegalArgumentException
ex
);
}
@Controller
@Controller
static
class
ResponseBodyController
{
static
class
ResponseBodyController
extends
WebApplicationObjectSupport
implements
ResponseBodyInterface
{
public
void
handle
()
{}
public
void
handle
()
{}
...
@@ -454,7 +481,7 @@ public class ExceptionHandlerExceptionResolverTests {
...
@@ -454,7 +481,7 @@ public class ExceptionHandlerExceptionResolverTests {
}
}
@RestControllerAdvice
(
"org.springframework.web.servlet.mvc.method.annotation"
)
@RestControllerAdvice
(
assignableTypes
=
WebApplicationObjectSupport
.
class
)
@Order
(
2
)
@Order
(
2
)
static
class
BasePackageTestExceptionResolver
{
static
class
BasePackageTestExceptionResolver
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录