spring5整合spring-data-redis2

更新修改一下redis的配置,源码看GitHub的
一. Redis基本配置


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg>
            <bean class="org.springframework.data.redis.connection.RedisStandaloneConfiguration"
                c:host-name="${redis.host}" c:port="${redis.port}" />
        constructor-arg>      
    bean>

    <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"
        factory-method="create" c:connection-factory-ref="jedisConnectionFactory" />    

    <bean id="stringRedisSerializer"  class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        <property name="keySerializer" ref="stringRedisSerializer" />
        <property name="hashKeySerializer" ref="stringRedisSerializer" />
    bean>

    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="jedisConnectionFactory"/>

beans>

二. Spring基础配置


<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:mvc="http://www.springframework.org/schema/mvc" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"
    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
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd"    
    default-autowire="byName">

    
    <mvc:annotation-driven />
    

    
    <context:component-scan base-package="com.controller,core.config" />
    
    <bean id="propertyConfig"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:application.propertiesvalue>
        property>
        <property name="fileEncoding">
            <value>UTF-8value>
        property>
    bean>

    
    

    
    <cache:annotation-driven cache-manager="redisCacheManager"/>
    <bean id="redisUtil" class="core.util.RedisUtil" />


beans>

三. spring-data-redis把缓存交由Spring的CacheManager管理


1.spring查询缓存xml配置


    <cache:annotation-driven cache-manager="redisCacheManager"/>

2 源码查看
下图中的org.springframework.data.redis.cache.RedisCacheManager,可以由这三种方式构建。第一种是RedisCacheManager的3个构造方法,第二种是调用RedisCacheManager的内部方法,第三种通过RedisCacheManager的内部类构建,上面使用的是内部类构建。
spring5整合spring-data-redis2_第1张图片
我们看看第二种的构建方式有什么区别,下图可以看出create方法是使用RedisCacheManager的构造方法构建,另外两个其实是通过RedisCacheManager的内部类构建的。
spring5整合spring-data-redis2_第2张图片
下图是org.springframework.data.redis.cache.RedisCacheWriter接口的实现类,它是RedisCacheManager的构造函数的参数之一,图中可以看到它提供了对redis读写操作的一些方法
spring5整合spring-data-redis2_第3张图片
同理下面是RedisCacheManager构造函数的另外一个参数org.springframework.data.redis.cache.RedisCacheConfiguration,提供了cache的一些配置方法
spring5整合spring-data-redis2_第4张图片

四. TestController

package com.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import core.util.RedisUtil;

@Controller
@RequestMapping("/test")
public class TestController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    @Qualifier("redisCacheManager")
    private CacheManager cacheManager;

    @RequestMapping(value="index", method = RequestMethod.GET)
    public Object index(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("/test");
        System.out.println(cacheManager.getCache("user"));
        return mv;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @ResponseBody
    @RequestMapping(value="json", method = RequestMethod.GET)
    public Object json(HttpServletRequest request) {
        Map m = new HashMap();
        m.put("key", "value");
        redisUtil.setCacheMap("m", m);
        return redisUtil.getCacheMap("m");
    }

}
  • 源码链接:https://github.com/MayOne007/Spring5-SpringDataRedis2.0
  • http://download.csdn.net/download/u011189939/10131456
  • Redis基础配置参考了这篇文章:https://www.cnblogs.com/shihaiming/p/6050777.html

你可能感兴趣的:(笔记)