Expose mvcPatternParser bean in WebMvcConfigurationSupport

See gh-26427
This commit is contained in:
Rossen Stoyanchev 2021-01-22 17:30:45 +00:00
parent 5c1cbb769c
commit 5a640bb635
3 changed files with 34 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -63,6 +63,9 @@ public class PathMatchConfigurer {
@Nullable @Nullable
private PathMatcher pathMatcher; private PathMatcher pathMatcher;
@Nullable
private PathPatternParser defaultPatternParser;
@Nullable @Nullable
private UrlPathHelper defaultUrlPathHelper; private UrlPathHelper defaultUrlPathHelper;
@ -226,7 +229,7 @@ public class PathMatchConfigurer {
} }
/** /**
* Return the configure UrlPathHelper instance or a default (shared) instance. * Return the configure UrlPathHelper or a default, shared instance otherwise.
* @since 5.3 * @since 5.3
*/ */
protected UrlPathHelper getUrlPathHelperOrDefault() { protected UrlPathHelper getUrlPathHelperOrDefault() {
@ -240,7 +243,7 @@ public class PathMatchConfigurer {
} }
/** /**
* Return the configure PathMatcher instance or a default (shared) instance. * Return the configure PathMatcher or a default, shared, instance otherwise.
* @since 5.3 * @since 5.3
*/ */
protected PathMatcher getPathMatcherOrDefault() { protected PathMatcher getPathMatcherOrDefault() {
@ -253,4 +256,17 @@ public class PathMatchConfigurer {
return this.defaultPathMatcher; return this.defaultPathMatcher;
} }
/**
* Return the configured PathPatternParser or a default, shared, instance otherwise.
* @since 5.3.4
*/
public PathPatternParser getPatternParserOrDefault() {
if (this.patternParser != null) {
return this.patternParser;
}
if (this.defaultPatternParser == null) {
this.defaultPatternParser = new PathPatternParser();
}
return this.defaultPatternParser;
}
} }

View File

@ -395,6 +395,17 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
protected void configurePathMatch(PathMatchConfigurer configurer) { protected void configurePathMatch(PathMatchConfigurer configurer) {
} }
/**
* Return a global {@link PathPatternParser} instance to use for parsing
* patterns to match to the {@link org.springframework.http.server.RequestPath}.
* The returned instance can be configured using
* {@link #configurePathMatch(PathMatchConfigurer)}.
* @since 5.3.4
*/
@Bean
public PathPatternParser mvcPatternParser() {
return getPathMatchConfigurer().getPatternParserOrDefault();
}
/** /**
* Return a global {@link UrlPathHelper} instance which is used to resolve * Return a global {@link UrlPathHelper} instance which is used to resolve

View File

@ -274,7 +274,6 @@ public class DelegatingWebMvcConfigurationTests {
@Test @Test
public void configurePathPatternParser() { public void configurePathPatternParser() {
PathPatternParser patternParser = new PathPatternParser(); PathPatternParser patternParser = new PathPatternParser();
PathMatcher pathMatcher = mock(PathMatcher.class); PathMatcher pathMatcher = mock(PathMatcher.class);
UrlPathHelper pathHelper = mock(UrlPathHelper.class); UrlPathHelper pathHelper = mock(UrlPathHelper.class);
@ -313,7 +312,9 @@ public class DelegatingWebMvcConfigurationTests {
webMvcConfig.mvcResourceUrlProvider()); webMvcConfig.mvcResourceUrlProvider());
assertThat(annotationsMapping).isNotNull(); assertThat(annotationsMapping).isNotNull();
assertThat(annotationsMapping.getPatternParser()).isSameAs(patternParser); assertThat(annotationsMapping.getPatternParser())
.isSameAs(patternParser)
.isSameAs(webMvcConfig.mvcPatternParser());
configAssertion.accept(annotationsMapping.getUrlPathHelper(), annotationsMapping.getPathMatcher()); configAssertion.accept(annotationsMapping.getUrlPathHelper(), annotationsMapping.getPathMatcher());
SimpleUrlHandlerMapping mapping = (SimpleUrlHandlerMapping) webMvcConfig.viewControllerHandlerMapping( SimpleUrlHandlerMapping mapping = (SimpleUrlHandlerMapping) webMvcConfig.viewControllerHandlerMapping(
@ -344,4 +345,5 @@ public class DelegatingWebMvcConfigurationTests {
assertThat(webMvcConfig.mvcResourceUrlProvider().getUrlPathHelper()).isSameAs(pathHelper); assertThat(webMvcConfig.mvcResourceUrlProvider().getUrlPathHelper()).isSameAs(pathHelper);
assertThat(webMvcConfig.mvcResourceUrlProvider().getPathMatcher()).isSameAs(pathMatcher); assertThat(webMvcConfig.mvcResourceUrlProvider().getPathMatcher()).isSameAs(pathMatcher);
} }
} }