@@ -85,20 +85,37 @@ public ControllerAdviceBean(Object bean) {
85
85
}
86
86
87
87
/**
88
- * Create a {@code ControllerAdviceBean} using the given bean name.
88
+ * Create a {@code ControllerAdviceBean} using the given bean name and
89
+ * {@code BeanFactory}.
89
90
* @param beanName the name of the bean
90
91
* @param beanFactory a {@code BeanFactory} to retrieve the bean type initially
91
92
* and later to resolve the actual bean
92
93
*/
93
94
public ControllerAdviceBean (String beanName , BeanFactory beanFactory ) {
95
+ this (beanName , beanFactory , null );
96
+ }
97
+
98
+ /**
99
+ * Create a {@code ControllerAdviceBean} using the given bean name,
100
+ * {@code BeanFactory}, and {@link ControllerAdvice @ControllerAdvice}
101
+ * annotation.
102
+ * @param beanName the name of the bean
103
+ * @param beanFactory a {@code BeanFactory} to retrieve the bean type initially
104
+ * and later to resolve the actual bean
105
+ * @param controllerAdvice the {@code @ControllerAdvice} annotation for the
106
+ * bean, or {@code null} if not yet retrieved
107
+ * @since 5.2
108
+ */
109
+ public ControllerAdviceBean (String beanName , BeanFactory beanFactory , @ Nullable ControllerAdvice controllerAdvice ) {
94
110
Assert .hasText (beanName , "Bean name must contain text" );
95
111
Assert .notNull (beanFactory , "BeanFactory must not be null" );
96
112
Assert .isTrue (beanFactory .containsBean (beanName ), () -> "BeanFactory [" + beanFactory
97
113
+ "] does not contain specified controller advice bean '" + beanName + "'" );
98
114
99
115
this .beanOrName = beanName ;
100
116
this .beanType = getBeanType (beanName , beanFactory );
101
- this .beanTypePredicate = createBeanTypePredicate (this .beanType );
117
+ this .beanTypePredicate = (controllerAdvice != null ? createBeanTypePredicate (controllerAdvice )
118
+ : createBeanTypePredicate (this .beanType ));
102
119
this .beanFactory = beanFactory ;
103
120
this .order = initOrderFromBeanType (this .beanType );
104
121
}
@@ -186,8 +203,11 @@ public String toString() {
186
203
public static List <ControllerAdviceBean > findAnnotatedBeans (ApplicationContext context ) {
187
204
List <ControllerAdviceBean > adviceBeans = new ArrayList <>();
188
205
for (String name : BeanFactoryUtils .beanNamesForTypeIncludingAncestors (context , Object .class )) {
189
- if (context .findAnnotationOnBean (name , ControllerAdvice .class ) != null ) {
190
- adviceBeans .add (new ControllerAdviceBean (name , context ));
206
+ ControllerAdvice controllerAdvice = context .findAnnotationOnBean (name , ControllerAdvice .class );
207
+ if (controllerAdvice != null ) {
208
+ // Use the @ControllerAdvice annotation found by findAnnotationOnBean()
209
+ // in order to avoid a subsequent lookup of the same annotation.
210
+ adviceBeans .add (new ControllerAdviceBean (name , context , controllerAdvice ));
191
211
}
192
212
}
193
213
return adviceBeans ;
@@ -199,15 +219,18 @@ private static Class<?> getBeanType(String beanName, BeanFactory beanFactory) {
199
219
}
200
220
201
221
private static HandlerTypePredicate createBeanTypePredicate (Class <?> beanType ) {
202
- ControllerAdvice annotation = (beanType != null ?
222
+ ControllerAdvice controllerAdvice = (beanType != null ?
203
223
AnnotatedElementUtils .findMergedAnnotation (beanType , ControllerAdvice .class ) : null );
224
+ return createBeanTypePredicate (controllerAdvice );
225
+ }
204
226
205
- if (annotation != null ) {
227
+ private static HandlerTypePredicate createBeanTypePredicate (ControllerAdvice controllerAdvice ) {
228
+ if (controllerAdvice != null ) {
206
229
return HandlerTypePredicate .builder ()
207
- .basePackage (annotation .basePackages ())
208
- .basePackageClass (annotation .basePackageClasses ())
209
- .assignableType (annotation .assignableTypes ())
210
- .annotation (annotation .annotations ())
230
+ .basePackage (controllerAdvice .basePackages ())
231
+ .basePackageClass (controllerAdvice .basePackageClasses ())
232
+ .assignableType (controllerAdvice .assignableTypes ())
233
+ .annotation (controllerAdvice .annotations ())
211
234
.build ();
212
235
}
213
236
return HandlerTypePredicate .forAnyHandlerType ();
0 commit comments