Контекст: Я пытаюсь использовать сеанс 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 {
}
Я попытался использовать конструктор своего RedisHttpSessionApplicationInitializer для загрузки SpringHttpSessionConfiguration вместо этого, когда Redis недоступен, и следил за документами в
SpringHttpSessionConfiguration и EnableSpringHttpSession.
Итак, я внес следующие изменения:
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
Не могли бы вы предложить мне попробовать что-нибудь в моем случае? Кроме того, дайте мне знать, если вам потребуется от меня дополнительная информация.




Мне удалось обойтись без 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 {
//...
}
}
@Matheus Lacerda Спасибо за исправление грамматики