spring-mvc - 将 Redis/Spring Session 合并到我的应用程序中会阻止我部署该应用程序

标签 spring-mvc redis spring-session

我正在尝试将 SpringSession 合并到我现有的(非 Spring boot)应用程序中。我关注了 Baeldung,这似乎最有意义: https://www.baeldung.com/spring-session

这是我的 Maven 依赖项:

    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session-data-redis</artifactId>
        <version>2.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>   

这是我创建的配置代码:

@Configuration
@EnableRedisHttpSession
public class SessionConfig{

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory jedisConnectionFactory = new 
JedisConnectionFactory();
        jedisConnectionFactory.setHostName("localhost");
        jedisConnectionFactory.setPort(6379);
        jedisConnectionFactory.setDatabase(1);
        return jedisConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }
}

下面是扩展 HttpSessionInitializer 的 Initializer 类:

public class Initializer extends AbstractHttpSessionApplicationInitializer {
    public Initializer() {
        super(SessionConfig.class);
    }
}

我的理解是,这会创建一个有效拦截 session 并使用 Redis 管理它们的 bean,而不是使用 Java In Memory session 管理。

但是。我无法部署应用程序,并且在 mt catalina 日志中收到以下错误:

07-Aug-2018 11:13:24.241 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener] java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml! at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:262) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4751) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5215) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734) at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:629) at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1839) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

我由此得出的结论是,我现在包含的类(可能在 AbstractHttpSessionApplicationInitializer 中)正在尝试加载与已在我的 web.xml 中声明的 ContextLoaderListener 冲突的应用程序上下文。但是,如果我从 web.xml 中删除该监听器,那么我的应用程序也将无法部署。

我的 tomcat 日志中也出现了这个异常,但不确定它是否相关:

07-Aug-2018 11:14:57.652 INFO [ektorp-idle-connection-monitor-thread-1] org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading Illegal access: this web application instance has been stopped already. Could not load [org.apache.http.pool.AbstractConnPool$4]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access. java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [org.apache.http.pool.AbstractConnPool$4]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access. at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForResourceLoading(WebappClassLoaderBase.java:1311) at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForClassLoading(WebappClassLoaderBase.java:1299) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1158) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119) at org.apache.http.pool.AbstractConnPool.closeExpired(AbstractConnPool.java:558) at org.apache.http.impl.conn.PoolingClientConnectionManager.closeExpiredConnections(PoolingClientConnectionManager.java:302) at org.ektorp.http.IdleConnectionMonitor$CleanupTask.run(IdleConnectionMonitor.java:52) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

我是否缺少任何可能导致此错误的内容?

最佳答案

如果您使用 web.xml 来引导您的网络应用程序,那么您不应该使用 AbstractHttpSessionApplicationInitializer,而应该使用传统的基于 XML 配置的方法。

可以看看Redis HttpSession with XML config guide可以从 Spring Session 的引用文档中获得。 你的 Spring 配置应该包含

关于spring-mvc - 将 Redis/Spring Session 合并到我的应用程序中会阻止我部署该应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51730315/

相关文章:

java - 为什么这个初始化和配置的 Dispatcher Servlet 不处理任何请求?

java - Spring中的自动缓存失效

python - 同一个 docker-compose 中的 Celery 和 Flask

java - Spring Session 2.0 Redis Lettuce 配置

spring-boot - Spring session : How to create separate session management policies for different URLs

jsp - 无法从 <spring :hasBindErrors> taglib 获得任何输出

Spring Security 5 身份验证总是返回 302

ruby-on-rails - 使用 Sidekiq 运行更复杂的作业

scala insert to redis 给出任务不可序列化

Spring Session Redis 和 Spring Security 如何更新用户 session ?