Shared read-only instance of UrlPathHelper

In many places UrlPathHelper is created and used without any
customizations, in some cases repeatedly. This commit adds a
shared read-only UrlPathHelper instance with default settings.

See gh-25100
This commit is contained in:
Rossen Stoyanchev
2020-05-19 07:38:56 +01:00
parent 92f7587a40
commit 23233c3041
11 changed files with 83 additions and 66 deletions

View File

@@ -132,8 +132,7 @@ class DefaultServerRequest implements ServerRequest {
public String path() {
String path = (String) servletRequest().getAttribute(HandlerMapping.LOOKUP_PATH);
if (path == null) {
UrlPathHelper helper = new UrlPathHelper();
path = helper.getLookupPathForRequest(servletRequest());
path = UrlPathHelper.defaultInstance.getLookupPathForRequest(servletRequest());
}
return path;
}

View File

@@ -129,7 +129,7 @@ public final class PatternsRequestCondition extends AbstractRequestCondition<Pat
boolean useTrailingSlashMatch, @Nullable List<String> fileExtensions) {
this.patterns = Collections.unmodifiableSet(prependLeadingSlash(patterns));
this.pathHelper = urlPathHelper != null ? urlPathHelper : new UrlPathHelper();
this.pathHelper = urlPathHelper != null ? urlPathHelper : UrlPathHelper.defaultInstance;
this.pathMatcher = pathMatcher != null ? pathMatcher : new AntPathMatcher();
this.useSuffixPatternMatch = useSuffixPatternMatch;
this.useTrailingSlashMatch = useTrailingSlashMatch;

View File

@@ -91,7 +91,7 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
new ParameterizedTypeReference<List<ResourceRegion>>() { }.getType();
private static final UrlPathHelper decodingUrlPathHelper = new UrlPathHelper();
private static final UrlPathHelper decodingUrlPathHelper = UrlPathHelper.defaultInstance;
private static final UrlPathHelper rawUrlPathHelper = new UrlPathHelper();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@ import org.springframework.web.util.WebUtils;
*/
public class ServletCookieValueMethodArgumentResolver extends AbstractCookieValueMethodArgumentResolver {
private UrlPathHelper urlPathHelper = new UrlPathHelper();
private UrlPathHelper urlPathHelper = UrlPathHelper.defaultInstance;
public ServletCookieValueMethodArgumentResolver(@Nullable ConfigurableBeanFactory beanFactory) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -54,7 +54,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
protected final Log logger = LogFactory.getLog(getClass());
private UrlPathHelper urlPathHelper = new UrlPathHelper();
private UrlPathHelper urlPathHelper = UrlPathHelper.defaultInstance;
private PathMatcher pathMatcher = new AntPathMatcher();

View File

@@ -54,7 +54,7 @@ public abstract class AbstractFlashMapManager implements FlashMapManager {
private int flashMapTimeout = 180;
private UrlPathHelper urlPathHelper = new UrlPathHelper();
private UrlPathHelper urlPathHelper = UrlPathHelper.defaultInstance;
/**

View File

@@ -92,7 +92,7 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
*/
public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request) {
ServletUriComponentsBuilder builder = fromContextPath(request);
if (StringUtils.hasText(new UrlPathHelper().getPathWithinServletMapping(request))) {
if (StringUtils.hasText(UrlPathHelper.defaultInstance.getPathWithinServletMapping(request))) {
builder.path(request.getServletPath());
}
return builder;