Improve Mongo auto-configuration

Disable Mongo auto-configuation when @EnableMongoRepositories is used
and adapt the test helper classes accordingly.

Change the property prefix and dependency management version property
from `...mongo` to `...mongodb` for consistency with Spring Data.

Fixes gh-315
This commit is contained in:
Oliver Gierke 2014-02-05 12:20:23 +01:00 committed by Phillip Webb
parent 16df38e928
commit cf4df1befb
7 changed files with 71 additions and 14 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2013 the original author or authors. * Copyright 2012-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -43,19 +43,21 @@ import com.mongodb.MongoClientURI;
* Repositories. * Repositories.
* *
* @author Dave Syer * @author Dave Syer
* @author Oliver Gierke
* @see EnableMongoRepositories * @see EnableMongoRepositories
*/ */
@Configuration @Configuration
@ConditionalOnClass({ Mongo.class, MongoRepository.class }) @ConditionalOnClass({ Mongo.class, MongoRepository.class })
public class MongoRepositoriesAutoConfiguration { public class MongoRepositoriesAutoConfiguration {
@Import(MongoRepositoriesAutoConfigureRegistrar.class)
@Configuration @Configuration
@Import(MongoRepositoriesAutoConfigureRegistrar.class)
@EnableConfigurationProperties(MongoProperties.class) @EnableConfigurationProperties(MongoProperties.class)
protected static class MongoRepositoriesConfiguration { protected static class MongoRepositoriesConfiguration {
@Autowired @Autowired
private MongoProperties config; private MongoProperties config;
private Mongo mongo; private Mongo mongo;
@PreDestroy @PreDestroy
@ -67,20 +69,20 @@ public class MongoRepositoriesAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean(Mongo.class) @ConditionalOnMissingBean(Mongo.class)
Mongo mongo() throws UnknownHostException { public Mongo mongo() throws UnknownHostException {
this.mongo = this.config.mongo(); this.mongo = this.config.mongo();
return this.mongo; return this.mongo;
} }
@Bean @Bean
@ConditionalOnMissingBean(MongoTemplate.class) @ConditionalOnMissingBean(MongoTemplate.class)
MongoTemplate mongoTemplate(Mongo mongo) throws UnknownHostException { public MongoTemplate mongoTemplate(Mongo mongo) throws UnknownHostException {
return new MongoTemplate(mongo, this.config.database()); return new MongoTemplate(mongo, this.config.database());
} }
} }
@ConfigurationProperties(name = "spring.data.mongo") @ConfigurationProperties(name = "spring.data.mongodb")
public static class MongoProperties { public static class MongoProperties {
private String host; private String host;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2013 the original author or authors. * Copyright 2012-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,9 +18,11 @@ package org.springframework.boot.autoconfigure.data;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.data.mongodb.repository.config.MongoRepositoryConfigurationExtension; import org.springframework.data.mongodb.repository.config.MongoRepositoryConfigurationExtension;
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean;
import org.springframework.data.repository.config.RepositoryConfigurationExtension; import org.springframework.data.repository.config.RepositoryConfigurationExtension;
/** /**
@ -29,6 +31,7 @@ import org.springframework.data.repository.config.RepositoryConfigurationExtensi
* *
* @author Dave Syer * @author Dave Syer
*/ */
@ConditionalOnMissingBean(MongoRepositoryFactoryBean.class)
class MongoRepositoriesAutoConfigureRegistrar extends class MongoRepositoriesAutoConfigureRegistrar extends
AbstractRepositoryConfigurationSourceSupport { AbstractRepositoryConfigurationSourceSupport {

View File

@ -36,6 +36,7 @@ import static org.junit.Assert.assertNotNull;
* Tests for {@link JpaRepositoriesAutoConfiguration}. * Tests for {@link JpaRepositoriesAutoConfiguration}.
* *
* @author Dave Syer * @author Dave Syer
* @author Oliver Gierke
*/ */
public class JpaRepositoriesAutoConfigurationTests { public class JpaRepositoriesAutoConfigurationTests {
@ -65,7 +66,7 @@ public class JpaRepositoriesAutoConfigurationTests {
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertNotNull(this.context assertNotNull(this.context
.getBean(org.springframework.boot.autoconfigure.data.alt.CityRepository.class)); .getBean(org.springframework.boot.autoconfigure.data.alt.CityJpaRepository.class));
assertNotNull(this.context.getBean(PlatformTransactionManager.class)); assertNotNull(this.context.getBean(PlatformTransactionManager.class));
assertNotNull(this.context.getBean(EntityManagerFactory.class)); assertNotNull(this.context.getBean(EntityManagerFactory.class));
} }
@ -77,7 +78,7 @@ public class JpaRepositoriesAutoConfigurationTests {
} }
@Configuration @Configuration
@EnableJpaRepositories(basePackageClasses = org.springframework.boot.autoconfigure.data.alt.CityRepository.class) @EnableJpaRepositories(basePackageClasses = org.springframework.boot.autoconfigure.data.alt.CityJpaRepository.class)
@TestAutoConfigurationPackage(City.class) @TestAutoConfigurationPackage(City.class)
protected static class CustomConfiguration { protected static class CustomConfiguration {

View File

@ -19,19 +19,26 @@ package org.springframework.boot.autoconfigure.data;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
import org.springframework.boot.autoconfigure.data.alt.CityMongoDbRepository;
import org.springframework.boot.autoconfigure.data.mongo.City; import org.springframework.boot.autoconfigure.data.mongo.City;
import org.springframework.boot.autoconfigure.data.mongo.CityRepository; import org.springframework.boot.autoconfigure.data.mongo.CityRepository;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import com.mongodb.Mongo; import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
/** /**
* Tests for {@link MongoRepositoriesAutoConfiguration}. * Tests for {@link MongoRepositoriesAutoConfiguration}.
* *
* @author Dave Syer * @author Dave Syer
* @author Oliver Gierke
*/ */
public class MongoRepositoriesAutoConfigurationTests { public class MongoRepositoriesAutoConfigurationTests {
@ -45,7 +52,9 @@ public class MongoRepositoriesAutoConfigurationTests {
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertNotNull(this.context.getBean(CityRepository.class)); assertNotNull(this.context.getBean(CityRepository.class));
assertNotNull(this.context.getBean(Mongo.class));
Mongo mongo = this.context.getBean(Mongo.class);
assertThat(mongo, is(instanceOf(MongoClient.class)));
} }
@Test @Test
@ -55,7 +64,19 @@ public class MongoRepositoriesAutoConfigurationTests {
MongoRepositoriesAutoConfiguration.class, MongoRepositoriesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertNotNull(this.context.getBean(Mongo.class));
Mongo mongo = this.context.getBean(Mongo.class);
assertThat(mongo, is(instanceOf(MongoClient.class)));
}
@Test
public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(CustomizedConfiguration.class,
MongoRepositoriesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertNotNull(this.context.getBean(CityMongoDbRepository.class));
} }
@Configuration @Configuration
@ -70,4 +91,10 @@ public class MongoRepositoriesAutoConfigurationTests {
} }
@Configuration
@TestAutoConfigurationPackage(MongoRepositoriesAutoConfigurationTests.class)
@EnableMongoRepositories(basePackageClasses = CityMongoDbRepository.class)
protected static class CustomizedConfiguration {
}
} }

View File

@ -19,6 +19,6 @@ package org.springframework.boot.autoconfigure.data.alt;
import org.springframework.boot.autoconfigure.data.jpa.City; import org.springframework.boot.autoconfigure.data.jpa.City;
import org.springframework.data.repository.Repository; import org.springframework.data.repository.Repository;
public interface CityRepository extends Repository<City, Long> { public interface CityJpaRepository extends Repository<City, Long> {
} }

View File

@ -0,0 +1,24 @@
/*
* Copyright 2014-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.alt;
import org.springframework.boot.autoconfigure.data.mongo.City;
import org.springframework.data.repository.Repository;
public interface CityMongoDbRepository extends Repository<City, Long> {
}

View File

@ -46,7 +46,7 @@
<spring-integration.version>3.0.0.RELEASE</spring-integration.version> <spring-integration.version>3.0.0.RELEASE</spring-integration.version>
<spring-batch.version>2.2.4.RELEASE</spring-batch.version> <spring-batch.version>2.2.4.RELEASE</spring-batch.version>
<spring-data-jpa.version>1.4.3.RELEASE</spring-data-jpa.version> <spring-data-jpa.version>1.4.3.RELEASE</spring-data-jpa.version>
<spring-data-mongo.version>1.3.3.RELEASE</spring-data-mongo.version> <spring-data-mongodb.version>1.3.3.RELEASE</spring-data-mongodb.version>
<spring-data-redis.version>1.1.1.RELEASE</spring-data-redis.version> <spring-data-redis.version>1.1.1.RELEASE</spring-data-redis.version>
<spring-rabbit.version>1.2.1.RELEASE</spring-rabbit.version> <spring-rabbit.version>1.2.1.RELEASE</spring-rabbit.version>
<spring-mobile.version>1.1.0.RELEASE</spring-mobile.version> <spring-mobile.version>1.1.0.RELEASE</spring-mobile.version>
@ -469,7 +469,7 @@
<dependency> <dependency>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId> <artifactId>spring-data-mongodb</artifactId>
<version>${spring-data-mongo.version}</version> <version>${spring-data-mongodb.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>