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 gh-1171
This commit is contained in:
parent
6fedcc3aca
commit
a0c316d392
|
@ -20,8 +20,11 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
|
||||
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
|
||||
|
||||
/**
|
||||
|
@ -32,18 +35,31 @@ import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguratio
|
|||
* {@link RepositoryRestMvcConfiguration} is found.
|
||||
* </p>
|
||||
* <p>
|
||||
* Once in effect, the auto-configuration is the equivalent of importing the
|
||||
* {@link RepositoryRestMvcConfiguration}.
|
||||
* Once in effect, the auto-configuration allows to configure any property
|
||||
* of {@link RepositoryRestConfiguration} using the {@code spring.data.rest}
|
||||
* prefix.
|
||||
* </p>
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnWebApplication
|
||||
@ConditionalOnMissingBean(RepositoryRestMvcConfiguration.class)
|
||||
@ConditionalOnClass(RepositoryRestMvcConfiguration.class)
|
||||
@Import(RepositoryRestMvcConfiguration.class)
|
||||
@Import(RepositoryRestMvcAutoConfiguration.RepositoryRestMvcBootConfiguration.class)
|
||||
public class RepositoryRestMvcAutoConfiguration {
|
||||
|
||||
|
||||
@Configuration
|
||||
static class RepositoryRestMvcBootConfiguration extends RepositoryRestMvcConfiguration {
|
||||
|
||||
@Override
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "spring.data.rest")
|
||||
public RepositoryRestConfiguration config() {
|
||||
return super.config();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,20 +16,27 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.data.rest;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
|
||||
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.data.jpa.city.City;
|
||||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
|
||||
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Tests for {@link RepositoryRestMvcAutoConfiguration}.
|
||||
|
@ -40,18 +47,49 @@ public class RepositoryRestMvcAutoConfigurationTests {
|
|||
|
||||
private AnnotationConfigWebApplicationContext context;
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultRepositoryConfiguration() throws Exception {
|
||||
this.context = new AnnotationConfigWebApplicationContext();
|
||||
this.context.setServletContext(new MockServletContext());
|
||||
this.context.register(TestConfiguration.class,
|
||||
load(TestConfiguration.class);
|
||||
assertNotNull(this.context.getBean(RepositoryRestMvcConfiguration.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithCustomBaseUri() throws Exception {
|
||||
load(TestConfiguration.class, "spring.data.rest.baseUri:foo");
|
||||
assertNotNull(this.context.getBean(RepositoryRestMvcConfiguration.class));
|
||||
RepositoryRestConfiguration bean = this.context.getBean(RepositoryRestConfiguration.class);
|
||||
assertEquals("Custom baseURI not set", URI.create("foo"), bean.getBaseUri());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void backOffWithCustomConfiguration() {
|
||||
load(TestConfigurationWithRestMvcConfig.class, "spring.data.rest.baseUri:foo");
|
||||
assertNotNull(this.context.getBean(RepositoryRestMvcConfiguration.class));
|
||||
RepositoryRestConfiguration bean = this.context.getBean(RepositoryRestConfiguration.class);
|
||||
assertEquals("Custom base URI should not have been set", URI.create(""), bean.getBaseUri());
|
||||
|
||||
}
|
||||
|
||||
private void load(Class<?> config,
|
||||
String... environment) {
|
||||
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
|
||||
applicationContext.setServletContext(new MockServletContext());
|
||||
applicationContext.register(config,
|
||||
EmbeddedDataSourceConfiguration.class,
|
||||
HibernateJpaAutoConfiguration.class,
|
||||
JpaRepositoriesAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
RepositoryRestMvcAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertNotNull(this.context.getBean(RepositoryRestMvcConfiguration.class));
|
||||
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
|
||||
applicationContext.refresh();
|
||||
this.context = applicationContext;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
@ -60,4 +98,10 @@ public class RepositoryRestMvcAutoConfigurationTests {
|
|||
protected static class TestConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@Import({TestConfiguration.class, RepositoryRestMvcConfiguration.class})
|
||||
protected static class TestConfigurationWithRestMvcConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -220,6 +220,9 @@ content into your application; rather pick only the properties that you need.
|
|||
spring.data.elasticsearch.local=true # if local mode should be used with client nodes
|
||||
spring.data.elasticsearch.repositories.enabled=true # if spring data repository support is enabled
|
||||
|
||||
# DATA RESET ({spring-data-rest-javadoc}/core/config/RepositoryRestConfiguration.{dc-ext}[RepositoryRestConfiguration}])
|
||||
spring.data.rest.baseUri=foo # base URI against which the exporter should calculate its links
|
||||
|
||||
# FLYWAY ({sc-spring-boot-autoconfigure}/flyway/FlywayProperties.{sc-ext}[FlywayProperties])
|
||||
flyway.locations=classpath:db/migrations # locations of migrations scripts
|
||||
flyway.schemas= # schemas to update
|
||||
|
|
|
@ -33,6 +33,7 @@ Phillip Webb; Dave Syer; Josh Long; Stéphane Nicoll; Rob Winch; Andy Wilkinson;
|
|||
:spring-data-javadoc: http://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa
|
||||
:spring-data-commons-javadoc: http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data
|
||||
:spring-data-mongo-javadoc: http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb
|
||||
:spring-data-rest-javadoc: http://docs.spring.io/spring-data/rest/docs/current/api/org/springframework/data/rest
|
||||
:gradle-userguide: http://www.gradle.org/docs/current/userguide
|
||||
// ======================================================================================
|
||||
|
||||
|
|
|
@ -88,7 +88,7 @@ public class ConfigurationBeanFactoryMetaData implements BeanFactoryPostProcesso
|
|||
@Override
|
||||
public void doWith(Method method) throws IllegalArgumentException,
|
||||
IllegalAccessException {
|
||||
if (method.getName().equals(factory)) {
|
||||
if (found.get() == null && method.getName().equals(factory)) {
|
||||
found.set(method);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue