Skip to content

Commit b369356

Browse files
committed
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
1 parent 47e88aa commit b369356

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -109,6 +109,7 @@
109109
*
110110
* @author Rossen Stoyanchev
111111
* @author Juergen Hoeller
112+
* @author Sam Brannen
112113
* @since 3.1
113114
* @see HandlerMethodArgumentResolver
114115
* @see HandlerMethodReturnValueHandler
@@ -562,30 +563,34 @@ private void initControllerAdviceCache() {
562563
List<Object> requestResponseBodyAdviceBeans = new ArrayList<Object>();
563564

564565
for (ControllerAdviceBean bean : beans) {
565-
Set<Method> attrMethods = MethodIntrospector.selectMethods(bean.getBeanType(), MODEL_ATTRIBUTE_METHODS);
566+
Class<?> beanType = bean.getBeanType();
567+
568+
Set<Method> attrMethods = MethodIntrospector.selectMethods(beanType, MODEL_ATTRIBUTE_METHODS);
566569
if (!attrMethods.isEmpty()) {
567570
this.modelAttributeAdviceCache.put(bean, attrMethods);
568571
if (logger.isInfoEnabled()) {
569572
logger.info("Detected @ModelAttribute methods in " + bean);
570573
}
571574
}
572-
Set<Method> binderMethods = MethodIntrospector.selectMethods(bean.getBeanType(), INIT_BINDER_METHODS);
575+
Set<Method> binderMethods = MethodIntrospector.selectMethods(beanType, INIT_BINDER_METHODS);
573576
if (!binderMethods.isEmpty()) {
574577
this.initBinderAdviceCache.put(bean, binderMethods);
575578
if (logger.isInfoEnabled()) {
576579
logger.info("Detected @InitBinder methods in " + bean);
577580
}
578581
}
579-
if (RequestBodyAdvice.class.isAssignableFrom(bean.getBeanType())) {
580-
requestResponseBodyAdviceBeans.add(bean);
581-
if (logger.isInfoEnabled()) {
582-
logger.info("Detected RequestBodyAdvice bean in " + bean);
583-
}
584-
}
585-
if (ResponseBodyAdvice.class.isAssignableFrom(bean.getBeanType())) {
582+
583+
boolean isRequestBodyAdvice = RequestBodyAdvice.class.isAssignableFrom(beanType);
584+
boolean isResponseBodyAdvice = ResponseBodyAdvice.class.isAssignableFrom(beanType);
585+
if (isRequestBodyAdvice || isResponseBodyAdvice) {
586586
requestResponseBodyAdviceBeans.add(bean);
587587
if (logger.isInfoEnabled()) {
588-
logger.info("Detected ResponseBodyAdvice bean in " + bean);
588+
if (isRequestBodyAdvice) {
589+
logger.info("Detected RequestBodyAdvice bean in " + bean);
590+
}
591+
else {
592+
logger.info("Detected ResponseBodyAdvice bean in " + bean);
593+
}
589594
}
590595
}
591596
}

0 commit comments

Comments
 (0)