redis与spring/springboot的整合

spring整合:

一、引入pom文件:

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.7.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.7.2.RELEASE</version>
</dependency>

二、创建redis配置属性文件redis.properties:

redis.host=127.0.0.1
redis.port=6379
redis.password=
redis.maxIdle=5
redis.maxTotal=20
redis.maxWait=3000

三、创建redis整合到spring的配置文件applicationContext-redis.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath*:properties/*.properties"></context:property-placeholder>

    <bean class="redis.clients.jedis.JedisPoolConfig" id="poolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxTotal" value="${redis.maxTotal}"/>
        <property name="maxWaitMillis" value="${redis.maxWait}"/>
    </bean>

    <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" id="connectionFactory2" p:poolConfig-ref="poolConfig" p:hostName="${redis.host}" p:password="${redis.password}" p:port="${redis.port}"></bean>

    <bean class="org.springframework.data.redis.core.RedisTemplate" id="redisTemplate">
        <property name="connectionFactory" ref="connectionFactory2"/>

		<!--        处理redis库内乱码-->
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>

        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>

        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>

        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
    </bean>

</beans>

在redis引用属性配置文件这个部分,需要重点说明下property-placeholder,在多个spring的xml配置引用属性配置文件时,会多次使用property-placeholder,这里需要统一将其配置为:

<context:property-placeholder location="classpath*:properties/*.properties" />

如applicationContext-dao.xml和applicationContext-redis.xml都有自己的属性配置文件需要引入,不能各自指向自己的配置文件,而要统一由上面的方式代替。否则会导致xml文件无法读取属性配置而报错。
这是由于:context:property-placeholder标签在Spring配置文件中只能存在一份。Spring容器是采用反射扫描的发现机制,通过标签的命名空间实例化实例,当Spring探测到容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描,即只能存在一个实例。所以正常来说引入多个属性配置文件需要采取如下配置:

<!-- 加载配置文件 多个文件用,隔开 -->
<context:property-placeholder location="classpath*:properties/xxx.properties,yyy.properties" />

但redis配置文件又不会存在于dao的classpath之下,所以在redis和dao的spring的xml配置文件上统一配置成:

<context:property-placeholder location="classpath*:properties/*.properties" />

四、从spring容器中取出redisTemplate,执行缓存逻辑:

@Autowired
private RedisTemplate<String,String> redisTemplate;
@Override
public User findUser(Integer uid) {
    User user = null;
    String userString = redisTemplate.boundValueOps("user").get();
    if (!StringUtils.isEmpty(userString)) {
        user = JSONObject.parseObject(userString, User.class);
        System.out.println("在redis中获取用户数据");
    }else {
        user = userMapper.findUser(uid);
        userString = JSONObject.toJSONString(user);
        redisTemplate.boundValueOps("user").set(userString);
        System.out.println("在mysql中获取用户数据");
    }
    return user;
}

springboot整合:

一、引入pom文件:

<dependency>
   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

二、在yml文件中配置redis配置:

spring:
  cache:
    type: redis
  redis:
    host: 192.168.2.66 # Redis服务器地址
    port: 6379 # Redis服务器连接端口
    database: 0 # Redis数据库索引(默认为0)
    password: # Redis服务器连接密码(默认为空)
    jedis:
      pool:
        max-active: 20 # 连接池最大连接数(使用负值表示没有限制)
        max-idle: 5 # 连接池中的最大空闲连接
        min-idle: 0 # 连接池中的最小空闲连接
        max-wait: 1000ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
    timeout: 1000ms # 连接超时时间(毫秒)

三、从spring容器中取出redisTemplate,执行缓存逻辑:

@Autowired
private RedisTemplate<String,String> redisTemplate;

此处同spring整合逻辑一致。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值