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

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

創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括渠縣網(wǎng)站建設(shè)、渠縣網(wǎng)站制作、渠縣網(wǎng)頁(yè)制作以及渠縣網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,渠縣網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到渠縣省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

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

2.    在調(diào)用緩存工具類的時(shí)候顯示注入的JedisPool為Null (一個(gè)跟spring掃描有關(guān)的細(xì)節(jié)錯(cuò)誤)

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

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" 
> 
  <!--開啟切面編程自動(dòng)代理--> 
  <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> 
  <!--讀取多個(gè)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,不是必選項(xiàng):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> 
  <!--聲明事務(wù)管理 采用注解方式--> 
  <tx:annotation-driven transaction-manager="transactionManager"/> 
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dataSource"/> 
  </bean> 
  <!--數(shù)據(jù)庫(kù)設(shè)置--> 
  <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"/> 
    <!-- 獲取連接最大等待時(shí)間 --> 
    <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"/> 
    <!-- 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 --> 
    <property name="timeBetweenEvictionRunsMillis" value="60000"/> 
    <!-- 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 --> 
    <property name="minEvictableIdleTimeMillis" value="25200000"/> 
    <!-- 打開removeAbandoned功能 --> 
    <property name="removeAbandoned" value="true"/> 
    <!-- 1800秒,也就是30分鐘 --> 
    <property name="removeAbandonedTimeout" value="1800"/> 
    <!-- 關(guān)閉abanded連接時(shí)輸出錯(cuò)誤日志 --> 
    <property name="logAbandoned" value="true"/> 
    <!-- 監(jiān)控?cái)?shù)據(jù)庫(kù) --> 
    <!-- <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= 

以上是整合的配置文件,其中有關(guān)redis的配置是整合成功的關(guān)鍵

問題總結(jié)

1.之前整合完啟動(dòng)項(xiàng)目報(bào)異常,是因?yàn)榕渲梦募胖玫奈恢脝栴},以至于啟動(dòng)不成功,多試幾遍,以上的配置文件是可以用的

2.之前調(diào)用緩存工具類,顯示注入JedisPool為空,在controller那邊注入又有值,是因?yàn)槲以赾ontroller那邊調(diào)用工具類的方式是new出來的,所以導(dǎo)致spring在掃描那個(gè)工具類時(shí)丟失Jedispool注入,在controller中改用注入工具類的形式即可解決

例如:

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

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

總結(jié)

以上所述是小編給大家介紹的Spring整合redis(jedis)實(shí)現(xiàn)Session共享的過程,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!

分享名稱:Spring整合redis(jedis)實(shí)現(xiàn)Session共享的過程
文章路徑:http://bm7419.com/article2/ijhooc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、App設(shè)計(jì)、外貿(mào)建站、自適應(yīng)網(wǎng)站、商城網(wǎng)站、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化