Skip to content

Commit b21c362

Browse files
committed
#1021 - Tweak setup of @EnableEntityLinks to make sure it only runs on Spring WebMVC.
We now clearly separate the beans registered by configuration backing @EnableEntityLinks into the ones that depend on Spring MVC being used and commonly shared ones. Tweaked the setup of @EnableHypermediaSupport to rather include the individual configuration classes instead of using @EnableEntityLinks directly as that would always pull WebMVC specific components.
1 parent ff63800 commit b21c362

File tree

6 files changed

+50
-17
lines changed

6 files changed

+50
-17
lines changed

src/main/java/org/springframework/hateoas/config/EnableEntityLinks.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
/**
3030
* Enables the collection of {@link LinkBuilder} instances from the application context. All found ones will be exposed
3131
* through an instance of {@link DelegatingEntityLinks}.
32-
*
32+
*
3333
* @author Oliver Gierke
3434
*/
3535
@Retention(RetentionPolicy.RUNTIME)
3636
@Target(ElementType.TYPE)
3737
@Inherited
3838
@Documented
39-
@Import(EntityLinksConfiguration.class)
39+
@Import({ EntityLinksConfiguration.class, WebMvcEntityLinksConfiguration.class })
4040
public @interface EnableEntityLinks {
4141
}

src/main/java/org/springframework/hateoas/config/EnableHypermediaSupport.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
@Retention(RetentionPolicy.RUNTIME)
4141
@Target(ElementType.TYPE)
4242
@Documented
43-
@EnableEntityLinks
4443
@Import({ HypermediaConfigurationImportSelector.class, HateoasConfiguration.class, WebStackImportSelector.class })
4544
public @interface EnableHypermediaSupport {
4645

src/main/java/org/springframework/hateoas/config/EntityLinksConfiguration.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@
1515
*/
1616
package org.springframework.hateoas.config;
1717

18-
import org.springframework.beans.factory.ObjectProvider;
1918
import org.springframework.context.annotation.Bean;
2019
import org.springframework.context.annotation.Configuration;
2120
import org.springframework.context.annotation.DependsOn;
2221
import org.springframework.context.annotation.Primary;
2322
import org.springframework.hateoas.server.EntityLinks;
24-
import org.springframework.hateoas.server.core.ControllerEntityLinksFactoryBean;
2523
import org.springframework.hateoas.server.core.DelegatingEntityLinks;
26-
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilderFactory;
2724
import org.springframework.plugin.core.PluginRegistry;
2825
import org.springframework.plugin.core.support.PluginRegistryFactoryBean;
29-
import org.springframework.stereotype.Controller;
3026

3127
/**
3228
* Spring configuration to register a {@link PluginRegistry} for {@link EntityLinks}.
@@ -53,14 +49,4 @@ PluginRegistryFactoryBean<EntityLinks, Class<?>> entityLinksPluginRegistry() {
5349
DelegatingEntityLinks delegatingEntityLinks(PluginRegistry<EntityLinks, Class<?>> entityLinksPluginRegistry) {
5450
return new DelegatingEntityLinks(entityLinksPluginRegistry);
5551
}
56-
57-
@Bean
58-
ControllerEntityLinksFactoryBean webMvcEntityLinks(ObjectProvider<WebMvcLinkBuilderFactory> linkBuilderFactory) {
59-
60-
ControllerEntityLinksFactoryBean factory = new ControllerEntityLinksFactoryBean();
61-
factory.setAnnotation(Controller.class);
62-
factory.setLinkBuilderFactory(linkBuilderFactory.getIfAvailable(WebMvcLinkBuilderFactory::new));
63-
64-
return factory;
65-
}
6652
}

src/main/java/org/springframework/hateoas/config/HateoasConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.springframework.beans.factory.BeanCreationException;
1919
import org.springframework.context.annotation.Bean;
2020
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.context.annotation.Import;
2122
import org.springframework.context.annotation.Primary;
2223
import org.springframework.context.support.MessageSourceAccessor;
2324
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
@@ -44,6 +45,7 @@
4445
* @since 0.19
4546
*/
4647
@Configuration
48+
@Import(EntityLinksConfiguration.class)
4749
@EnablePluginRegistries({ LinkDiscoverer.class })
4850
class HateoasConfiguration {
4951

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.hateoas.config;
17+
18+
import org.springframework.beans.factory.ObjectProvider;
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.hateoas.server.EntityLinks;
22+
import org.springframework.hateoas.server.core.ControllerEntityLinksFactoryBean;
23+
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilderFactory;
24+
import org.springframework.stereotype.Controller;
25+
26+
/**
27+
* Spring WebMVC specific bean definitions to support {@link EntityLinks}.
28+
*
29+
* @author Greg Turnquist
30+
* @author Oliver Gierke
31+
*/
32+
@Configuration
33+
class WebMvcEntityLinksConfiguration {
34+
35+
@Bean
36+
ControllerEntityLinksFactoryBean webMvcEntityLinks(ObjectProvider<WebMvcLinkBuilderFactory> linkBuilderFactory) {
37+
38+
ControllerEntityLinksFactoryBean factory = new ControllerEntityLinksFactoryBean();
39+
factory.setAnnotation(Controller.class);
40+
factory.setLinkBuilderFactory(linkBuilderFactory.getIfAvailable(WebMvcLinkBuilderFactory::new));
41+
42+
return factory;
43+
}
44+
}

src/main/java/org/springframework/hateoas/config/WebMvcHateoasConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.beans.factory.config.BeanPostProcessor;
2828
import org.springframework.context.annotation.Bean;
2929
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.context.annotation.Import;
3031
import org.springframework.hateoas.RepresentationModel;
3132
import org.springframework.hateoas.server.RepresentationModelProcessor;
3233
import org.springframework.hateoas.server.mvc.RepresentationModelProcessorHandlerMethodReturnValueHandler;
@@ -50,6 +51,7 @@
5051
* @author Oliver Drotbohm
5152
*/
5253
@Configuration
54+
@Import(WebMvcEntityLinksConfiguration.class)
5355
class WebMvcHateoasConfiguration {
5456

5557
@Bean

0 commit comments

Comments
 (0)