zoukankan      html  css  js  c++  java
  • ssm不用springboot 配置spring data redis 的redisTemplate

    首先maven依赖,别用最新版,用这版,因为新版jedisConnectionFactory的配置过时了,新版怎么配置我也没搞清楚

    <!--redis-->
        <dependency>
          <groupId>org.springframework.data</groupId>
          <artifactId>spring-data-redis</artifactId>
          <version>1.3.0.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>redis.clients</groupId>
          <artifactId>jedis</artifactId>
          <version>2.4.2</version>
        </dependency>
    redis.properties
    # 设置最大连接数
    REDIS.MAXTOTAL=50
    #设置获取连接的最大等待时间
    REDIS.MAXWAITMILLIS=1000
    #开启获取连接时可用性校验,保证拿到的连接都是可用的
    REDIS.TESTONBORROW=true
    #设置IP
    REDIS.HOSTNAME=localhost
    #设置端口
    REDIS.PORT=6379
    #密码
    REDIS.PASSWORD=
    #设置是否使用连接池
    REDIS.USEPOOL=true

    然后配置applicationContext-redis.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
           xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    
        <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true" />
        <!--构建连接池配置信息-->
        <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
            <!--设置最大连接数-->
            <property name="maxTotal" value="${REDIS.MAXTOTAL}"></property>
            <!--设置获取连接的最大等待时间-->
            <property name="maxWaitMillis" value="${REDIS.MAXWAITMILLIS}"></property>
            <!--开启获取连接时可用性校验,保证拿到的连接都是可用的-->
            <property name="testOnBorrow" value="${REDIS.TESTONBORROW}"></property>
        </bean>
        <!--构建Spring-data-redis连接工厂,其实就是连接池-->
        <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
            <constructor-arg index="0" ref="jedisPoolConfig"></constructor-arg>
            <!--设置IP-->
            <property name="hostName" value="${REDIS.HOSTNAME}"></property>
            <!--设置端口-->
            <property name="port" value="${REDIS.PORT}"></property>
            <!--设置是否使用连接池-->
            <property name="usePool" value="${REDIS.USEPOOL}"></property>
            <!--设置密码默认是没有密码-->
            <property name="password" value="${REDIS.PASSWORD}"></property>
        </bean>
        <!--stringRedisTemplate-->
        <!-- String类型的RedisTemplate模板 -->
        <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
            <constructor-arg index="0" ref="jedisConnectionFactory" />
        </bean>
    </beans>

    在applicationContext.xml中引入上面的xml

    <import resource="applicationContext-redis.xml"/>



    然后就可以在需要的地方注入;如下:
    @Autowired
        private RedisTemplate<String, String> redisTemplate;
    
    
    /**
                 * 存储token到redis并设置过期时间10分钟
                 */
                redisTemplate.opsForValue().set(Const.TOKEN_PREFIX + username, forgetToken, 10, TimeUnit.MINUTES);
    
    /**
             * 从redis中取出对应用户的token
             */
            String token  = redisTemplate.opsForValue().get(Const.TOKEN_PREFIX + username);
    
    
    /**
                     * 修改成功,删除redis中的存储
                     */
                    redisTemplate.delete(Const.TOKEN_PREFIX + username);



  • 相关阅读:
    初步了解Spark生态系统及Spark Streaming
    VS2010 项目引用了微软企业库,但是编译时提示:未能找到类型或命名空间名称
    Apache MPM winnt
    华为 HG8245C 光猫 修改无线用户数限制+hw_ctree.xml 文件解密
    将j-ui(dwz)套用到thinkphp注意事项
    Excel 导入 Sql Server出错——“文本被截断,或者一个或多个字符在目标代码页中没有匹配项”错误的解决
    wampserver 安装后 启动失败的解决方法
    东进语音卡的声音文件格式
    mysql insert into select 语法
    糗百新鲜事——爬虫python
  • 原文地址:https://www.cnblogs.com/yunqing/p/11601540.html
Copyright © 2011-2022 走看看