-
Notifications
You must be signed in to change notification settings - Fork 0
/
MongoConfiguration.java
62 lines (53 loc) · 2.4 KB
/
MongoConfiguration.java
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
package in.neuw.example.config;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import in.neuw.example.utils.DateToZonedDateTimeConverter;
import in.neuw.example.utils.ZonedDateTimeToDateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory;
import org.springframework.data.mongodb.ReactiveMongoTransactionManager;
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
import org.springframework.data.mongodb.config.EnableMongoAuditing;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.gridfs.ReactiveGridFsTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import java.util.ArrayList;
import java.util.List;
/**
* @author Karanbir Singh on 05/07/2020
*/
@Configuration
@EnableMongoAuditing(dateTimeProviderRef = "dateTimeProvider")
@EnableTransactionManagement
public class MongoConfiguration extends AbstractMongoClientConfiguration {
@Autowired
private MongoProperties mongoProperties;
@Bean
ReactiveMongoTransactionManager transactionManager(ReactiveMongoDatabaseFactory dbFactory) {
return new ReactiveMongoTransactionManager(dbFactory);
}
@Override
protected String getDatabaseName() {
return mongoProperties.getDatabase();
}
@Bean
public MongoCustomConversions customConversions() {
List<Converter<?, ?>> converters = new ArrayList<>();
converters.add(new DateToZonedDateTimeConverter());
converters.add(new ZonedDateTimeToDateConverter());
return new MongoCustomConversions(converters);
}
@Bean
public ReactiveGridFsTemplate reactiveGridFsTemplate(ReactiveMongoDatabaseFactory reactiveMongoDbFactory, MappingMongoConverter mappingMongoConverter) {
return new ReactiveGridFsTemplate(reactiveMongoDbFactory, mappingMongoConverter);
}
@Override
public MongoClient mongoClient() {
return MongoClients.create(mongoProperties.getUri());
}
}