今天来记录一下自己在整合框架过程中所遇到的问题:
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共享的过程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://blog.csdn.net/zengyunt/article/details/80605086