java - 无法配置 Java Spring Boot 数据 session Mongodb

标签 java mongodb spring-boot session

我一直在使用本指南通过 mongodb 设置 Spring session 数据

https://docs.spring.io/spring-session-data-mongodb/docs/2.1.1.RELEASE/reference/htmlsingle/#introduction

但是我在配置方面遇到问题。我将 Mongodb 与 Spring boot 结合使用,并尝试为 Spring boot Web 应用程序配置 session 时间和 session 名称,但它始终默认为 30 分钟,并且 mongodb 中的集合名称仍然是“sessions”

这些是我尝试过的:

将这些添加到 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 的 session 时间和集合名称。

经过几个小时的研究,似乎 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

这是java文件MongoHttpSessionConfiguration从 spring 开始,这是 spring 的自动配置;我尝试扩展 "MongoHttpSessionConfiguration" 并重写我自己的 setter 方法。例如 sessionTime 的 "setMaxInactiveIntervalInSeconds""setCollectionName" 为 mongododb 数据库集合名称。 但我遇到了这个错误:

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 session 。 session 始终默认为 30 分钟,并且集合名称在 mongodb 集合中始终为“sessions”。不确定如何更改 serverSelectionTimeout='30000 ms' 和 mongodb 集合名称“sessions”我不知道该怎么办,需要帮助。

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}

最佳答案

经过大量研究,最后,通过查看源代码,我找到了解决方案:

@EnableMongoHttpSession(maxInactiveIntervalInSeconds = 24 * 60 * 60)
public class SessionConfiguration {}

为了覆盖默认的集合名称,还有另一个注释属性collectionName

这适用于 Spring Boot 2.1.1

关于java - 无法配置 Java Spring Boot 数据 session Mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54854942/

相关文章:

java - 如何在Morphia Java中创建索引和不同的唯一索引?

python - pymongo - 搜索包含 $ 和运算符的文档集合,不区分大小写

javascript - Mongoose 的行为和模式

java - Spring集成(MQTT): Retrieving published message

java - 开发环境中的 Gradle 构建参数

java - 将 java.awt.Image 对象转换为字节数组 (`byte[]` )

Java 从 Mac/Linux Dock 隐藏应用程序

java - 使用尝试在 HashMap 中递归查找单词

spring-boot - 带 Spring 启动的 QAF cucumber testNG

java - 捕获异常后可以继续事务操作吗?