Jpa auto configuration updates for new Spring Data release

User now gets @EnableSpringDataWebSupport for free in any
autoconfigured JPA webapp.

[Fixes #53028329] [bs-216]
This commit is contained in:
Dave Syer
2013-11-06 12:45:49 +00:00
parent 24486eb68c
commit 51f240c1d5
3 changed files with 28 additions and 9 deletions

View File

@@ -52,7 +52,8 @@ import org.springframework.util.StringUtils;
* @author Phillip Webb
* @see AutoConfigurationUtils
*/
class ComponentScanDetector implements ImportBeanDefinitionRegistrar, BeanFactoryAware {
public class ComponentScanDetector implements ImportBeanDefinitionRegistrar,
BeanFactoryAware {
private final Log logger = LogFactory.getLog(getClass());

View File

@@ -23,12 +23,15 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
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.
@@ -44,4 +47,12 @@ import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class JpaRepositoriesAutoConfiguration {
@Configuration
@EnableSpringDataWebSupport
@ConditionalOnWebApplication
@ConditionalOnMissingBean(PageableHandlerMethodArgumentResolver.class)
protected static class JpaWebConfiguration {
}
}

View File

@@ -16,27 +16,28 @@
package org.springframework.boot.autoconfigure.data;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.boot.autoconfigure.ComponentScanDetector;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.data.jpa.City;
import org.springframework.boot.autoconfigure.data.jpa.CityRepository;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.repository.support.DomainClassConverter;
import org.springframework.context.annotation.Import;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.format.support.FormattingConversionService;
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.assertTrue;
/**
* @author Dave Syer
*/
@Ignore
// FIXME until spring data commons 1.6.0, jpa 1.5.0 available
public class JpaWebAutoConfigurationTests {
private AnnotationConfigWebApplicationContext context;
@@ -46,17 +47,23 @@ public class JpaWebAutoConfigurationTests {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(TestConfiguration.class,
EmbeddedDataSourceConfiguration.class, HibernateJpaAutoConfiguration.class,
EmbeddedDataSourceConfiguration.class,
HibernateJpaAutoConfiguration.class,
JpaRepositoriesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(CityRepository.class));
assertNotNull(this.context.getBean(DomainClassConverter.class));
assertNotNull(this.context.getBean(PageableHandlerMethodArgumentResolver.class));
assertTrue(this.context.getBean(FormattingConversionService.class).canConvert(
Long.class, City.class));
}
@Configuration
// @EnableSpringDataWebSupport
@ComponentScan(basePackageClasses = City.class)
// These is usually added by @EnableAutoConfiguration but have to be added as
// annotations if not using that feature
@Import(ComponentScanDetector.class)
@EnableWebMvc
protected static class TestConfiguration {
}