Skip to content

Commit 42f475b

Browse files
committed
Use EmbeddedValueResolver to resolve embedded value in MvcUriComponentsBuilder
Signed-off-by: 秦利斌 <[email protected]>
1 parent 3dc2aa7 commit 42f475b

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.aot.AotDetector;
3434
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3535
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
36+
import org.springframework.beans.factory.config.EmbeddedValueResolver;
3637
import org.springframework.cglib.core.SpringNamingPolicy;
3738
import org.springframework.cglib.proxy.Callback;
3839
import org.springframework.cglib.proxy.Enhancer;
@@ -54,7 +55,6 @@
5455
import org.springframework.util.ReflectionUtils;
5556
import org.springframework.util.ReflectionUtils.MethodFilter;
5657
import org.springframework.util.StringUtils;
57-
import org.springframework.util.SystemPropertyUtils;
5858
import org.springframework.web.bind.annotation.RequestMapping;
5959
import org.springframework.web.context.WebApplicationContext;
6060
import org.springframework.web.context.request.RequestAttributes;
@@ -629,14 +629,13 @@ private static CompositeUriComponentsContributor getUriComponentsContributor() {
629629
}
630630

631631
private static String resolveEmbeddedValue(String value) {
632-
if (value.contains(SystemPropertyUtils.PLACEHOLDER_PREFIX)) {
633-
WebApplicationContext webApplicationContext = getWebApplicationContext();
634-
if (webApplicationContext != null &&
635-
webApplicationContext.getAutowireCapableBeanFactory() instanceof ConfigurableBeanFactory cbf) {
636-
String resolvedEmbeddedValue = cbf.resolveEmbeddedValue(value);
637-
if (resolvedEmbeddedValue != null) {
638-
return resolvedEmbeddedValue;
639-
}
632+
WebApplicationContext webApplicationContext = getWebApplicationContext();
633+
if (webApplicationContext != null &&
634+
webApplicationContext.getAutowireCapableBeanFactory() instanceof ConfigurableBeanFactory cbf) {
635+
EmbeddedValueResolver embeddedValueResolver = new EmbeddedValueResolver(cbf);
636+
String resolvedEmbeddedValue = embeddedValueResolver.resolveStringValue(value);
637+
if (resolvedEmbeddedValue != null) {
638+
return resolvedEmbeddedValue;
640639
}
641640
}
642641
return value;

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilderTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,22 @@ void fromMethodNameConfigurablePath() {
335335
assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/something/custom/1/foo");
336336
}
337337

338+
@Test
339+
void fromMethodNameConfigurablePathSpEL() {
340+
try {
341+
System.setProperty("customMapping", "custom");
342+
StandardEnvironment environment = new StandardEnvironment();
343+
initWebApplicationContext(WebConfig.class, environment);
344+
UriComponents uriComponents = fromMethodName(ControllerWithMethods.class,
345+
"methodWithConfigurableMappingThroughSpEL", "1").build();
346+
assertThat(uriComponents.toUriString()).isEqualTo("http://localhost/something/custom/1/foo");
347+
}
348+
finally {
349+
System.clearProperty("customMapping");
350+
}
351+
352+
}
353+
338354
@Test
339355
void fromMethodNameWithAnnotationsOnInterface() {
340356
initWebApplicationContext(WebConfig.class);
@@ -703,6 +719,11 @@ HttpEntity<Void> methodWithOptionalNamedParam(@RequestParam("search") Optional<S
703719
HttpEntity<Void> methodWithConfigurableMapping(@PathVariable String id) {
704720
return null;
705721
}
722+
723+
@RequestMapping("/#{systemProperties.customMapping}/{id}/foo")
724+
HttpEntity<Void> methodWithConfigurableMappingThroughSpEL(@PathVariable String id) {
725+
return null;
726+
}
706727
}
707728

708729

0 commit comments

Comments
 (0)