溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring整合redis(jedis)實現(xiàn)Session共享的過程

發(fā)布時間:2020-10-12 21:20:49 來源:腳本之家 閱讀:217 作者:Z濤子 欄目:編程語言

今天來記錄一下自己在整合框架過程中所遇到的問題:

1.    在用redis實現(xiàn)session共享時,項目啟動報 No bean named 'springSessionRepositoryFilter' is defined 異常

2.    在調用緩存工具類的時候顯示注入的JedisPool為Null (一個跟spring掃描有關的細節(jié)錯誤)

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

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監(jiān)聽器 --> 
 <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> 
  <!--數(shù)據(jù)庫設置--> 
  <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"/> 
    <!-- 連接池最大使用連接數(shù)量 --> 
    <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"/> 
    <!-- 監(jiān)控數(shù)據(jù)庫 --> 
    <!-- <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)實現(xiàn)Session共享的過程

Spring整合redis(jedis)實現(xiàn)Session共享的過程

總結

以上所述是小編給大家介紹的Spring整合redis(jedis)實現(xiàn)Session共享的過程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI