Spring session 小例子运行

本文详细介绍了一个使用SpringSession和Redis进行会话管理的Maven项目案例,包括配置文件、依赖包、过滤器及Servlet设置等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文例子是Spring Session官网(http://docs.spring.io/spring-session/docs/current/reference/html5/#rest-spring-configuration)的HttpSession xml 例子(http://docs.spring.io/spring-session/docs/current/reference/html5/guides/httpsession-xml.html)修改而来,是一个Maven项目。

官网提供的例子使用gradle部署,并且它依赖的redis版本只在linux和macos下运行,所以需要修改它。

1.例子依赖的包。

<dependencies>
<!-- spring session 包-->
<dependency>
 <groupId>org.springframework.session</groupId>
 <artifactId>spring-session-data-redis</artifactId>
 <version>1.0.1.RELEASE</version>
 <type>pom</type>
 </dependency>
<!-- spring session 缺省使用redis保存session 需要依赖该包-->
 <dependency>
 <groupId>org.springframework.data</groupId>
 <artifactId>spring-data-redis</artifactId>
 <version>1.4.2.RELEASE</version>
 </dependency>
<!-- 嵌入式redis-->
 <dependency>
 <groupId>com.github.kstyrc</groupId>
 <artifactId>embedded-redis</artifactId>
 <version>0.6</version>
 </dependency>
<!-- spring web -->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-web</artifactId>
 <version>4.1.6.RELEASE</version>
 </dependency>

 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>4.1.6.RELEASE</version>
 </dependency>

 <dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>javax.servlet-api</artifactId>
 <version>3.1.0</version>
 </dependency>
<!-- 解析jsp标签-->
 <dependency>
 <groupId>jstl</groupId>
 <artifactId>jstl</artifactId>
 <version>1.2</version>
 </dependency>

 <dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.17</version>
 </dependency>

 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>
 </dependencies>


2.相关xml配置(session.xml)

<!-- tag::beans[] -->
 <bean class="org.springframework.session.redis.embedded.EmbeddedRedisConfiguration"/> <!--1-->
<!--2-->
 <context:annotation-config/>
 <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
<!--3-->
 <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:port="6379"/>
 <!-- end::beans[] -->

其中,

EmbeddedRedisConfiguration 用于配置嵌入式的RedisServer
RedisHttpSessionConfiguration 用于配置Spring Session, 实例化与Spring Session相关的bean。比如springSessionRepositoryFilter,拦截相关请求,以提供spring session操作
JedisConnectionFactory  Jedis redis java客户端开发包。工厂实现,redisTemplate依赖于它。RedisTemplate在RedisHttpSessionConfiguration实例化了。

3. web.xml配置

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</context-param>
<!-- end::context-param[] -->
<!-- tag::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>
<!-- end::springSessionRepositoryFilter[] -->
<!-- tag::listeners[] -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- end::listeners[] -->
<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>

这里配置了filter  springSessionRepositoryFilter 所有的请求都要通过这个filter。

SessionServlet 做了对Session的一些存取操作。

4.相关类说明

EmbeddedRedisConfiguration 

原本的类使用的redis版本不适合windows,所以下载了windows版本的redis包,并修改了代码

<span style="color:#333333;">@Bean
public RedisServerBean redisServer(){
        return new RedisServerBean();
}

class RedisServerBean implements InitializingBean, DisposableBean {
        private RedisServer redisServer;

        @Override
        public void afterPropertiesSet() throws Exception {
</span><span style="color:#ff0000;">                RedisExecProvider customProvider = RedisExecProvider.defaultProvider()
                .override(OS.WINDOWS, Architecture.x86_64, "D:\\redis2.8.21\\redis-server.exe");

                redisServer = new RedisServer(customProvider, 6379);
                redisServer = RedisServer.builder()
                .redisExecProvider(customProvider)
                .port(6379)
                .slaveOf("localhost", 6379)
                .configFile("D:\\redis2.8.21\\redis.windows.conf")
                .build();

                redisServer.start();</span><span style="color:#333333;">
        }

        @Override
        public void destroy() throws Exception {
                if(redisServer != null) {
                        redisServer.stop();
                }
        }
}</span>

由代码可知,我的redisserver包在D:\\redis2.8.21。


SessionServlet

获取页面输入的信息并保存到session中


5.页面


6.使用RedisClient连接redis查看

   可以看到已经保存到了redis里面



通过RedisClient 删除该session的一个sessionAttr信息,刷新页面也发生了更新,说明Spring session 每次都会去redis获取session。













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值