Skip to content

Commit 2da02cc

Browse files
committed
Polishing
(cherry picked from commit 70424a7)
1 parent f2cc70e commit 2da02cc

File tree

1 file changed

+49
-47
lines changed

1 file changed

+49
-47
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/config/AnnotationDrivenBeanDefinitionParser.java

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@
128128
* <li>the {@link HandlerMapping} for ViewControllers
129129
* <li>and the {@link HandlerMapping} for serving resources
130130
* </ul>
131-
* Note that those beans can be configured by using the {@code path-matching} MVC namespace element.
131+
* Note that those beans can be configured by using the {@code path-matching}
132+
* MVC namespace element.
132133
*
133134
* <p>Both the {@link RequestMappingHandlerAdapter} and the
134135
* {@link ExceptionHandlerExceptionResolver} are configured with instances of
@@ -138,7 +139,7 @@
138139
* <li>A {@link DefaultFormattingConversionService}
139140
* <li>A {@link org.springframework.validation.beanvalidation.LocalValidatorFactoryBean}
140141
* if a JSR-303 implementation is available on the classpath
141-
* <li>A range of {@link HttpMessageConverter}s depending on what 3rd party
142+
* <li>A range of {@link HttpMessageConverter}s depending on which third-party
142143
* libraries are available on the classpath.
143144
* </ul>
144145
*
@@ -158,6 +159,7 @@ class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
158159

159160
public static final String CONTENT_NEGOTIATION_MANAGER_BEAN_NAME = "mvcContentNegotiationManager";
160161

162+
161163
private static final boolean javaxValidationPresent =
162164
ClassUtils.isPresent("javax.validation.Validator",
163165
AnnotationDrivenBeanDefinitionParser.class.getClassLoader());
@@ -189,7 +191,8 @@ class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
189191
AnnotationDrivenBeanDefinitionParser.class.getClassLoader());
190192

191193
private static final boolean gsonPresent =
192-
ClassUtils.isPresent("com.google.gson.Gson", AnnotationDrivenBeanDefinitionParser.class.getClassLoader());
194+
ClassUtils.isPresent("com.google.gson.Gson",
195+
AnnotationDrivenBeanDefinitionParser.class.getClassLoader());
193196

194197

195198
@Override
@@ -269,39 +272,38 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
269272
handlerAdapterDef.getPropertyValues().add("deferredResultInterceptors", deferredResultInterceptors);
270273
readerContext.getRegistry().registerBeanDefinition(HANDLER_ADAPTER_BEAN_NAME , handlerAdapterDef);
271274

272-
String uriCompContribName = MvcUriComponentsBuilder.MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME;
273-
RootBeanDefinition uriCompContribDef = new RootBeanDefinition(CompositeUriComponentsContributorFactoryBean.class);
274-
uriCompContribDef.setSource(source);
275-
uriCompContribDef.getPropertyValues().addPropertyValue("handlerAdapter", handlerAdapterDef);
276-
uriCompContribDef.getPropertyValues().addPropertyValue("conversionService", conversionService);
277-
readerContext.getRegistry().registerBeanDefinition(uriCompContribName, uriCompContribDef);
275+
RootBeanDefinition uriContributorDef =
276+
new RootBeanDefinition(CompositeUriComponentsContributorFactoryBean.class);
277+
uriContributorDef.setSource(source);
278+
uriContributorDef.getPropertyValues().addPropertyValue("handlerAdapter", handlerAdapterDef);
279+
uriContributorDef.getPropertyValues().addPropertyValue("conversionService", conversionService);
280+
String uriContributorName = MvcUriComponentsBuilder.MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME;
281+
readerContext.getRegistry().registerBeanDefinition(uriContributorName, uriContributorDef);
278282

