Skip to content

Commit a0c316d

Browse files
committed
Improve RepositoryRestConfiguration customization
This commit binds RepositoryRestConfiguration to the spring.data.rest prefix so that any of its property can be customized through the environment. If a RepositoryRestMvcConfiguration is defined in the context, those customization do not apply, as it was the case before. Fixes spring-projectsgh-1171
1 parent 6fedcc3 commit a0c316d

File tree

5 files changed

+74
-10
lines changed

5 files changed

+74
-10
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfiguration.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2121
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2222
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
23+
import org.springframework.boot.context.properties.ConfigurationProperties;
24+
import org.springframework.context.annotation.Bean;
2325
import org.springframework.context.annotation.Configuration;
2426
import org.springframework.context.annotation.Import;
27+
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
2528
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
2629

2730
/**
@@ -32,18 +35,31 @@
3235
* {@link RepositoryRestMvcConfiguration} is found.
3336
* </p>
3437
* <p>
35-
* Once in effect, the auto-configuration is the equivalent of importing the
36-
* {@link RepositoryRestMvcConfiguration}.
38+
* Once in effect, the auto-configuration allows to configure any property
39+
* of {@link RepositoryRestConfiguration} using the {@code spring.data.rest}
40+
* prefix.
3741
* </p>
3842
*
3943
* @author Rob Winch
44+
* @author Stephane Nicoll
4045
* @since 1.1.0
4146
*/
4247
@Configuration
4348
@ConditionalOnWebApplication
4449
@ConditionalOnMissingBean(RepositoryRestMvcConfiguration.class)
4550
@ConditionalOnClass(RepositoryRestMvcConfiguration.class)
46-
@Import(RepositoryRestMvcConfiguration.class)
51+
@Import(RepositoryRestMvcAutoConfiguration.RepositoryRestMvcBootConfiguration.class)
4752
public class RepositoryRestMvcAutoConfiguration {
4853

54+
55+
@Configuration
56+
static class RepositoryRestMvcBootConfiguration extends RepositoryRestMvcConfiguration {
57+
58+
@Override
59+
@Bean
60+
@ConfigurationProperties(prefix = "spring.data.rest")
61+
public RepositoryRestConfiguration config() {
62+
return super.config();
63+
}
64+
}
4965
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/rest/RepositoryRestMvcAutoConfigurationTests.java

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,27 @@
1616

1717
package org.springframework.boot.autoconfigure.data.rest;
1818

19+
import java.net.URI;
20+
21+
import org.junit.After;
1922
import org.junit.Test;
23+
2024
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
2125
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
2226
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
2327
import org.springframework.boot.autoconfigure.data.jpa.city.City;
2428
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
2529
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
30+
import org.springframework.boot.test.EnvironmentTestUtils;
2631
import org.springframework.context.annotation.Configuration;
32+
import org.springframework.context.annotation.Import;
33+
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
2734
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
2835
import org.springframework.mock.web.MockServletContext;
2936
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
3037
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
3138

32-
import static org.junit.Assert.assertNotNull;
39+
import static org.junit.Assert.*;
3340

3441
/**
3542
* Tests for {@link RepositoryRestMvcAutoConfiguration}.
@@ -40,18 +47,49 @@ public class RepositoryRestMvcAutoConfigurationTests {
4047

4148
private AnnotationConfigWebApplicationContext context;
4249

50+
@After
51+
public void tearDown() {
52+
if (this.context != null) {
53+
this.context.close();
54+
}
55+
}
56+
4357
@Test
4458
public void testDefaultRepositoryConfiguration() throws Exception {
45-
this.context = new AnnotationConfigWebApplicationContext();
46-
this.context.setServletContext(new MockServletContext());
47-
this.context.register(TestConfiguration.class,
59+
load(TestConfiguration.class);
60+
assertNotNull(this.context.getBean(RepositoryRestMvcConfiguration.class));
61+
}
62+
63+
@Test
64+
public void testWithCustomBaseUri() throws Exception {
65+
load(TestConfiguration.class, "spring.data.rest.baseUri:foo");
66+
assertNotNull(this.context.getBean(RepositoryRestMvcConfiguration.class));
67+
RepositoryRestConfiguration bean = this.context.getBean(RepositoryRestConfiguration.class);
68+
assertEquals("Custom baseURI not set", URI.create("foo"), bean.getBaseUri());
69+
}
70+
71+
@Test
72+
public void backOffWithCustomConfiguration() {
73+
load(TestConfigurationWithRestMvcConfig.class, "spring.data.rest.baseUri:foo");
74+
assertNotNull(this.context.getBean(RepositoryRestMvcConfiguration.class));
75+
RepositoryRestConfiguration bean = this.context.getBean(RepositoryRestConfiguration.class);
76+
assertEquals("Custom base URI should not have been set", URI.create(""), bean.getBaseUri());
77+
78+
}
79+
80+
private void load(Class<?> config,
81+
String... environment) {
82+
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
83+
applicationContext.setServletContext(new MockServletContext());
84+
applicationContext.register(config,
4885
EmbeddedDataSourceConfiguration.class,
4986
HibernateJpaAutoConfiguration.class,
5087
JpaRepositoriesAutoConfiguration.class,
5188
PropertyPlaceholderAutoConfiguration.class,
5289
RepositoryRestMvcAutoConfiguration.class);
53-
this.context.refresh();
54-
assertNotNull(this.context.getBean(RepositoryRestMvcConfiguration.class));
90+
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
91+
applicationContext.refresh();
92+
this.context = applicationContext;
5593
}
5694

5795
@Configuration
@@ -60,4 +98,10 @@ public void testDefaultRepositoryConfiguration() throws Exception {
6098
protected static class TestConfiguration {
6199

62100
}
101+
102+
@Import({TestConfiguration.class, RepositoryRestMvcConfiguration.class})
103+
protected static class TestConfigurationWithRestMvcConfig {
104+
105+
}
106+
63107
}

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ content into your application; rather pick only the properties that you need.
220220
spring.data.elasticsearch.local=true # if local mode should be used with client nodes
221221
spring.data.elasticsearch.repositories.enabled=true # if spring data repository support is enabled
222222
223+
# DATA RESET ({spring-data-rest-javadoc}/core/config/RepositoryRestConfiguration.{dc-ext}[RepositoryRestConfiguration}])
224+
spring.data.rest.baseUri=foo # base URI against which the exporter should calculate its links
225+
223226
# FLYWAY ({sc-spring-boot-autoconfigure}/flyway/FlywayProperties.{sc-ext}[FlywayProperties])
224227
flyway.locations=classpath:db/migrations # locations of migrations scripts
225228
flyway.schemas= # schemas to update

spring-boot-docs/src/main/asciidoc/index.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Phillip Webb; Dave Syer; Josh Long; Stéphane Nicoll; Rob Winch; Andy Wilkinson;
3333
:spring-data-javadoc: http://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa
3434
:spring-data-commons-javadoc: http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data
3535
:spring-data-mongo-javadoc: http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb
36+
:spring-data-rest-javadoc: http://docs.spring.io/spring-data/rest/docs/current/api/org/springframework/data/rest
3637
:gradle-userguide: http://www.gradle.org/docs/current/userguide
3738
// ======================================================================================
3839

spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationBeanFactoryMetaData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private Method findFactoryMethod(String beanName) {
8888
@Override
8989
public void doWith(Method method) throws IllegalArgumentException,
9090
IllegalAccessException {
91-
if (method.getName().equals(factory)) {
91+
if (found.get() == null && method.getName().equals(factory)) {
9292
found.set(method);
9393
}
9494
}

0 commit comments

Comments
 (0)