redis初識

首先需要安裝redis服務器 其實原本是支持linux系統的版本的,但是可以在git上下載到windows redis 64位,我下載的是Redis-x64-3.0.504.解壓就能用,不需要安裝。

下載網址:https://github.com/MSOpenTech/redis/releases


cmd啓動redis服務器:redis-server.exe  redis.windows.conf


cmd啓動redis客戶端:redis-cli.exe -h 127.0.0.1 -p 6379   然後可以操作查看redis服務器上的數據了,其實redis服務器默認是16個庫,但是隻是方便歸集查看,並不能在性能上有所提升。
 


使用eclipse新建一個java web項目,在根目錄下建立pom.xml文件,然後對着項目右鍵configure->convent to maven project


window->preferences->Maven->user settings 配置settings.xml

對於settings.xml有兩個地方一個是maven安裝目錄下的conf中,一個是用戶目錄下.m2\settings.xml,是兩個settings.xml融合,優先級。


當時主要是爲了學習redis的使用

所以pom.xml文件簡單點

1. pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>redisSession</groupId>
    <artifactId>redisSession</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>

    </build>
    <dependencies>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.4.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
            <version>1.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.0.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.7.RELEASE</version>
        </dependency>
        
    </dependencies>
</project>

2.web-inf   下 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_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>redisSession</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/*applicationContext.xml
        </param-value>
    </context-param>
   <!-- springContext(WebApplicationContext) 初始化-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- delegatingFilterProxy springSessionRepositoryFilter 名字不能變,因爲spring-session實例化的bean叫springSessionRepositoryFilter-->
    <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>

</web-app>

3.web-inf   下  applicationContext.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:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.2.xsd
    http://www.springframework.org/schema/data/mongo
    http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

   <!-- 掃描註釋配置,實例化bean-->
    <context:annotation-config />

    <!-- Property Placeholder Configurer -->
    <bean id="config"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>/WEB-INF/redis.properties</value>
            </list>
        </property>
    </bean>

    <!-- 設置Cookie domain 和 名稱,瀏覽器cookie安全的原因 domainName path -->

    <!-- spring session使用封裝新的請求和響應,其中響應返回cookie到瀏覽器端的cookieName爲SESSION-->

    <bean id="defaultCookieSerializer"
        class="org.springframework.session.web.http.DefaultCookieSerializer">
        <property name="domainName" value=".example.com" />
        <property name="cookieName" value="SESSION" />
    </bean>

    <!-- redis -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    </bean>

    <bean id="jedisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}" />
        <property name="port" value="${redis.port}" />
        <!-- <property name="password" value="${redis.pass}" /> <property name="timeout"
            value="${redis.timeout}" /> -->
        <property name="poolConfig" ref="jedisPoolConfig" />
        <!-- <property name="usePool" value="true" /> -->
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
    </bean>

    <!-- 將session放入redis -->
    <bean id="redisHttpSessionConfiguration"
        class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <property name="maxInactiveIntervalInSeconds" value="1800" />
    </bean>
</beans>


備註延伸:

第一點:
<context:annotation-config/>與<context:component-scan base-package="pack.pack"/>的關係
其中一句話:使用<context:component-scan/>後,即可將<context:annotation-config/>省去


第二點:

在啓動項目時有時會報錯

spring :  
1.<!--   <context:annotation-config/> --> applicationContext
有時會報springSessionRepositoryFilter找不到名字是springSessionRepositoryFilter的bean
2.<!-- <context:component-scan
        base-package="org.springframework.web.filter.DelegatingFilterProxy">
  </context:component-scan> -->
加上都可以


4. web-inf  下redis.properties

redis.host=127.0.0.1
redis.port=6379


參考網頁:

http://blog.csdn.net/patrickyoung6625/article/details/45694157


測試jsp

index.jsp :

debug時發現request.getSession()得到的session 類型也變化了,是springSession中的session類型

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%=request.getSession().getId() %>
</body>
</html>

request.getSession().getId()在瀏覽器打開並且在一定時間內每次請求中每次的sessionId不會變化,而且在redis客戶端輸入KEY * 查詢到此sessionId

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章