spring5整合spring-data-redis2

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

<?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: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">

    <!-- 以下是spring-data-redis配置方式 -->
    <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基础配置

<?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: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:annotation-config /> -->

    <!-- 设置需要进行Spring注解扫描的类包 -->
    <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.properties</value>
        </property>
        <property name="fileEncoding">
            <value>UTF-8</value>
        </property>
    </bean>

    <!-- 使用AspectJ方式配置AOP,解决AOP不拦截内部调用 -->
    <!-- <aop:aspectj-autoproxy proxy-target-class="true" /> -->

    <!-- 启用缓存注解功能 -->
    <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的内部类构建,上面使用的是内部类构建。
这里写图片描述
我们看看第二种的构建方式有什么区别,下图可以看出create方法是使用RedisCacheManager的构造方法构建,另外两个其实是通过RedisCacheManager的内部类构建的。
这里写图片描述
下图是org.springframework.data.redis.cache.RedisCacheWriter接口的实现类,它是RedisCacheManager的构造函数的参数之一,图中可以看到它提供了对redis读写操作的一些方法
这里写图片描述
同理下面是RedisCacheManager构造函数的另外一个参数org.springframework.data.redis.cache.RedisCacheConfiguration,提供了cache的一些配置方法
这里写图片描述

四. 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");
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值