欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-20 12:59:22
今天来记录一下自己在整合框架过程中所遇到的问题: 1.    在用redis实现session共享时,项目启动报 no bean named...

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

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

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

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

pom.xml依赖jar包

<!-- spring session begin --> 
  <dependency> 
   <groupid>redis.clients</groupid> 
   <artifactid>jedis</artifactid> 
   <version>2.9.0</version> 
  </dependency> 
  <dependency> 
   <groupid>org.springframework.session</groupid> 
   <artifactid>spring-session-data-redis</artifactid> 
   <version>1.2.1.release</version> 
  </dependency> 

web.xml配置

<?xml version="1.0" encoding="utf-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
     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"> 
 <context-param> 
  <param-name>contextconfiglocation</param-name> 
  <param-value>classpath:spring-cfg.xml</param-value> 
 </context-param> 
 <!--session过滤器 放在过滤器头--> 
 <filter> 
  <filter-name>springsessionrepositoryfilter</filter-name> 
  <filter-class>org.springframework.web.filter.delegatingfilterproxy</filter-class> 
 </filter> 
 <filter-mapping> 
  <filter-name>springsessionrepositoryfilter</filter-name> 
  <url-pattern>/*</url-pattern> 
 </filter-mapping> 
 <!-- 编码过滤器 --> 
 <filter> 
  <filter-name>encodingfilter</filter-name> 
  <filter-class>org.springframework.web.filter.characterencodingfilter</filter-class> 
  <async-supported>true</async-supported> 
  <init-param> 
   <param-name>encoding</param-name> 
   <param-value>utf-8</param-value> 
  </init-param> 
 </filter> 
 <filter-mapping> 
  <filter-name>encodingfilter</filter-name> 
  <url-pattern>/*</url-pattern> 
 </filter-mapping> 
 <!-- spring监听器 --> 
 <listener> 
  <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> 
 </listener> 
 <!-- spring mvc--> 
 <servlet> 
  <servlet-name>springmvc</servlet-name> 
  <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> 
  <init-param> 
   <param-name>contextconfiglocation</param-name> 
   <param-value>classpath:spring-mvc.xml</param-value> 
  </init-param> 
  <load-on-startup>1</load-on-startup> 
  <async-supported>true</async-supported> 
 </servlet> 
 <servlet-mapping> 
  <servlet-name>springmvc</servlet-name> 
  <url-pattern>/</url-pattern> 
 </servlet-mapping> 
 <!-- <servlet-mapping> 
  <servlet-name>default</servlet-name> 
  <url-pattern>/static/*</url-pattern> 
 </servlet-mapping>--> 
</web-app> 

spring-cfg.xml配置文件

<?xml version="1.0" encoding="utf-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemalocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.0.xsd 
  http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
  http://www.springframework.org/schema/util 
  http://www.springframework.org/schema/util/spring-util.xsd" 
> 
  <!--开启切面编程自动代理--> 
  <aop:aspectj-autoproxy proxy-target-class="true"/> 
  <!--扫描注解生成bean--> 
  <context:annotation-config/> 
  <!--包扫描--> 
  <context:component-scan base-package="com.zyt"> 
      <context:exclude-filter type="annotation" expression="org.springframework.stereotype.controller"/> 
  </context:component-scan> 
  <!--读取多个properties配置文件--> 
  <bean id="propertyconfigurer" 
     class="org.springframework.beans.factory.config.propertyplaceholderconfigurer"> 
    <property name="locations"> 
      <list> 
        <value>classpath:jdbc.properties</value> 
        <value>classpath:redis.properties</value> 
      </list> 
    </property> 
  </bean> 
  <!-- jedis连接池 --> 
  <bean id="poolconfig" class="redis.clients.jedis.jedispoolconfig"> 
    <property name="maxidle" value="${redis.maxidle}"/> 
    <property name="maxtotal" value="${redis.maxactive}"/> 
    <property name="maxwaitmillis" value="${redis.maxwait}"/> 
    <property name="testonborrow" value="${redis.testonborrow}"/> 
  </bean> 
  <!-- redis的连接池pool,不是必选项:timeout/password --> 
  <bean id = "jedispool" class="redis.clients.jedis.jedispool"> 
    <constructor-arg index="0" ref="poolconfig"/> 
    <constructor-arg index="1" value="${redis.host}"/> 
    <constructor-arg index="2" value="${redis.port}" type="int"/> 
    <constructor-arg index="3" value="${redis.timeout}" type="int"/> 
    <!-- <constructor-arg index="4" value="${redis.password}"/>--> 
  </bean> 
  <!-- jedis连接工厂 --> 
  <bean id="jedisconnectionfactory" 
     class="org.springframework.data.redis.connection.jedis.jedisconnectionfactory"> 
    <property name="poolconfig" ref="poolconfig"/> 
    <property name="port" value="${redis.port}"/> 
    <property name="hostname" value="${redis.host}"/> 
    <!-- <property name="password" value="${redis.pass}"/>--> 
  </bean> 
  <util:constant static-field="org.springframework.session.data.redis.config.configureredisaction.no_op"/> 
  <!-- spring redis template --> 
  <bean id="redistemplate" class="org.springframework.data.redis.core.stringredistemplate"> 
    <property name="connectionfactory" ref="jedisconnectionfactory"/> 
  </bean> 
  <!-- redis end --> 
  <!-- spring session begin --> 
  <bean id="redishttpsessionconfiguration" 
     class="org.springframework.session.data.redis.config.annotation.web.http.redishttpsessionconfiguration"> 
    <property name="maxinactiveintervalinseconds" value="1800"/> 
  </bean> 
  <!--整合mybatis--> 
  <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> 
    <property name="datasource" ref="datasource"/> 
    <property name="mapperlocations" value="classpath:com/zyt/**/**.xml"/> 
  </bean> 
  <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> 
    <property name="basepackage" value="com.zyt.*.dao"/> 
    <property name="sqlsessionfactorybeanname" value="sqlsessionfactory"/> 
  </bean> 
  <!--声明事务管理 采用注解方式--> 
  <tx:annotation-driven transaction-manager="transactionmanager"/> 
  <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> 
    <property name="datasource" ref="datasource"/> 
  </bean> 
  <!--数据库设置--> 
  <bean id="datasource" class="com.alibaba.druid.pool.druiddatasource" 
     destroy-method="close" init-method="init"> 
    <property name="url" value="${jdbc_url}"/> 
    <property name="username" value="${jdbc_username}"/> 
    <property name="password" value="${jdbc_password}"/> 
    <!-- 初始化连接大小 --> 
    <property name="initialsize" value="0"/> 
    <!-- 连接池最大使用连接数量 --> 
    <property name="maxactive" value="20"/> 
    <!-- 连接池最小空闲 --> 
    <property name="minidle" value="0"/> 
    <!-- 获取连接最大等待时间 --> 
    <property name="maxwait" value="60000"/> 
    <!-- 
    <property name="poolpreparedstatements" value="true" /> 
    <property name="maxpoolpreparedstatementperconnectionsize" value="33" /> 
    --> 
    <property name="validationquery" value="${validationquery}"/> 
    <property name="testonborrow" value="false"/> 
    <property name="testonreturn" value="false"/> 
    <property name="testwhileidle" value="true"/> 
    <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> 
    <property name="timebetweenevictionrunsmillis" value="60000"/> 
    <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> 
    <property name="minevictableidletimemillis" value="25200000"/> 
    <!-- 打开removeabandoned功能 --> 
    <property name="removeabandoned" value="true"/> 
    <!-- 1800秒,也就是30分钟 --> 
    <property name="removeabandonedtimeout" value="1800"/> 
    <!-- 关闭abanded连接时输出错误日志 --> 
    <property name="logabandoned" value="true"/> 
    <!-- 监控数据库 --> 
    <!-- <property name="filters" value="stat" /> --> 
    <property name="filters" value="mergestat"/> 
  </bean> 
</beans> 

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共享的过程

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

总结

以上所述是小编给大家介绍的spring整合redis(jedis)实现session共享的过程,希望对大家有所帮助

上一篇:

下一篇: