`

spring-session之2 依葫芦画瓢做个小项目

阅读更多

spring-session之2 依葫芦画瓢做个小项目

前文,我们聊了下 初探 spring-session, 不过毕竟代码是别人的, 我们自己的项目要是想用spring-session 那么该怎么用呢?

那么我们先做个小小的例子吧,今天我们先定个小目标,比如先....,额...

今天的目标是:

  1. 建一个 maven war 项目,里面包含简单的 servlet(你们想弄成springmvc还是 struts 随意),
  2. 然后加上 spring-session 的配置(基于redis),
  3. 最后使用 maven tomcat plugin启动

1.创建个 maven war 项目

大致信息

1.1 项目结构:

1.2 web.xml:

1.3 sample.SessionServlet:

1.4 index.jsp:

1.5 项目启动:

表示启动没有问题,那么我们可以继续下一步

2.加入 spring-session 的配置

2.1.依赖jar

要想使用spring-session,首先需要 jar包 (此处以maven依赖为例)

首先,需要依赖 spring-session

        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
            <version>${version.spring-session}</version>
        </dependency>

其次,我们还需要使用适配的会话存储,官方提供了 spring-session-jdbc spring-session-data-mongo spring-session-data-redisspring-session-data-gemfire,可以根据不同的场景而择一使用

我们使用的是 spring-session-data-redis

        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>${version.spring-session}</version>
        </dependency>

完整依赖:

        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
            <version>${version.spring-session}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>${version.spring-session}</version>
        </dependency>

        <!---->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${version.spring4}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${version.spring4}</version>
        </dependency>

        <!-- servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

2.2 加入 spring-session 的配置文件

2.2.1 web.xml 增加 spring-session.xml 配置文件以及 springSessionRepositoryFilter

完整代码如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">


        <!-- spring-session config -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath*:spring-session.xml
            </param-value>
        </context-param>

        <!-- 这个filter 要放在第一个 -->
        <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>
            <dispatcher>REQUEST</dispatcher>
            <dispatcher>ERROR</dispatcher>
        </filter-mapping>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

        <!-- ************************************************ -->
        <servlet>
            <servlet-name>session</servlet-name>
            <servlet-class>sample.SessionServlet</servlet-class>
        </servlet>

        <servlet-mapping>
            <servlet-name>session</servlet-name>
            <url-pattern>/session</url-pattern>
        </servlet-mapping>

        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

2.2.2 新增 spring-session.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:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

        <context:annotation-config />

        <!-- RedisHttpSessionConfiguration -->
        <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration" />

        <!--JedisConnectionFactory -->
        <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" />
    </beans>

说明:

  • 这里使用 redis 存储,默认是 localhost ,端口号 6379 ,下一节我们聊聊怎么修改配置

3.启动

mvn tomcat7:run

效果:

redis 服务端显示信息:

表明已经使用了 redis 来

至此,我们今天开头的3个小目标已经完成,我们可以开始新的旅程,各位系好安全带~~

4.附录

4.1 版本说明

spring-session 有这么多版本,我该使用哪个版本? 是越新越好吗? spring-session 依赖的spring版本是多少?

  • spring-session 1.2系列 依赖的是spring 4.2.5.RELEASE
  • spring-session 1.1系列 依赖的是spring 4.1.6.RELEASE
  • spring-session 1.0系列 依赖的是spring 4.1.3.RELEASE

也就是说 spring-session 至少需要 spring 4.1+以上的版本,这里我们使用 spring-session 1.2.2.RELEASE 的版本

下面是完整的 依赖关系

[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ feilong-spring-session-example ---
[INFO] com.feilong.incubator:feilong-spring-session-example:war:1.8.9-SNAPSHOT

[INFO] +- org.springframework.session:spring-session:jar:1.2.2.RELEASE:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.2:compile

[INFO] +- org.springframework.session:spring-session-data-redis:jar:1.2.2.RELEASE:compile
[INFO] |  +- org.apache.commons:commons-pool2:jar:2.4.2:compile
[INFO] |  +- org.springframework.data:spring-data-redis:jar:1.7.1.RELEASE:compile
[INFO] |  |  +- org.springframework.data:spring-data-keyvalue:jar:1.1.1.RELEASE:compile
[INFO] |  |  |  +- org.springframework.data:spring-data-commons:jar:1.12.1.RELEASE:compile
[INFO] |  |  |  \- org.springframework:spring-context:jar:3.2.17.RELEASE:compile
[INFO] |  |  |     \- org.springframework:spring-expression:jar:3.2.17.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-tx:jar:3.2.17.RELEASE:compile
[INFO] |  |  |  +- org.springframework:spring-beans:jar:3.2.17.RELEASE:compile
[INFO] |  |  |  \- org.springframework:spring-core:jar:3.2.17.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-oxm:jar:3.2.17.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-aop:jar:3.2.17.RELEASE:compile
[INFO] |  |  |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  |  \- org.springframework:spring-context-support:jar:3.2.17.RELEASE:compile
[INFO] |  \- redis.clients:jedis:jar:2.8.1:compile

可以看出所依赖的 spring-data-redis 依赖的是 spring 3.2.17.RELEASE 版本 ,这里的关系有点乱

5.参考

--to be continued

0
0
分享到:
评论
1 楼 lzbcrs 2016-11-28  
能把 pom.xml 完整贴出来吗

相关推荐

Global site tag (gtag.js) - Google Analytics