279283
RootBeanDefinition csInterceptorDef = new RootBeanDefinition(ConversionServiceExposingInterceptor.class);
280284
csInterceptorDef.setSource(source);
281285
csInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, conversionService);
282-
RootBeanDefinition mappedCsInterceptorDef = new RootBeanDefinition(MappedInterceptor.class);
283-
mappedCsInterceptorDef.setSource(source);
284-
mappedCsInterceptorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
285-
mappedCsInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, (Object) null);
286-
mappedCsInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, csInterceptorDef);
287-
String mappedInterceptorName = readerContext.registerWithGeneratedName(mappedCsInterceptorDef);
288-
289-
RootBeanDefinition exceptionResolver = new RootBeanDefinition(ExceptionHandlerExceptionResolver.class);
290-
exceptionResolver.setSource(source);
291-
exceptionResolver.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
292-
exceptionResolver.getPropertyValues().add("contentNegotiationManager", contentNegotiationManager);
293-
exceptionResolver.getPropertyValues().add("messageConverters", messageConverters);
294-
exceptionResolver.getPropertyValues().add("order", 0);
295-
addResponseBodyAdvice(exceptionResolver);
296-
286+
RootBeanDefinition mappedInterceptorDef = new RootBeanDefinition(MappedInterceptor.class);
287+
mappedInterceptorDef.setSource(source);
288+
mappedInterceptorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
289+
mappedInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, (Object) null);
290+
mappedInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, csInterceptorDef);
291+
String mappedInterceptorName = readerContext.registerWithGeneratedName(mappedInterceptorDef);
292+
293+
RootBeanDefinition methodExceptionResolver = new RootBeanDefinition(ExceptionHandlerExceptionResolver.class);
294+
methodExceptionResolver.setSource(source);
295+
methodExceptionResolver.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
296+
methodExceptionResolver.getPropertyValues().add("contentNegotiationManager", contentNegotiationManager);
297+
methodExceptionResolver.getPropertyValues().add("messageConverters", messageConverters);
298+
methodExceptionResolver.getPropertyValues().add("order", 0);
299+
addResponseBodyAdvice(methodExceptionResolver);
297300
if (argumentResolvers != null) {
298-
exceptionResolver.getPropertyValues().add("customArgumentResolvers", argumentResolvers);
301+
methodExceptionResolver.getPropertyValues().add("customArgumentResolvers", argumentResolvers);
299302
}
300303
if (returnValueHandlers != null) {
301-
exceptionResolver.getPropertyValues().add("customReturnValueHandlers", returnValueHandlers);
304+
methodExceptionResolver.getPropertyValues().add("customReturnValueHandlers", returnValueHandlers);
302305
}
303-
304-
String methodExceptionResolverName = readerContext.registerWithGeneratedName(exceptionResolver);
306+
String methodExResolverName = readerContext.registerWithGeneratedName(methodExceptionResolver);
305307

306308
RootBeanDefinition statusExceptionResolver = new RootBeanDefinition(ResponseStatusExceptionResolver.class);
307309
statusExceptionResolver.setSource(source);
@@ -317,11 +319,11 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
317319

318320
parserContext.registerComponent(new BeanComponentDefinition(handlerMappingDef, HANDLER_MAPPING_BEAN_NAME));
319321
parserContext.registerComponent(new BeanComponentDefinition(handlerAdapterDef, HANDLER_ADAPTER_BEAN_NAME));
320-
parserContext.registerComponent(new BeanComponentDefinition(uriCompContribDef, uriCompContribName));
321-
parserContext.registerComponent(new BeanComponentDefinition(exceptionResolver, methodExceptionResolverName));
322+
parserContext.registerComponent(new BeanComponentDefinition(uriContributorDef, uriContributorName));
323+
parserContext.registerComponent(new BeanComponentDefinition(mappedInterceptorDef, mappedInterceptorName));
324+
parserContext.registerComponent(new BeanComponentDefinition(methodExceptionResolver, methodExResolverName));
322325
parserContext.registerComponent(new BeanComponentDefinition(statusExceptionResolver, statusExResolverName));
323326
parserContext.registerComponent(new BeanComponentDefinition(defaultExceptionResolver, defaultExResolverName));
324-
parserContext.registerComponent(new BeanComponentDefinition(mappedCsInterceptorDef, mappedInterceptorName));
325327

326328
// Ensure BeanNameUrlHandlerMapping (SPR-8289) and default HandlerAdapters are not "turned off"
327329
MvcNamespaceUtils.registerDefaultComponents(parserContext, source);
@@ -382,8 +384,8 @@ else if (javaxValidationPresent) {
382384
}
383385
}
384386

