Skip to content

Commit 5c1cbb7

Browse files
committed
Check that PathPatternParser is not set too late
This commit ensures the PathPatternParser cannot be set after request mappings have been initialized when it is too late. See gh-26427
1 parent 4a7a225 commit 5c1cbb7

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,6 +48,7 @@
4848
import org.springframework.web.cors.CorsUtils;
4949
import org.springframework.web.method.HandlerMethod;
5050
import org.springframework.web.servlet.HandlerMapping;
51+
import org.springframework.web.util.pattern.PathPatternParser;
5152

5253
/**
5354
* Abstract base class for {@link HandlerMapping} implementations that define
@@ -99,6 +100,14 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
99100
private final MappingRegistry mappingRegistry = new MappingRegistry();
100101

101102

103+
@Override
104+
public void setPatternParser(PathPatternParser patternParser) {
105+
Assert.state(this.mappingRegistry.getRegistrations().isEmpty(),
106+
"PathPatternParser must be set before the initialization of " +
107+
"request mappings through InitializingBean#afterPropertiesSet.");
108+
super.setPatternParser(patternParser);
109+
}
110+
102111
/**
103112
* Whether to detect handler methods in beans in ancestor ApplicationContexts.
104113
* <p>Default is "false": Only beans in the current ApplicationContext are

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.web.util.ServletRequestPathUtils;
4242
import org.springframework.web.util.UrlPathHelper;
4343
import org.springframework.web.util.pattern.PathPattern;
44+
import org.springframework.web.util.pattern.PathPatternParser;
4445

4546
/**
4647
* Abstract base class for URL-mapped {@link HandlerMapping} implementations.
@@ -74,6 +75,14 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping i
7475
private final Map<PathPattern, Object> pathPatternHandlerMap = new LinkedHashMap<>();
7576

7677

78+
@Override
79+
public void setPatternParser(PathPatternParser patternParser) {
80+
Assert.state(this.handlerMap.isEmpty(),
81+
"PathPatternParser must be set before the initialization of " +
82+
"the handler map via ApplicationContextAware#setApplicationContext.");
83+
super.setPatternParser(patternParser);
84+
}
85+
7786
/**
7887
* Set the root handler for this handler mapping, that is,
7988
* the handler to be registered for the root path ("/").

0 commit comments

Comments
 (0)