diff --git a/src/main/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscoverer.java b/src/main/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscoverer.java index 768fbf25f..6a7c4b28d 100644 --- a/src/main/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscoverer.java +++ b/src/main/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscoverer.java @@ -19,6 +19,7 @@ import java.util.Collection; import java.util.List; +import org.springframework.core.env.Environment; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.lang.Nullable; @@ -98,10 +99,10 @@ private static String resolveProperties(@Nullable String mapping) { return mapping; } - WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext(); + Environment environment = StaticEnvironmentProvider.get(); - return context == null // + return environment == null // ? mapping // - : context.getEnvironment().resolvePlaceholders(mapping); + : environment.resolvePlaceholders(mapping); } } diff --git a/src/main/java/org/springframework/hateoas/server/core/StaticEnvironmentProvider.java b/src/main/java/org/springframework/hateoas/server/core/StaticEnvironmentProvider.java new file mode 100644 index 000000000..3d62f7238 --- /dev/null +++ b/src/main/java/org/springframework/hateoas/server/core/StaticEnvironmentProvider.java @@ -0,0 +1,46 @@ +/* + * Copyright 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.hateoas.server.core; + +import org.springframework.context.EnvironmentAware; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +/** + * Class to statically provide the {@link Environment} of the current application for the property resolving of the + * {@link PropertyResolvingMappingDiscoverer}. + * + * @author Lars Michele + */ +@Component +class StaticEnvironmentProvider implements EnvironmentAware { + + private static final EnvironmentHolder HOLDER = new EnvironmentHolder(); + + @Override + public void setEnvironment(Environment environment) { + HOLDER.environment = environment; + } + + static Environment get() { + return HOLDER.environment; + } + + private static class EnvironmentHolder { + + private Environment environment; + } +} diff --git a/src/test/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscovererUnitTest.java b/src/test/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscovererUnitTest.java index f94ef4dea..8c44ced9e 100755 --- a/src/test/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscovererUnitTest.java +++ b/src/test/java/org/springframework/hateoas/server/core/PropertyResolvingMappingDiscovererUnitTest.java @@ -16,6 +16,7 @@ package org.springframework.hateoas.server.core; import static org.assertj.core.api.Assertions.*; +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*; import java.lang.reflect.Method; @@ -23,6 +24,8 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; +import org.springframework.hateoas.IanaLinkRelations; +import org.springframework.hateoas.Link; import org.springframework.hateoas.TestUtils; import org.springframework.mock.web.MockServletContext; import org.springframework.test.context.TestPropertySource; @@ -37,17 +40,10 @@ * @author Lars Michele * @author Oliver Drotbohm */ -@SpringJUnitWebConfig(classes = PropertyResolvingMappingDiscovererUnitTest.Config.class) -@TestPropertySource(properties = { "test.parent=resolvedparent", "test.child=resolvedchild" }) +@SpringJUnitWebConfig(classes = {PropertyResolvingMappingDiscovererUnitTest.Config.class, StaticEnvironmentProvider.class}) +@TestPropertySource(properties = {"test.parent=resolvedparent", "test.child=resolvedchild"}) class PropertyResolvingMappingDiscovererUnitTest extends TestUtils { - @Autowired WebApplicationContext context; - - @BeforeEach - void contextLoading() { - new ContextLoader(context).initWebApplicationContext(new MockServletContext()); - } - /** * @see #361 */ @@ -72,6 +68,14 @@ void resolvesVariablesInMappings() throws NoSuchMethodException { .isEqualTo("/resolvedparent/resolvedchild"); } + @Test + void resolvesVariablesInLinkToMethodOnController() { + + Link link = linkTo(methodOn(ResolveMethodEndpointController.class).method()).withSelfRel(); + assertThat(link.getRel()).isEqualTo(IanaLinkRelations.SELF); + assertThat(link.getHref()).endsWith("/resolvedparent/resolvedchild"); + } + @RequestMapping("/${test.parent}") interface ResolveEndpointController {} @@ -79,7 +83,7 @@ interface ResolveEndpointController {} interface ResolveMethodEndpointController { @RequestMapping("/${test.child}") - void method(); + Object method(); } @Configuration