亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2018年06月07日 14:20:55   作者:Z濤子  
這篇文章主要介紹了Spring整合redis(jedis)實(shí)現(xiàn)Session共享,需要的朋友可以參考下

今天來(lái)記錄一下自己在整合框架過(guò)程中所遇到的問(wèn)題:

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過(guò)濾器 放在過(guò)濾器頭--> 
 <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> 
 <!-- 編碼過(guò)濾器 --> 
 <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)鍵

問(wèn)題總結(jié)

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

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

例如:

總結(jié)

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

相關(guān)文章

  • 將idea工程打包成jar文件的全步驟

    將idea工程打包成jar文件的全步驟

    這篇文章主要給大家介紹了關(guān)于將idea工程打包成jar文件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Springboot項(xiàng)目異常處理及返回結(jié)果統(tǒng)一

    Springboot項(xiàng)目異常處理及返回結(jié)果統(tǒng)一

    這篇文章主要介紹了Springboot項(xiàng)目異常處理及返回結(jié)果統(tǒng)一,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-08-08
  • Springboot如何解決前端請(qǐng)求跨域的問(wèn)題

    Springboot如何解決前端請(qǐng)求跨域的問(wèn)題

    這篇文章主要介紹了Springboot如何解決前端請(qǐng)求跨域的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 如何在Spring中自定義scope的方法示例

    如何在Spring中自定義scope的方法示例

    這篇文章主要介紹了如何在Spring中自定義scope的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • 帶你快速入門掌握Spring的那些注解使用

    帶你快速入門掌握Spring的那些注解使用

    注解是個(gè)好東西,注解是Java語(yǔ)法,被Java編譯器檢查,可以減少配置錯(cuò)誤,這篇文章主要給大家介紹了關(guān)于Spring的那些注解使用的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • java二叉樹的數(shù)據(jù)插入算法介紹

    java二叉樹的數(shù)據(jù)插入算法介紹

    大家好,本篇文章主要講的是java二叉樹的數(shù)據(jù)插入算法介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • 詳解Java如何優(yōu)雅的使用策略模式

    詳解Java如何優(yōu)雅的使用策略模式

    設(shè)計(jì)模式是軟件設(shè)計(jì)中常見問(wèn)題的典型解決方案。 它們就像能根據(jù)需求進(jìn)行調(diào)整的預(yù)制藍(lán)圖, 可用于解決代碼中反復(fù)出現(xiàn)的設(shè)計(jì)問(wèn)題。今天就拿其中一個(gè)問(wèn)題來(lái)分析如何優(yōu)雅的使用策略模式吧
    2023-02-02
  • Java網(wǎng)絡(luò)編程之簡(jiǎn)單的服務(wù)端客戶端應(yīng)用實(shí)例

    Java網(wǎng)絡(luò)編程之簡(jiǎn)單的服務(wù)端客戶端應(yīng)用實(shí)例

    這篇文章主要介紹了Java網(wǎng)絡(luò)編程之簡(jiǎn)單的服務(wù)端客戶端應(yīng)用,以實(shí)例形式較為詳細(xì)的分析了java網(wǎng)絡(luò)編程的原理與服務(wù)器端客戶端的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • MybatisPlus保存、讀取MySQL中的json字段失敗問(wèn)題及解決

    MybatisPlus保存、讀取MySQL中的json字段失敗問(wèn)題及解決

    這篇文章主要介紹了MybatisPlus保存、讀取MySQL中的json字段失敗問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • springboot vue完成發(fā)送接口請(qǐng)求顯示響應(yīng)頭信息

    springboot vue完成發(fā)送接口請(qǐng)求顯示響應(yīng)頭信息

    這篇文章主要為大家介紹了springboot+vue完成發(fā)送接口請(qǐng)求顯示響應(yīng)頭信息,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05

最新評(píng)論