From b36935689ca1180b780360ff6610607e9e9a8233 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 22 Mar 2019 14:51:29 +0800 Subject: [PATCH] Avoid duplicate registration of [RequestBody|ResponseBody]Advice Prior to this commit, if a @ControllerAdvice bean implemented both RequestBodyAdvice and ResponseBodyAdvice, it was registered twice in RequestMappingHandlerAdapter, leading to duplicate application of the same logic. This commit ensures that such instances are only registered once. Fixes gh-22638 --- .../RequestMappingHandlerAdapter.java | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java index 7817cda18d4..d73a3cfba2a 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -109,6 +109,7 @@ import org.springframework.web.util.WebUtils; * * @author Rossen Stoyanchev * @author Juergen Hoeller + * @author Sam Brannen * @since 3.1 * @see HandlerMethodArgumentResolver * @see HandlerMethodReturnValueHandler @@ -562,30 +563,34 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter List requestResponseBodyAdviceBeans = new ArrayList(); for (ControllerAdviceBean bean : beans) { - Set attrMethods = MethodIntrospector.selectMethods(bean.getBeanType(), MODEL_ATTRIBUTE_METHODS); + Class beanType = bean.getBeanType(); + + Set attrMethods = MethodIntrospector.selectMethods(beanType, MODEL_ATTRIBUTE_METHODS); if (!attrMethods.isEmpty()) { this.modelAttributeAdviceCache.put(bean, attrMethods); if (logger.isInfoEnabled()) { logger.info("Detected @ModelAttribute methods in " + bean); } } - Set binderMethods = MethodIntrospector.selectMethods(bean.getBeanType(), INIT_BINDER_METHODS); + Set binderMethods = MethodIntrospector.selectMethods(beanType, INIT_BINDER_METHODS); if (!binderMethods.isEmpty()) { this.initBinderAdviceCache.put(bean, binderMethods); if (logger.isInfoEnabled()) { logger.info("Detected @InitBinder methods in " + bean); } } - if (RequestBodyAdvice.class.isAssignableFrom(bean.getBeanType())) { - requestResponseBodyAdviceBeans.add(bean); - if (logger.isInfoEnabled()) { - logger.info("Detected RequestBodyAdvice bean in " + bean); - } - } - if (ResponseBodyAdvice.class.isAssignableFrom(bean.getBeanType())) { + + boolean isRequestBodyAdvice = RequestBodyAdvice.class.isAssignableFrom(beanType); + boolean isResponseBodyAdvice = ResponseBodyAdvice.class.isAssignableFrom(beanType); + if (isRequestBodyAdvice || isResponseBodyAdvice) { requestResponseBodyAdviceBeans.add(bean); if (logger.isInfoEnabled()) { - logger.info("Detected ResponseBodyAdvice bean in " + bean); + if (isRequestBodyAdvice) { + logger.info("Detected RequestBodyAdvice bean in " + bean); + } + else { + logger.info("Detected ResponseBodyAdvice bean in " + bean); + } } } }