一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - Spring整合redis(jedis)實現Session共享的過程

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

2021-05-08 10:33Z濤子 Java教程

這篇文章主要介紹了Spring整合redis(jedis)實現Session共享,需要的朋友可以參考下

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

1.    在用redis實現session共享時,項目啟動報 no bean named 'springsessionrepositoryfilter' is defined 異常

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

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

pom.xml依賴jar包

?
1
2
3
4
5
6
7
8
9
10
11
<!-- 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配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?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配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?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

?
1
2
3
4
5
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

?
1
2
3
4
5
6
7
8
9
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共享的過程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:https://blog.csdn.net/zengyunt/article/details/80605086

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日日操日日舔 | 小辣椒精品福利视频导航 | 日韩成人精品在线 | 99久久综合| 婷婷综合缴情亚洲五月伊 | 2019中文字幕在线视频 | 四虎网址大全 | 亚洲欧美日韩中文字幕久久 | 国产欧美日韩专区毛茸茸 | 扒开腚眼子视频大全 | 美女被绑着吸下部的故事 | 三级黄色片在线免费观看 | 欧美精品一区二区三区免费观看 | 亚洲国产成人久久精品影视 | 学生小泬无遮挡女HD | 国产午夜久久精品 | 亚洲精品短视频 | 欧美精品日韩一区二区三区 | 99久久免费国产精品热 | 性xxx免费视频 | 精品久久香蕉国产线看观看麻豆 | 亚洲AV永久无码精品老司机蜜桃 | 日韩精品 欧美 | 门卫老张和女警花小说 | 亚洲一欧洲中文字幕在线 | 日本一区三区 | 免费在线观看亚洲 | 亚洲精品国产自在现线最新 | 色婷婷在线 | 欧美亚洲综合另类 | 无人影院在线播放视频 | 亚洲六月丁香六月婷婷色伊人 | 亚洲欧美韩国日产综合在线 | 天天操网| jazz中国在线视频 | 国产一精品一av一免费爽爽 | 91桃色污 | 无遮无挡免费视频 | 日本亚洲免费 | 欧美一区不卡二区不卡三区 | 国产成人高清精品免费5388密 |