将 Redis/Spring Session 合并到我的应用程序中使我无法部署该应用程序
Incorporating Redis/Spring Session into my application prevents me from deploying the app
我正在尝试将 SpringSession 合并到我现有的(非 Spring 启动)应用程序中。我关注了 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 class:
public class Initializer extends AbstractHttpSessionApplicationInitializer {
public Initializer() {
super(SessionConfig.class);
}
}
我的理解是,这会创建一个有效拦截会话并使用 Redis 管理它们的 bean,而不是使用 Java In Memory 会话管理。
不过。我无法部署应用程序,并且在 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)
我由此得出的结论是,我现在包括的 classes(可能在 AbstractHttpSessionApplicationInitializer 中)正在尝试加载一个应用程序上下文,该上下文与已在我的 [ 中声明的 ContextLoaderListener 冲突=35=]。但是,如果我从 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]. 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]. 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.access1(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
到 bootstrap 网络应用程序,则不应使用 AbstractHttpSessionApplicationInitializer
,而应使用传统的 XML 基于配置的方法。
您可以查看 Redis HttpSession
with XML config guide Spring Session 的参考文档。
您的 Spring 配置应包含
我正在尝试将 SpringSession 合并到我现有的(非 Spring 启动)应用程序中。我关注了 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 class:
public class Initializer extends AbstractHttpSessionApplicationInitializer {
public Initializer() {
super(SessionConfig.class);
}
}
我的理解是,这会创建一个有效拦截会话并使用 Redis 管理它们的 bean,而不是使用 Java In Memory 会话管理。
不过。我无法部署应用程序,并且在 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)
我由此得出的结论是,我现在包括的 classes(可能在 AbstractHttpSessionApplicationInitializer 中)正在尝试加载一个应用程序上下文,该上下文与已在我的 [ 中声明的 ContextLoaderListener 冲突=35=]。但是,如果我从 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]. 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]. 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.access1(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
到 bootstrap 网络应用程序,则不应使用 AbstractHttpSessionApplicationInitializer
,而应使用传统的 XML 基于配置的方法。
您可以查看 Redis HttpSession
with XML config guide Spring Session 的参考文档。
您的 Spring 配置应包含