spring框架下集成spring-session

来源:互联网 发布:阀门选型软件 编辑:程序博客网 时间:2024/06/15 11:05

使用maven构建项目,pom.xml配置如下:

<dependencies>        <!-- spring-data-redis依赖的JAR配置start -->        <dependency>            <groupId>org.springframework.data</groupId>            <artifactId>spring-data-redis</artifactId>            <version>1.8.4.RELEASE</version>        </dependency>        <!-- spring-data-redis依赖的JAR配置end -->        <!-- jedis依赖的JAR配置start -->        <dependency>            <groupId>redis.clients</groupId>            <artifactId>jedis</artifactId>            <version>2.9.0</version>        </dependency>        <!-- jedis依赖的JAR配置end -->        <dependency>            <groupId>org.springframework.session</groupId>            <artifactId>spring-session</artifactId>            <version>1.3.1.RELEASE</version>        </dependency>        <!-- spring session end -->        <!-- Spring框架依赖的JAR配置start -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>4.3.9.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aop</artifactId>            <version>4.3.9.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>4.3.9.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-beans</artifactId>            <version>4.3.9.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-jdbc</artifactId>            <version>4.3.9.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-tx</artifactId>            <version>4.3.9.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>4.3.9.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>4.3.9.RELEASE</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-oxm</artifactId>            <version>4.3.9.RELEASE</version>        </dependency>        <!-- Spring框架依赖的JAR配置end -->    </dependencies>    <build>        <finalName>springsession</finalName>    </build>

我这里使用了spring框架,测试只用到了servlet,没有使用到springmvc框架.

applicationContext.xml:

<context:annotation-config/>    <!-- Jedis连接工厂 -->    <bean id="jedisConnectionFactory"        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">        <property name="hostName" value="192.168.3.18" />        <property name="port" value="6379" />        <property name="password" value="123456" />    </bean>    <bean id="redisHttpSessionConfiguration"class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">        <property name="maxInactiveIntervalInSeconds" value="1800" />    </bean>

上面的配置是建立redis的连接,将bean注入到spring容器中.
这里需要注意的是,在servlet类中使用session和以前使用方法没有变化.因为springsession重写了getsession方法.这里需要配置<context:annotation-config/>,因为使用到了spring容器中的bean,该配置就是激活spring容器中注册过的bean.

web.xml:

<filter>        <filtername>springSessionRepositoryFilter</filtername>        <filterclass>org.springframework.web.filter.DelegatingFilterProxy</filter-class>    </filter>    <filter-mapping>        <filter-name>springSessionRepositoryFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:applicationContext.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>

这里的过滤器最好写在其他过滤器的前面.

protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {        request.getSession().setAttribute("session", "gao");;        response.getWriter().append("ok");    }

我这里使用session的方法和以前的方式没有区别.

原创粉丝点击