SpringData-Mongo基本配置类
# 起步依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
1
2
3
4
2
3
4
1
2
3
4
2
3
4
# MongoConfig
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.jetbrains.annotations.NotNull;
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.config.AbstractMongoClientConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* MongoDb配置
*/
@Configuration
public class MongoConfig extends AbstractMongoClientConfiguration {
@Autowired
private MongoProperties mongoProperties;
@NotNull
@Override
protected String getDatabaseName() {
return mongoProperties.getDatabase();
}
/**
* 配置MongoClient带有mongo工厂设置
* 用于mongoTemplate配置
*
* @return MongoClient
*/
@NotNull
@Override
public MongoClient mongoClient() {
ConnectionString connectionString = new ConnectionString("mongodb://" + mongoProperties.getHost());
MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
.applyConnectionString(connectionString)
// 设置Factory参数,默认keepAlive true
.applyToSocketSettings(builder -> {
builder.connectTimeout(5000, TimeUnit.MILLISECONDS);
builder.readTimeout(1500, TimeUnit.MILLISECONDS);
})
.applyToConnectionPoolSettings(builder -> {
// 最小连接数
builder.minSize(10);
// 最大连接数
builder.maxSize(500);
// 最长等待时间
builder.maxWaitTime(1500, TimeUnit.MILLISECONDS);
})
.build();
return MongoClients.create(mongoClientSettings);
}
/**
* 创建自定义ConverterList
*
* @return CustomConversions
*/
@Bean
public MongoCustomConversions addConverter() {
List<Converter<?, ?>> converterList = new ArrayList<>();
converterList.add(new YourConverter());
return new MongoCustomConversions(converterList);
}
/**
* 创建MongoTemplate
*
* @return MongoTemplate
*/
@Bean
public MongoTemplate mongoTemplate() {
MongoTemplate mongoTemplate = new MongoTemplate(mongoClient(), getDatabaseName());
MappingMongoConverter mongoMapping = (MappingMongoConverter) mongoTemplate.getConverter();
mongoMapping.setCustomConversions(addConverter());
mongoMapping.afterPropertiesSet();
return mongoTemplate;
}
}
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
82
83
84
85
86
87
88
89
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
82
83
84
85
86
87
88
89
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
82
83
84
85
86
87
88
89
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
82
83
84
85
86
87
88
89
编辑 (opens new window)
上次更新: 2023/06/03, 15:42:56
- 01
- SpringCache基本配置类05-16
- 03
- Rpamis-security-原理解析12-13