Redis不可用时使用默认的Spring会话

时间:2018-06-18 21:30:45

标签: java spring spring-mvc session redis

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

当我的环境中没有Redis时,我无法找到恢复默认弹簧会话配置的正确方法。

我正在使用:

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

当我按如下方式扩展类时,我可以使用Redis会话(使用其默认配置):

public class RedisHttpSessionApplicationInitializer extends 
    AbstractHttpSessionApplicationInitializer {

}

当Redis不可用时,我尝试使用RedisHttpSessionApplicationInitializer的构造函数来加载SpringHttpSessionConfiguration,并按照文档中的说明进行操作 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

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

1 个答案:

答案 0 :(得分:0)

我能够解决的方法是使用CustomRedisHttpSessionConfiguration

@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 {
        //...
    }
}