Redis与spring整合缓存的业务场景使用方法二(使用注解@Cacheable@CacheEvict)

一:配置appliction-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:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"
	xmlns:cache="http://www.springframework.org/schema/cache" 
	xmlns:c='http://www.springframework.org/schema/c'
	xmlns:p="http://www.springframework.org/schema/p"
	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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"
	default-lazy-init="true">
	
	<!-- 开启spring cache注解功能-->
	<cache:annotation-driven cache-manager="redisCacheManager" />
	
	<context:annotation-config/>
	
	<context:property-placeholder ignore-unresolvable="true" location="classpath:config.properties" />
	
	<!-- Redis -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"/>

	<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="hostName"   value="${redis.hostName}" />
		<property name="port"       value="${redis.port}" />
		<property name="password"   value="${redis.password}" />
		<property name="timeout"    value="${redis.timeout}" />
		<property name="usePool"    value="${redis.usePool}" />
		<property name="poolConfig" ref="jedisPoolConfig" />
	</bean>

	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >
		<property name="connectionFactory" ref="jedisConnectionFactory"/>
	</bean>
	
	 <!-- redis缓存管理器 -->
	<bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
		 <constructor-arg name="redisOperations" ref="redisTemplate" />
    </bean>
</beans>
二: 在业务层添加注解


三:测试缓存



其实用这种方法,只要了解注解的含义,用起来很方便。比第一种方法方便太多,当然你也可以使用AOP的思想,自己进行自定义注解,并使用,下篇说明




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值