1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
25
25
import org .springframework .beans .factory .InitializingBean ;
26
26
import org .springframework .beans .factory .config .BeanDefinition ;
27
27
import org .springframework .beans .factory .config .BeanDefinitionHolder ;
28
- import org .springframework .beans .factory .config .BeanReference ;
29
28
import org .springframework .beans .factory .config .RuntimeBeanReference ;
30
29
import org .springframework .beans .factory .parsing .BeanComponentDefinition ;
31
30
import org .springframework .beans .factory .parsing .CompositeComponentDefinition ;
53
52
import org .springframework .http .converter .xml .MappingJackson2XmlHttpMessageConverter ;
54
53
import org .springframework .http .converter .xml .SourceHttpMessageConverter ;
55
54
import org .springframework .util .ClassUtils ;
56
- import org .springframework .util .StringUtils ;
57
55
import org .springframework .util .xml .DomUtils ;
58
56
import org .springframework .web .HttpRequestHandler ;
59
57
import org .springframework .web .accept .ContentNegotiationManager ;
60
58
import org .springframework .web .accept .ContentNegotiationManagerFactoryBean ;
61
- import org .springframework .web .bind .annotation .ExceptionHandler ;
62
- import org .springframework .web .bind .annotation .ResponseStatus ;
63
59
import org .springframework .web .bind .support .ConfigurableWebBindingInitializer ;
64
60
import org .springframework .web .bind .support .WebArgumentResolver ;
65
61
import org .springframework .web .method .support .CompositeUriComponentsContributor ;
110
106
*
111
107
* <p>This class registers the following {@link HandlerExceptionResolver}s:
112
108
* <ul>
113
- * <li>{@link ExceptionHandlerExceptionResolver} for handling exceptions
114
- * through @ {@link ExceptionHandler} methods.
109
+ * <li>{@link ExceptionHandlerExceptionResolver} for handling exceptions through
110
+ * {@link org.springframework.web.bind.annotation. ExceptionHandler} methods.
115
111
* <li>{@link ResponseStatusExceptionResolver} for exceptions annotated
116
- * with @ {@link ResponseStatus}.
112
+ * with {@link org.springframework.web.bind.annotation. ResponseStatus}.
117
113
* <li>{@link DefaultHandlerExceptionResolver} for resolving known Spring
118
114
* exception types
119
115
* </ul>
@@ -155,6 +151,7 @@ class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
155
151
156
152
public static final String CONTENT_NEGOTIATION_MANAGER_BEAN_NAME = "mvcContentNegotiationManager" ;
157
153
154
+
158
155
private static final boolean javaxValidationPresent =
159
156
ClassUtils .isPresent ("javax.validation.Validator" ,
160
157
AnnotationDrivenBeanDefinitionParser .class .getClassLoader ());
@@ -210,8 +207,8 @@ else if (element.hasAttribute("enableMatrixVariables")) {
210
207
configurePathMatchingProperties (handlerMappingDef , element , parserContext );
211
208
readerContext .getRegistry ().registerBeanDefinition (HANDLER_MAPPING_BEAN_NAME , handlerMappingDef );
212
209
213
- RuntimeBeanReference corsConfigurationsRef = MvcNamespaceUtils .registerCorsConfigurations (null , parserContext , source );
214
- handlerMappingDef .getPropertyValues ().add ("corsConfigurations" , corsConfigurationsRef );
210
+ RuntimeBeanReference corsRef = MvcNamespaceUtils .registerCorsConfigurations (null , parserContext , source );
211
+ handlerMappingDef .getPropertyValues ().add ("corsConfigurations" , corsRef );
215
212
216
213
RuntimeBeanReference conversionService = getConversionService (element , source , parserContext );
217
214
RuntimeBeanReference validator = getValidator (element , source , parserContext );
@@ -285,44 +282,40 @@ else if (element.hasAttribute("ignoreDefaultModelOnRedirect")) {
285
282
mappedCsInterceptorDef .getConstructorArgumentValues ().addIndexedArgumentValue (1 , csInterceptorDef );
286
283
String mappedInterceptorName = readerContext .registerWithGeneratedName (mappedCsInterceptorDef );
287
284
288
- RootBeanDefinition exceptionHandlerExceptionResolver = new RootBeanDefinition (ExceptionHandlerExceptionResolver .class );
289
- exceptionHandlerExceptionResolver .setSource (source );
290
- exceptionHandlerExceptionResolver .setRole (BeanDefinition .ROLE_INFRASTRUCTURE );
291
- exceptionHandlerExceptionResolver .getPropertyValues ().add ("contentNegotiationManager" , contentNegotiationManager );
292
- exceptionHandlerExceptionResolver .getPropertyValues ().add ("messageConverters" , messageConverters );
293
- exceptionHandlerExceptionResolver .getPropertyValues ().add ("order" , 0 );
294
- addResponseBodyAdvice (exceptionHandlerExceptionResolver );
295
-
285
+ RootBeanDefinition methodExceptionResolver = new RootBeanDefinition (ExceptionHandlerExceptionResolver .class );
286
+ methodExceptionResolver .setSource (source );
287
+ methodExceptionResolver .setRole (BeanDefinition .ROLE_INFRASTRUCTURE );
288
+ methodExceptionResolver .getPropertyValues ().add ("contentNegotiationManager" , contentNegotiationManager );
289
+ methodExceptionResolver .getPropertyValues ().add ("messageConverters" , messageConverters );
290
+ methodExceptionResolver .getPropertyValues ().add ("order" , 0 );
291
+ addResponseBodyAdvice (methodExceptionResolver );
296
292
if (argumentResolvers != null ) {
297
- exceptionHandlerExceptionResolver .getPropertyValues ().add ("customArgumentResolvers" , argumentResolvers );
293
+ methodExceptionResolver .getPropertyValues ().add ("customArgumentResolvers" , argumentResolvers );
298
294
}
299
295
if (returnValueHandlers != null ) {
300
- exceptionHandlerExceptionResolver .getPropertyValues ().add ("customReturnValueHandlers" , returnValueHandlers );
296
+ methodExceptionResolver .getPropertyValues ().add ("customReturnValueHandlers" , returnValueHandlers );
301
297
}
298
+ String methodExResolverName = readerContext .registerWithGeneratedName (methodExceptionResolver );
302
299
303
- String methodExceptionResolverName = readerContext .registerWithGeneratedName (exceptionHandlerExceptionResolver );
304
-
305
- RootBeanDefinition responseStatusExceptionResolver = new RootBeanDefinition (ResponseStatusExceptionResolver .class );
306
- responseStatusExceptionResolver .setSource (source );
307
- responseStatusExceptionResolver .setRole (BeanDefinition .ROLE_INFRASTRUCTURE );
308
- responseStatusExceptionResolver .getPropertyValues ().add ("order" , 1 );
309
- String responseStatusExceptionResolverName =
310
- readerContext .registerWithGeneratedName (responseStatusExceptionResolver );
300
+ RootBeanDefinition statusExceptionResolver = new RootBeanDefinition (ResponseStatusExceptionResolver .class );
301
+ statusExceptionResolver .setSource (source );
302
+ statusExceptionResolver .setRole (BeanDefinition .ROLE_INFRASTRUCTURE );
303
+ statusExceptionResolver .getPropertyValues ().add ("order" , 1 );
304
+ String statusExResolverName = readerContext .registerWithGeneratedName (statusExceptionResolver );
311
305
312
306
RootBeanDefinition defaultExceptionResolver = new RootBeanDefinition (DefaultHandlerExceptionResolver .class );
313
307
defaultExceptionResolver .setSource (source );
314
308
defaultExceptionResolver .setRole (BeanDefinition .ROLE_INFRASTRUCTURE );
315
309
defaultExceptionResolver .getPropertyValues ().add ("order" , 2 );
316
- String defaultExceptionResolverName =
317
- readerContext .registerWithGeneratedName (defaultExceptionResolver );
310
+ String defaultExResolverName = readerContext .registerWithGeneratedName (defaultExceptionResolver );
318
311
319
312
parserContext .registerComponent (new BeanComponentDefinition (handlerMappingDef , HANDLER_MAPPING_BEAN_NAME ));
320
313
parserContext .registerComponent (new BeanComponentDefinition (handlerAdapterDef , HANDLER_ADAPTER_BEAN_NAME ));
321
- parserContext .registerComponent (new BeanComponentDefinition (uriCompContribDef , uriCompContribName ));
322
- parserContext .registerComponent (new BeanComponentDefinition (exceptionHandlerExceptionResolver , methodExceptionResolverName ));
323
- parserContext .registerComponent (new BeanComponentDefinition (responseStatusExceptionResolver , responseStatusExceptionResolverName ));
324
- parserContext .registerComponent (new BeanComponentDefinition (defaultExceptionResolver , defaultExceptionResolverName ));
325
- parserContext .registerComponent (new BeanComponentDefinition (mappedCsInterceptorDef , mappedInterceptorName ));
314
+ parserContext .registerComponent (new BeanComponentDefinition (uriContributorDef , uriContributorName ));
315
+ parserContext .registerComponent (new BeanComponentDefinition (mappedInterceptorDef , mappedInterceptorName ));
316
+ parserContext .registerComponent (new BeanComponentDefinition (methodExceptionResolver , methodExResolverName ));
317
+ parserContext .registerComponent (new BeanComponentDefinition (statusExceptionResolver , statusExResolverName ));
318
+ parserContext .registerComponent (new BeanComponentDefinition (defaultExceptionResolver , defaultExResolverName ));
326
319
327
320
// Ensure BeanNameUrlHandlerMapping (SPR-8289) and default HandlerAdapters are not "turned off"
328
321
MvcNamespaceUtils .registerDefaultComponents (parserContext , source );
@@ -380,8 +373,8 @@ else if (javaxValidationPresent) {
380
373
}
381
374
}
382
375
383
- private RuntimeBeanReference getContentNegotiationManager (Element element , Object source ,
384
- ParserContext parserContext ) {
376
+ private RuntimeBeanReference getContentNegotiationManager (
377
+ Element element , Object source , ParserContext parserContext ) {
385
378
386
379
RuntimeBeanReference beanRef ;
387
380
if (element .hasAttribute ("content-negotiation-manager" )) {
@@ -402,8 +395,8 @@ private RuntimeBeanReference getContentNegotiationManager(Element element, Objec
402
395
return beanRef ;
403
396
}
404
397
405
- private void configurePathMatchingProperties (RootBeanDefinition handlerMappingDef , Element element ,
406
- ParserContext parserContext ) {
398
+ private void configurePathMatchingProperties (
399
+ RootBeanDefinition handlerMappingDef , Element element , ParserContext parserContext ) {
407
400
408
401
Element pathMatchingElement = DomUtils .getChildElementByTagName (element , "path-matching" );
409
402
if (pathMatchingElement != null ) {
@@ -462,15 +455,13 @@ private RuntimeBeanReference getMessageCodesResolver(Element element) {
462
455
463
456
private String getAsyncTimeout (Element element ) {
464
457
Element asyncElement = DomUtils .getChildElementByTagName (element , "async-support" );
465
- return (asyncElement != null ) ? asyncElement .getAttribute ("default-timeout" ) : null ;
458
+ return (asyncElement != null ? asyncElement .getAttribute ("default-timeout" ) : null ) ;
466
459
}
467
460
468
461
private RuntimeBeanReference getAsyncExecutor (Element element ) {
469
462
Element asyncElement = DomUtils .getChildElementByTagName (element , "async-support" );
470
- if (asyncElement != null ) {
471
- if (asyncElement .hasAttribute ("task-executor" )) {
472
- return new RuntimeBeanReference (asyncElement .getAttribute ("task-executor" ));
473
- }
463
+ if (asyncElement != null && asyncElement .hasAttribute ("task-executor" )) {
464
+ return new RuntimeBeanReference (asyncElement .getAttribute ("task-executor" ));
474
465
}
475
466
return null ;
476
467
}
@@ -571,7 +562,8 @@ private ManagedList<?> getMessageConverters(Element element, Object source, Pars
571
562
}
572
563
573
564
if (jackson2XmlPresent ) {
574
- RootBeanDefinition jacksonConverterDef = createConverterDefinition (MappingJackson2XmlHttpMessageConverter .class , source );
565
+ Class <?> type = MappingJackson2XmlHttpMessageConverter .class ;
566
+ RootBeanDefinition jacksonConverterDef = createConverterDefinition (type , source );
575
567
GenericBeanDefinition jacksonFactoryDef = createObjectMapperFactoryDefinition (source );
576
568
jacksonFactoryDef .getPropertyValues ().add ("createXmlMapper" , true );
577
569
jacksonConverterDef .getConstructorArgumentValues ().addIndexedArgumentValue (0 , jacksonFactoryDef );
@@ -582,7 +574,8 @@ else if (jaxb2Present) {
582
574
}
583
575
584
576
if (jackson2Present ) {
585
- RootBeanDefinition jacksonConverterDef = createConverterDefinition (MappingJackson2HttpMessageConverter .class , source );
577
+ Class <?> type = MappingJackson2HttpMessageConverter .class ;
578
+ RootBeanDefinition jacksonConverterDef = createConverterDefinition (type , source );
586
579
GenericBeanDefinition jacksonFactoryDef = createObjectMapperFactoryDefinition (source );
587
580
jacksonConverterDef .getConstructorArgumentValues ().addIndexedArgumentValue (0 , jacksonFactoryDef );
588
581
messageConverters .add (jacksonConverterDef );
@@ -609,7 +602,6 @@ private RootBeanDefinition createConverterDefinition(Class<?> converterClass, Ob
609
602
return beanDefinition ;
610
603
}
611
604
612
-
613
605
private ManagedList <Object > extractBeanSubElements (Element parentElement , ParserContext parserContext ) {
614
606
ManagedList <Object > list = new ManagedList <Object >();
615
607
list .setSource (parserContext .extractSource (parentElement ));
@@ -620,27 +612,6 @@ private ManagedList<Object> extractBeanSubElements(Element parentElement, Parser
620
612
return list ;
621
613
}
622
614
623
- private ManagedList <BeanReference > extractBeanRefSubElements (Element parentElement , ParserContext parserContext ){
624
- ManagedList <BeanReference > list = new ManagedList <BeanReference >();
625
- list .setSource (parserContext .extractSource (parentElement ));
626
- for (Element refElement : DomUtils .getChildElementsByTagName (parentElement , "ref" )) {
627
- BeanReference reference ;
628
- if (StringUtils .hasText ("bean" )) {
629
- reference = new RuntimeBeanReference (refElement .getAttribute ("bean" ),false );
630
- list .add (reference );
631
- }
632
- else if (StringUtils .hasText ("parent" )){
633
- reference = new RuntimeBeanReference (refElement .getAttribute ("parent" ),true );
634
- list .add (reference );
635
- }
636
- else {
637
- parserContext .getReaderContext ().error ("'bean' or 'parent' attribute is required for <ref> element" ,
638
- parserContext .extractSource (parentElement ));
639
- }
640
- }
641
- return list ;
642
- }
643
-
644
615
645
616
/**
646
617
* A FactoryBean for a CompositeUriComponentsContributor that obtains the
@@ -671,7 +642,7 @@ public void afterPropertiesSet() {
671
642
}
672
643
673
644
@ Override
674
- public CompositeUriComponentsContributor getObject () throws Exception {
645
+ public CompositeUriComponentsContributor getObject () {
675
646
return this .uriComponentsContributor ;
676
647
}
677
648
0 commit comments