385-
private RuntimeBeanReference getContentNegotiationManager(Element element, @Nullable Object source,
386-
ParserContext parserContext) {
387+
private RuntimeBeanReference getContentNegotiationManager(
388+
Element element, @Nullable Object source, ParserContext parserContext) {
387389

388390
RuntimeBeanReference beanRef;
389391
if (element.hasAttribute("content-negotiation-manager")) {
@@ -395,7 +397,6 @@ private RuntimeBeanReference getContentNegotiationManager(Element element, @Null
395397
factoryBeanDef.setSource(source);
396398
factoryBeanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
397399
factoryBeanDef.getPropertyValues().add("mediaTypes", getDefaultMediaTypes());
398-
399400
String name = CONTENT_NEGOTIATION_MANAGER_BEAN_NAME;
400401
parserContext.getReaderContext().getRegistry().registerBeanDefinition(name , factoryBeanDef);
401402
parserContext.registerComponent(new BeanComponentDefinition(factoryBeanDef, name));
@@ -404,12 +405,13 @@ private RuntimeBeanReference getContentNegotiationManager(Element element, @Null
404405
return beanRef;
405406
}
406407

407-
private void configurePathMatchingProperties(RootBeanDefinition handlerMappingDef, Element element,
408-
ParserContext parserContext) {
408+
private void configurePathMatchingProperties(
409+
RootBeanDefinition handlerMappingDef, Element element, ParserContext parserContext) {
409410

410411
Element pathMatchingElement = DomUtils.getChildElementByTagName(element, "path-matching");
411412
if (pathMatchingElement != null) {
412413
Object source = parserContext.extractSource(element);
414+
413415
if (pathMatchingElement.hasAttribute("suffix-pattern")) {
414416
Boolean useSuffixPatternMatch = Boolean.valueOf(pathMatchingElement.getAttribute("suffix-pattern"));
415417
handlerMappingDef.getPropertyValues().add("useSuffixPatternMatch", useSuffixPatternMatch);
@@ -422,6 +424,7 @@ private void configurePathMatchingProperties(RootBeanDefinition handlerMappingDe
422424
Boolean useRegisteredSuffixPatternMatch = Boolean.valueOf(pathMatchingElement.getAttribute("registered-suffixes-only"));
423425
handlerMappingDef.getPropertyValues().add("useRegisteredSuffixPatternMatch", useRegisteredSuffixPatternMatch);
424426
}
427+
425428
RuntimeBeanReference pathHelperRef = null;
426429
if (pathMatchingElement.hasAttribute("path-helper")) {
427430
pathHelperRef = new RuntimeBeanReference(pathMatchingElement.getAttribute("path-helper"));
@@ -439,24 +442,24 @@ private void configurePathMatchingProperties(RootBeanDefinition handlerMappingDe
439442
}
440443

441444
private Properties getDefaultMediaTypes() {
442-
Properties props = new Properties();
445+
Properties defaultMediaTypes = new Properties();
443446
if (romePresent) {
444-
props.put("atom", MediaType.APPLICATION_ATOM_XML_VALUE);
445-
props.put("rss", MediaType.APPLICATION_RSS_XML_VALUE);
447+
defaultMediaTypes.put("atom", MediaType.APPLICATION_ATOM_XML_VALUE);
448+
defaultMediaTypes.put("rss", MediaType.APPLICATION_RSS_XML_VALUE);
446449
}
447450
if (jaxb2Present || jackson2XmlPresent) {
448-
props.put("xml", MediaType.APPLICATION_XML_VALUE);
451+
defaultMediaTypes.put("xml", MediaType.APPLICATION_XML_VALUE);
449452
}
450453
if (jackson2Present || gsonPresent) {
451-
props.put("json", MediaType.APPLICATION_JSON_VALUE);
454+
defaultMediaTypes.put("json", MediaType.APPLICATION_JSON_VALUE);
452455
}
453456
if (jackson2SmilePresent) {
454-
props.put("smile", "application/x-jackson-smile");
457+
defaultMediaTypes.put("smile", "application/x-jackson-smile");
455458
}
456459
if (jackson2CborPresent) {
457-
props.put("cbor", "application/cbor");
460+
defaultMediaTypes.put("cbor", "application/cbor");
458461
}
459-
return props;
462+
return defaultMediaTypes;
460463
}
461464

462465
@Nullable
@@ -472,7 +475,7 @@ private RuntimeBeanReference getMessageCodesResolver(Element element) {
472475
@Nullable
473476
private String getAsyncTimeout(Element element) {
474477
Element asyncElement = DomUtils.getChildElementByTagName(element, "async-support");
475-
return (asyncElement != null) ? asyncElement.getAttribute("default-timeout") : null;
478+
return (asyncElement != null ? asyncElement.getAttribute("default-timeout") : null);
476479
}
477480

478481
@Nullable
@@ -649,7 +652,6 @@ private RootBeanDefinition createConverterDefinition(Class<?> converterClass, @N
649652
return beanDefinition;
650653
}
651654

652-
653655
private ManagedList<Object> extractBeanSubElements(Element parentElement, ParserContext parserContext) {
654656
ManagedList<Object> list = new ManagedList<>();
655657
list.setSource(parserContext.extractSource(parentElement));
@@ -695,7 +697,7 @@ public void afterPropertiesSet() {
695697

696698
@Override
697699
@Nullable
698-
public CompositeUriComponentsContributor getObject() throws Exception {
700+
public CompositeUriComponentsContributor getObject() {
699701
return this.uriComponentsContributor;
700702
}
701703

0 commit comments

Comments
 (0)