【发布时间】:2019-07-18 04:36:52
【问题描述】:
我一直在使用本指南使用 mongodb 设置 Spring 会话数据
但是我遇到了配置问题。我在 Spring Boot 中使用 Mongodb,我正在尝试为 Spring Boot Web 应用程序配置我的会话时间和会话名称,但它一直默认为 30 分钟,并且 mongodb 中的集合名称仍然是“会话”
这些是我尝试过的:
将这些添加到 application.properties:
server.session.timeout=1
spring.session.mongodb.collection-name=TestSESSIONS
还有这个
server.servlet.session.timeout=60s
spring.session.mongodb.collection-name=TestSESSIONS
这些配置都不起作用
我已经查看了这个URL 以了解 mongodb 的 spring 通用应用程序属性,但这些都没有帮助配置 mongodb 的会话时间和集合名称。
经过数小时的研究,似乎 Spring Boot 使用了某种自动配置与 "org.springframework.boot.autoconfigure"
然后我在我的 application.properties 中添加了这个
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
禁用自动配置。
但现在它只给我这个错误:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method mongoSessionRepository in org.springframework.session.data.mongo.config.annotation.web.http.MongoHttpSessionConfiguration required a bean of type 'org.springframework.data.mongodb.core.MongoOperations' that could not be found.
The following candidates were found but could not be injected:
- Bean method 'mongoTemplate' in 'MongoDataAutoConfiguration' not loaded because AnyNestedCondition 0 matched 2 did not; NestedCondition on MongoDataAutoConfiguration.AnyMongoClientAvailable.FallbackClientAvailable @ConditionalOnBean (types: com.mongodb.client.MongoClient; SearchStrategy: all) did not find any beans of type com.mongodb.client.MongoClient; NestedCondition on MongoDataAutoConfiguration.AnyMongoClientAvailable.PreferredClientAvailable @ConditionalOnBean (types: com.mongodb.MongoClient; SearchStrategy: all) did not find any beans of type com.mongodb.MongoClient
Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.data.mongodb.core.MongoOperations' in your configuration.
这是来自上面链接的 spring.io 指南 'mongoSessionConverter' 中的 @bean
这是来自 spring 的 java 文件 MongoHttpSessionConfiguration,它是 spring 的自动配置;我尝试过扩展"MongoHttpSessionConfiguration" 并覆盖我自己的setter 方法。比如"setMaxInactiveIntervalInSeconds" 代表 sessionTime 和
"setCollectionName" 用于 mongodb 数据库集合名称。
但我有这个错误:
Description:
The bean 'mongoSessionRepository', defined in class path resource [com/khatpass/app/config/SessionListenerConfig.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfiguration.class] and overriding is disabled.
我一直在尝试使用 Mongodb 配置 Spring Boot 会话。会话始终默认为 30 分钟,集合名称在 mongodb 集合中始终为“会话”。不知道如何更改 serverSelectionTimeout='30000 ms' 和 mongodb 集合名称“会话”我不知道该怎么做,需要帮助。
2019-02-24 13:39:54.501 INFO 36113 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
【问题讨论】:
-
你将这些配置写入哪里?在你从 spring 创建的 applications.properties 文件中?
-
在“/app/src/main/resources/application.properties”中是的
标签: java mongodb spring-boot session