001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.data.mongo; 018 019import com.mongodb.reactivestreams.client.MongoClient; 020 021import org.springframework.boot.autoconfigure.AutoConfigureAfter; 022import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 023import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; 024import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 025import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 026import org.springframework.boot.autoconfigure.mongo.MongoProperties; 027import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration; 028import org.springframework.boot.context.properties.EnableConfigurationProperties; 029import org.springframework.context.annotation.Bean; 030import org.springframework.context.annotation.Configuration; 031import org.springframework.context.annotation.Import; 032import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory; 033import org.springframework.data.mongodb.core.ReactiveMongoTemplate; 034import org.springframework.data.mongodb.core.SimpleReactiveMongoDatabaseFactory; 035import org.springframework.data.mongodb.core.convert.MappingMongoConverter; 036import org.springframework.data.mongodb.core.convert.MongoConverter; 037import org.springframework.data.mongodb.core.convert.MongoCustomConversions; 038import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver; 039import org.springframework.data.mongodb.core.mapping.MongoMappingContext; 040 041/** 042 * {@link EnableAutoConfiguration Auto-configuration} for Spring Data's reactive mongo 043 * support. 044 * <p> 045 * Registers a {@link ReactiveMongoTemplate} bean if no other bean of the same type is 046 * configured. 047 * <P> 048 * Honors the {@literal spring.data.mongodb.database} property if set, otherwise connects 049 * to the {@literal test} database. 050 * 051 * @author Mark Paluch 052 * @since 2.0.0 053 */ 054@Configuration 055@ConditionalOnClass({ MongoClient.class, ReactiveMongoTemplate.class }) 056@ConditionalOnBean(MongoClient.class) 057@EnableConfigurationProperties(MongoProperties.class) 058@Import(MongoDataConfiguration.class) 059@AutoConfigureAfter(MongoReactiveAutoConfiguration.class) 060public class MongoReactiveDataAutoConfiguration { 061 062 private final MongoProperties properties; 063 064 public MongoReactiveDataAutoConfiguration(MongoProperties properties) { 065 this.properties = properties; 066 } 067 068 @Bean 069 @ConditionalOnMissingBean(ReactiveMongoDatabaseFactory.class) 070 public SimpleReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory( 071 MongoClient mongo) { 072 String database = this.properties.getMongoClientDatabase(); 073 return new SimpleReactiveMongoDatabaseFactory(mongo, database); 074 } 075 076 @Bean 077 @ConditionalOnMissingBean 078 public ReactiveMongoTemplate reactiveMongoTemplate( 079 ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory, 080 MongoConverter converter) { 081 return new ReactiveMongoTemplate(reactiveMongoDatabaseFactory, converter); 082 } 083 084 @Bean 085 @ConditionalOnMissingBean(MongoConverter.class) 086 public MappingMongoConverter mappingMongoConverter(MongoMappingContext context, 087 MongoCustomConversions conversions) { 088 MappingMongoConverter mappingConverter = new MappingMongoConverter( 089 NoOpDbRefResolver.INSTANCE, context); 090 mappingConverter.setCustomConversions(conversions); 091 return mappingConverter; 092 } 093 094}