关于StringRedisTemlate的使用

本文介绍了一种解决StringRedisTemplate注入失败的方法,并提供了一种手动建立Redis连接的方式。通过这种方式,可以避免创建Bean时出现的错误。文章还列举了具体的Maven依赖配置及连接设置。

关于StringRedisTemlate的使用

项目需要进行缓存的使用,主要是使用到了StringRedisTemplate,新手小白,使用@Autowired注入直接报错,create bean ...."*controller" failed,autowired  ... failed 等错误。无奈之下,使用了另一种方式,使用手动建立缓存链接。方法步骤如下:
  1. maven依赖:
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.3.0.RELEASE</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-context-support</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.4.2</version>
        </dependency>
  1. 建立链接

        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
        //设置HostName
        connectionFactory.setHostName("192.168.**.***");
        //设置port
        connectionFactory.setPort(****);
        //设置密码
        connectionFactory.setPassword("password");
        //建立链接
        StringRedisTemplate template = new StringRedisTemplate(connectionFactory);
  1. 关于StringRedisTemplate的用法
        template.opsForValue.get(key);//获取key的值
        template.opsForValue.set(key,value);//设置键值对
  1. 其他参考文献
    http://www.ithao123.cn/content-10612204.html
    http://blog.youkuaiyun.com/hotdust/article/details/52251148

    新手小白,如果知道上面问题出现原因还请恳请留言告知,或发送至邮箱875674770@qq.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值