Skip to content

Commit

Permalink
Improve MongoDataAutoConfiguration
Browse files Browse the repository at this point in the history
Update MongoDataAutoConfiguration to provide easier configuration of
CustomConversions, the MappingMongoConverter, MongoMappingContext
and an authentication database.

Fixes spring-projectsgh-1619
Fixes spring-projectsgh-1730
  • Loading branch information
philwebb committed Nov 20, 2014
1 parent eb03539 commit 670ba33
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.net.UnknownHostException;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Expand All @@ -28,9 +30,16 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.authentication.UserCredentials;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.CustomConversions;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
Expand All @@ -50,6 +59,7 @@
* @author Dave Syer
* @author Oliver Gierke
* @author Josh Long
* @author Phillip Webb
* @since 1.1.0
*/
@Configuration
Expand All @@ -64,15 +74,45 @@ public class MongoDataAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public MongoDbFactory mongoDbFactory(Mongo mongo) throws Exception {
String db = this.properties.getMongoClientDatabase();
return new SimpleMongoDbFactory(mongo, db);
String database = this.properties.getMongoClientDatabase();
String authDatabase = this.properties.getAuthenticationDatabase();
if (StringUtils.hasLength(authDatabase)) {
String username = this.properties.getUsername();
String password = new String(this.properties.getPassword());
UserCredentials credentials = new UserCredentials(username, password);
return new SimpleMongoDbFactory(mongo, database, credentials, authDatabase);
}
return new SimpleMongoDbFactory(mongo, database);
}

@Bean
@ConditionalOnMissingBean
public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory,
MongoConverter converter) throws UnknownHostException {
return new MongoTemplate(mongoDbFactory, converter);
}

@Bean
@ConditionalOnMissingBean(MongoConverter.class)
public MappingMongoConverter mappingMongoConverter(MongoDbFactory factory,
MongoMappingContext context, BeanFactory beanFactory) {
DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver,
context);
try {
mappingConverter.setCustomConversions(beanFactory
.getBean(CustomConversions.class));
}
catch (NoSuchBeanDefinitionException ex) {
// Ignore
}
return mappingConverter;
}

@Bean
@ConditionalOnMissingBean
public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory)
throws UnknownHostException {
return new MongoTemplate(mongoDbFactory);
public MongoMappingContext mongoMappingContext() {
return new MongoMappingContext();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class MongoProperties {

private String database;

private String authenticationDatabase;

private String gridFsDatabase;

private String username;
Expand All @@ -69,6 +71,14 @@ public void setDatabase(String database) {
this.database = database;
}

public String getAuthenticationDatabase() {
return this.authenticationDatabase;
}

public void setAuthenticationDatabase(String authenticationDatabase) {
this.authenticationDatabase = authenticationDatabase;
}

public String getUsername() {
return this.username;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,24 @@

package org.springframework.boot.autoconfigure.mongo;

import java.util.Arrays;

import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.CustomConversions;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;

import com.mongodb.Mongo;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* Tests for {@link MongoDataAutoConfiguration}.
Expand Down Expand Up @@ -61,4 +70,34 @@ public void gridFsTemplateExists() {
assertEquals(1, this.context.getBeanNamesForType(GridFsTemplate.class).length);
}

@Test
public void customConversions() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(CustomConversionsConfig.class);
this.context.register(PropertyPlaceholderAutoConfiguration.class,
MongoAutoConfiguration.class, MongoDataAutoConfiguration.class);
this.context.refresh();
MongoTemplate template = this.context.getBean(MongoTemplate.class);
assertTrue(template.getConverter().getConversionService()
.canConvert(Mongo.class, Boolean.class));
}

@Configuration
static class CustomConversionsConfig {

@Bean
public CustomConversions customConversions() {
return new CustomConversions(Arrays.asList(new MyConverter()));
}

}

private static class MyConverter implements Converter<Mongo, Boolean> {

@Override
public Boolean convert(Mongo source) {
return null;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ content into your application; rather pick only the properties that you need.
spring.data.mongodb.host= # the db host
spring.data.mongodb.port=27017 # the connection port (defaults to 27107)
spring.data.mongodb.uri=mongodb://localhost/test # connection URL
spring.data.mongodb.database=
spring.data.mongodb.authentication-database=
spring.data.mongodb.grid-fs-database=
spring.data.mongodb.username=
spring.data.mongodb.password=
spring.data.mongo.repositories.enabled=true # if spring data repository support is enabled
# JPA ({sc-spring-boot-autoconfigure}/orm/jpa/JpaBaseConfiguration.{sc-ext}[JpaBaseConfiguration], {sc-spring-boot-autoconfigure}/orm/jpa/HibernateJpaAutoConfiguration.{sc-ext}[HibernateJpaAutoConfiguration])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/
1 change: 1 addition & 0 deletions spring-boot-samples/spring-boot-sample-web-jsp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target/

0 comments on commit 670ba33

Please sign in to comment.