首页 > 解决方案 > 无法配置 Java Spring Boot 数据会话 Mongodb

问题描述

我一直在使用本指南使用 mongodb 设置春季会话数据

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

但是我在配置方面遇到问题。我在 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.

'mongoSessionConverter'这是来自上述链接的 spring.io 指南中的 @bean

这是来自 spring 的 java 文件MongoHttpSessionConfiguration,它是 spring 的自动配置;我已经尝试过扩展"MongoHttpSessionConfiguration"和覆盖我自己的 setter 方法。如"setMaxInactiveIntervalInSeconds"for sessionTime 和 "setCollectionName"for 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 会话。会话始终默认为 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}

标签: javamongodbspring-bootsession

解决方案


在查看了MongoOperationsSessionRepository类之后,org.springframework.session.data.mongo它似乎无法配置,application.properties因为该类使用的是静态最终值

public static final int DEFAULT_INACTIVE_INTERVAL = 1800;

public static final String DEFAULT_COLLECTION_NAME = "sessions";

更改值的唯一方法是在对象保存之前拦截它。这些字段没有 getter 或 setter,不能轻易改变,真是个笑话!


推荐阅读