diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/JpaRepositoriesAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/JpaRepositoriesAutoConfiguration.java index 2410fe0541..48cbc57e4d 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/JpaRepositoriesAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/jpa/JpaRepositoriesAutoConfiguration.java @@ -24,7 +24,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -32,8 +31,6 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.config.JpaRepositoryConfigExtension; import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean; -import org.springframework.data.web.PageableHandlerMethodArgumentResolver; -import org.springframework.data.web.config.EnableSpringDataWebSupport; /** * {@link EnableAutoConfiguration Auto-configuration} for Spring Data's JPA Repositories. @@ -64,12 +61,4 @@ import org.springframework.data.web.config.EnableSpringDataWebSupport; @AutoConfigureAfter(HibernateJpaAutoConfiguration.class) public class JpaRepositoriesAutoConfiguration { - @Configuration - @EnableSpringDataWebSupport - @ConditionalOnWebApplication - @ConditionalOnMissingBean(PageableHandlerMethodArgumentResolver.class) - protected static class JpaWebConfiguration { - - } - } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java new file mode 100644 index 0000000000..1d0d68b4dc --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfiguration.java @@ -0,0 +1,45 @@ +/* + * Copyright 2012-2014 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 + * + * http://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.boot.autoconfigure.data.web; + +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +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.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.web.PageableHandlerMethodArgumentResolver; +import org.springframework.data.web.config.EnableSpringDataWebSupport; + +/** + * {@link EnableAutoConfiguration Auto-configuration} for Spring Data's web support. + *

+ * When in effect, the auto-configuration is the equivalent of enabling Spring Data's web + * support through the {@link EnableSpringDataWebSupport} annotation. + * + * @author Andy Wilkinson + */ +@Configuration +@EnableSpringDataWebSupport +@ConditionalOnWebApplication +@ConditionalOnClass(PageableHandlerMethodArgumentResolver.class) +@ConditionalOnMissingBean(PageableHandlerMethodArgumentResolver.class) +@AutoConfigureAfter(RepositoryRestMvcAutoConfiguration.class) +public class SpringDataWebAutoConfiguration { + +} diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index 6a41d4929f..b549c5db27 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -16,6 +16,7 @@ org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\ +org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\ org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\ org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\ org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\ diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jpa/JpaWebAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jpa/JpaWebAutoConfigurationTests.java index e3c7b393db..05211a891c 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jpa/JpaWebAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/jpa/JpaWebAutoConfigurationTests.java @@ -20,9 +20,9 @@ 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.JpaWebConfiguration; import org.springframework.boot.autoconfigure.data.jpa.city.City; import org.springframework.boot.autoconfigure.data.jpa.city.CityRepository; +import org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.context.annotation.Configuration; @@ -36,7 +36,8 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; /** - * Tests for {@link JpaWebConfiguration}. + * Tests for {@link SpringDataWebAutoConfiguration} and + * {@link JpaRepositoriesAutoConfiguration}. * * @author Dave Syer */ @@ -57,6 +58,7 @@ public class JpaWebAutoConfigurationTests { EmbeddedDataSourceConfiguration.class, HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class, + SpringDataWebAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertNotNull(this.context.getBean(CityRepository.class)); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java new file mode 100644 index 0000000000..2b8b7d3e5e --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/web/SpringDataWebAutoConfigurationTests.java @@ -0,0 +1,72 @@ +/* + * Copyright 2012-2014 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 + * + * http://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.boot.autoconfigure.data.web; + +import java.util.Map; + +import org.junit.After; +import org.junit.Test; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.data.web.PageableHandlerMethodArgumentResolver; +import org.springframework.mock.web.MockServletContext; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +/** + * Tests for {@link SpringDataWebAutoConfiguration}. + * + * @author Andy Wilkinson + */ +public class SpringDataWebAutoConfigurationTests { + + private ConfigurableApplicationContext context; + + @After + public void after() { + if (this.context != null) { + this.context.close(); + } + } + + @Test + public void webSupportIsAutoConfiguredInWebApplicationContexts() { + this.context = new AnnotationConfigWebApplicationContext(); + ((AnnotationConfigWebApplicationContext) this.context) + .register(SpringDataWebAutoConfiguration.class); + this.context.refresh(); + ((AnnotationConfigWebApplicationContext) this.context) + .setServletContext(new MockServletContext()); + Map beans = this.context + .getBeansOfType(PageableHandlerMethodArgumentResolver.class); + assertThat(beans.size(), is(equalTo(1))); + } + + @Test + public void autoConfigurationBacksOffInNonWebApplicationContexts() { + this.context = new AnnotationConfigApplicationContext(); + ((AnnotationConfigApplicationContext) this.context) + .register(SpringDataWebAutoConfiguration.class); + this.context.refresh(); + Map beans = this.context + .getBeansOfType(PageableHandlerMethodArgumentResolver.class); + assertThat(beans.size(), is(equalTo(0))); + } +}