gpt4 book ai didi

java - 当 Redis 不可用时使用默认的 Spring session

转载 作者:可可西里 更新时间:2023-11-01 11:24:32 25 4
gpt4 key购买 nike

上下文:我正在尝试使用 Spring 的 Redis session 在云上跨多个 Tomcat 保存 session 。但是在某些环境中,Redis 将不可用。我想配置一种方法来允许我的应用程序根据运行时的环境返回到默认实现。

当 Redis 在我的环境中不可用时,我无法想出恢复默认 spring session 配置的正确方法。

我正在使用:

<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>

当我如下扩展类时,我能够使用 Redis session (使用其默认配置):

public class RedisHttpSessionApplicationInitializer extends 
AbstractHttpSessionApplicationInitializer {

}

我尝试使用我的 RedisHttpSessionApplicationInitializer 的构造函数来加载 SpringHttpSessionConfiguration 而不是当 Redis 不可用时并遵循文档在 SpringHttpSessionConfigurationEnableSpringHttpSession .

所以我做了如下修改:

public class RedisHttpSessionApplicationInitializer extends 
AbstractHttpSessionApplicationInitializer {
public RedisHttpSessionApplicationInitializer(){
super(SpringHttpSessionConfiguration.class);
}
}

和:

@Bean
public SessionRepository<MapSession> sessionRepository() {
return new MapSessionRepository(new HashMap<>());
}

我仍然收到如下错误:

ERROR 921  [Thread 19] org.springframework.web.context.ContextLoader - 
Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'springSessionRepositoryFilter' defined
in org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration: Unsatisfied dependency expressed through method
'springSessionRepositoryFilter' parameter 0; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
'org.springframework.session.SessionRepository<?>' available: expected
at least 1 bean which qualifies as autowire candidate. Dependency
annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:458)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1249)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1098)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBea

您能建议我为我的用例尝试任何东西吗?另外,如果您需要我提供更多信息,请告诉我。

最佳答案

我能够解决的方法是使用Custom RedisHttpSessionConfiguration

@Configuration
public class CustomRedisHttpSessionConfiguration extends RedisHttpSessionConfiguration {

@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
if (isRedisPresent()) {
// Do something
}else{
// Do Something else
}
}

}

此外,为了初始化我的应用程序,我使用了以下内容:

public class RedisHttpSessionApplicationInitializer extends AbstractHttpSessionApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//...
}
}

关于java - 当 Redis 不可用时使用默认的 Spring session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50917743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com