Spring Session Redis集成(一)

Spring Session Redis集成(一)

spring session的介绍在这就不详细讲解了,有兴趣的朋友可以查看这篇博文:

http://blog.csdn.net/admin1973/article/details/56485730

在这里就直接贴出代码,让大家以更快的方式搭建好Spring session redis环境

1.新建一个web项目gradle配置:

dependencies {
    compile(
            "org.springframework.session:spring-session-data-redis:1.3.0.RELEASE",
            "biz.paluch.redis:lettuce:3.5.0.Final",
            "org.springframework:spring-web:4.3.4.RELEASE",
    )
    testCompile group: 'junit', name: 'junit', version: '4.+'
}
下载所需要的必须的jar!

2.新建RedisHttpSessionConfig.java代码如下:

@EnableRedisHttpSession
public class RedisHttpSessionConfig {
    @Bean
    public LettuceConnectionFactory connectionFactory() {
        return new LettuceConnectionFactory();
    }
}

此处连接redis使用的是默认设置(本地地址,默认端口,无密码),详情请直接插看LettuceConnectionFactory.class源码!

3新建SpringSessionInitializer.java代码如下:

public class SpringSessionInitializer extends AbstractHttpSessionApplicationInitializer {
    public SpringSessionInitializer() {
        super(RedisHttpSessionConfig.class);
    }
}
4写一个测试Servlet
@WebServlet("/test")
public class SpringSessionTestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        System.out.println("hello");
        System.out.println("hello");
        String attributeName = request.getParameter("attributeName");
        String attributeValue = request.getParameter("attributeValue");
        request.getSession().setAttribute(attributeName, attributeValue);
        response.sendRedirect(request.getContextPath() + "/");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doPost(request,response);
    }
}
5添加一个测试页面

<body>
      hello Spring Session Redis!
      <form class="form-inline" role="form" action="./test" method="post">
        <label for="attributeName">Attribute Name</label>
        <input id="attributeName" type="text" name="attributeName"/>
        <label for="attributeValue">Attribute Value</label>
        <input id="attributeValue" type="text" name="attributeValue"/>
        <input type="submit" value="Set Attribute"/>
      </form>
  </body>
现在就可以直接启动项目了

随便输入就可以在自己的redis中查看session了


如果要取session的话,和原始的方法一样,取出的就是redis里面的session

@WebServlet("/testSession")
public class TestSessionServlet extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String attributeName = session.getAttribute("lt").toString();
        System.out.println(attributeName);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }


}

=完结=











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值