1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| package com.winterchen.label.service.configuration;
import com.mongodb.MongoClient; import com.mongodb.MongoClientURI; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.MongoTransactionManager; import org.springframework.data.mongodb.config.AbstractMongoConfiguration; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver; import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper; import org.springframework.data.mongodb.core.convert.MappingMongoConverter; import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
@Configuration public class BusinessMongoConfig extends AbstractMongoConfiguration {
@Value("${spring.data.mongodb.uri}") private String uri;
@Value("${spring.data.mongodb.database}") private String database;
@Override protected String getDatabaseName() { return database; }
@Override public MongoClient mongoClient() { MongoClientURI mongoClientURI = new MongoClientURI(uri); return new MongoClient(mongoClientURI); }
@Primary @Bean("mongoMappingContext") public MongoMappingContext mongoMappingContext() { MongoMappingContext mappingContext = new MongoMappingContext(); return mappingContext; }
@Primary @Bean public MongoTransactionManager transactionManager(@Qualifier("mongoDbFactory") MongoDbFactory mongoDbFactory) throws Exception { return new MongoTransactionManager(mongoDbFactory); }
@Primary @Bean("mongoDbFactory") public MongoDbFactory mongoDbFactory() { return new SimpleMongoDbFactory(mongoClient(), getDatabaseName()); }
@Primary @Bean("mappingMongoConverter") public MappingMongoConverter mappingMongoConverter(@Qualifier("mongoDbFactory") MongoDbFactory mongoDbFactory, @Qualifier("mongoMappingContext") MongoMappingContext mongoMappingContext) throws Exception { DefaultDbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory); MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext); converter.setTypeMapper(new DefaultMongoTypeMapper(null)); return converter; }
@Primary @Bean(name = "mongoTemplate") public MongoTemplate getMongoTemplate(@Qualifier("mongoDbFactory") MongoDbFactory mongoDbFactory, @Qualifier("mappingMongoConverter") MappingMongoConverter mappingMongoConverter) throws Exception { return new MongoTemplate(mongoDbFactory, mappingMongoConverter); } }
|