use of org.springframework.web.method.annotation.ExceptionHandlerMethodResolver in project spring-mvc-31-demo by rstoyanchev.
the class ExtendedExceptionHandlerExceptionResolver method setExceptionHandler.
/**
* Provide a handler with @{@link ExceptionHandler} methods.
*/
public void setExceptionHandler(Object handler) {
this.handler = handler;
this.methodResolver = new ExceptionHandlerMethodResolver(handler.getClass());
}
use of org.springframework.web.method.annotation.ExceptionHandlerMethodResolver in project spring-framework by spring-projects.
the class ControllerMethodResolver method initControllerAdviceCaches.
private void initControllerAdviceCaches(ApplicationContext applicationContext) {
List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(applicationContext);
for (ControllerAdviceBean bean : beans) {
Class<?> beanType = bean.getBeanType();
if (beanType != null) {
Set<Method> attrMethods = MethodIntrospector.selectMethods(beanType, MODEL_ATTRIBUTE_METHODS);
if (!attrMethods.isEmpty()) {
this.modelAttributeAdviceCache.put(bean, attrMethods);
}
Set<Method> binderMethods = MethodIntrospector.selectMethods(beanType, INIT_BINDER_METHODS);
if (!binderMethods.isEmpty()) {
this.initBinderAdviceCache.put(bean, binderMethods);
}
ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType);
if (resolver.hasExceptionMappings()) {
this.exceptionHandlerAdviceCache.put(bean, resolver);
}
}
}
if (logger.isDebugEnabled()) {
int modelSize = this.modelAttributeAdviceCache.size();
int binderSize = this.initBinderAdviceCache.size();
int handlerSize = this.exceptionHandlerAdviceCache.size();
if (modelSize == 0 && binderSize == 0 && handlerSize == 0) {
logger.debug("ControllerAdvice beans: none");
} else {
logger.debug("ControllerAdvice beans: " + modelSize + " @ModelAttribute, " + binderSize + " @InitBinder, " + handlerSize + " @ExceptionHandler");
}
}
}
use of org.springframework.web.method.annotation.ExceptionHandlerMethodResolver in project spring-framework by spring-projects.
the class ControllerMethodResolver method getExceptionHandlerMethod.
/**
* Find an {@code @ExceptionHandler} method in {@code @ControllerAdvice}
* components or in the controller of the given {@code @RequestMapping} method.
*/
@Nullable
public InvocableHandlerMethod getExceptionHandlerMethod(Throwable ex, HandlerMethod handlerMethod) {
Class<?> handlerType = handlerMethod.getBeanType();
// Controller-local first...
Object targetBean = handlerMethod.getBean();
Method targetMethod = this.exceptionHandlerCache.computeIfAbsent(handlerType, ExceptionHandlerMethodResolver::new).resolveMethodByThrowable(ex);
if (targetMethod == null) {
// Global exception handlers...
for (Map.Entry<ControllerAdviceBean, ExceptionHandlerMethodResolver> entry : this.exceptionHandlerAdviceCache.entrySet()) {
ControllerAdviceBean advice = entry.getKey();
if (advice.isApplicableToBeanType(handlerType)) {
targetBean = advice.resolveBean();
targetMethod = entry.getValue().resolveMethodByThrowable(ex);
if (targetMethod != null) {
break;
}
}
}
}
if (targetMethod == null) {
return null;
}
InvocableHandlerMethod invocable = new InvocableHandlerMethod(targetBean, targetMethod);
invocable.setArgumentResolvers(this.exceptionHandlerResolvers);
return invocable;
}
Aggregations