Spring整合redis(jedis)实现Session共享的过程

网友投稿 663 2023-01-30


Spring整合redis(jedis)实现Session共享的过程

今天来记录一下自己在整合框架过程中所遇到的问题:

1.    在用redis实现session共享时,项目启动报 No bean named 'springSessionRepositoryFilter' is defined 异常

2.    在调用缓存工具类的时候显示注入的JedisPool为Null (一个跟spring扫描有关的细节错误)

好了,开始上我整合的文件了

pom.xml依赖jar包

redis.clients

jedis

2.9.0

org.springframework.session

spring-session-data-rutytTJedis

1.2.1.RELEASE

web.xml配置

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

contextConfigLocation

classpath:spring-cfg.xml

springSessionRepositoryFilter

org.springframework.web.filter.DelegatingFilterProxy

springSessionRepositoryFilter

/*

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

true

encoding

UTF-8

encodingFilter

/*

org.springframework.web.context.ContextLoaderListener

SpringMVC

org.springframework.web.servutytTJlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

1

true

SpringMVC

/

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

contextConfigLocation

classpath:spring-cfg.xml

springSessionRepositoryFilter

org.springframework.web.filter.DelegatingFilterProxy

springSessionRepositoryFilter

/*

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

true

encoding

UTF-8

encodingFilter

/*

org.springframework.web.context.ContextLoaderListener

SpringMVC

org.springframework.web.servutytTJlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

1

true

SpringMVC

/

spring-cfg.xml配置文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:util="http://springframework.org/schema/util"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-4.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util.xsd"

>

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

classpath:jdbc.properties

classpath:redis.properties

class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">

destroy-method="close" init-method="init">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx" xmlns:util="http://springframework.org/schema/util"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-4.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/util

http://springframework.org/schema/util/spring-util.xsd"

>

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

classpath:jdbc.properties

classpath:redis.properties

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

classpath:jdbc.properties

classpath:redis.properties

class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">

class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">

destroy-method="close" init-method="init">

destroy-method="close" init-method="init">

jdbc.properties

driverClassName=com.mysql.jdbc.Driver

validationQuery=SELECT 1

jdbc_url=jdbc:mysql://localhost:3306/zyt_demo?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull

jdbc_username=root

jdbc_password=root

redis.properties

redis.isopen=on

redis.host=127.0.0.1

redis.port=6379

redis.maxIdle=300

redis.maxActive=600

redis.maxWait=1000

redis.testOnBorrow=true

redis.timeout=2000

#redis.password=

以上是整合的配置文件,其中有关redis的配置是整合成功的关键

问题总结

1.之前整合完启动项目报异常,是因为配置文件放置的位置问题,以至于启动不成功,多试几遍,以上的配置文件是可以用的

2.之前调用缓存工具类,显示注入JedisPool为空,在controller那边注入又有值,是因为我在controller那边调用工具类的方式是new出来的,所以导致spring在扫描那个工具类时丢失Jedispool注入,在controller中改用注入工具类的形式即可解决

例如:

总结

以上所述是给大家介绍的Spring整合redis(jedis)实现Session共享的过程,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:共享文件系统扩容(文件共享存储)
下一篇:java做接口自动化测试(java接口自动化测试黑马程序员)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~