1.pom.xml文件

<dependencies>
<!-- spring核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- spring整合redis包 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<!-- spring整合junit包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.2.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- redis客户端包 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<port>9996</port>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

  2.、applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <!-- 扫描 @Server @Controller @Repository -->
<context:component-scan base-package="com.baidu"/> <!-- 加载properties文件 -->
<context:property-placeholder location="classpath:config.properties" /> <!-- 引入redis配置 -->
<import resource="applicationContext-cache.xml"/> <!-- 引入mq配置 -->
<!-- <import resource="applicationContext-mq.xml"/> -->
</beans>

applicationContext-cache.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cache="http://www.springframework.org/schema/cache"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd"> <!-- jedis 连接池配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="30" />
<property name="maxWaitMillis" value="30" />
<property name="testOnBorrow" value="true" />
</bean> <!-- jedis 连接工厂 -->
<bean id="redisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
<property name="hostName" value="localhost" />
<property name="port" value="6379" />
<property name="poolConfig" ref="poolConfig" />
<property name="database" value="0"></property>
</bean> <!-- spring data 提供 redis模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory" />
<!-- 如果不指定 Serializer -->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer">
</bean>
</property>
</bean> </beans>

  测试类

package com.baidu.redistest;

import java.util.concurrent.TimeUnit;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Demo {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
public void testRedis() {
// 保存key value
// 设置30秒失效
redisTemplate.opsForValue().set("c", "北京", 300, TimeUnit.SECONDS);
System.out.println(redisTemplate.opsForValue().get("c"));
}
}

  

测试成功

最新文章

  1. java常用加密和解密工具类EncryptUtil.java
  2. MyISAM InnoDB 区别
  3. MySQL查询表内重复记录
  4. 用Python对excel文件的简单操作
  5. nginx + php +mysql (适配thinkphp)
  6. Leetcode#59 Spiral Matrix II
  7. [反汇编练习] 160个CrackMe之021
  8. Python练习题 029:Project Euler 001:3和5的倍数
  9. spring mvc 存取值
  10. Java 线程和多线程执行过程分析
  11. 一步一步配置ABP Core Template with Angular
  12. Playfair 加密
  13. docker部署archery
  14. amoeba实现读写分离
  15. 2018-2019-2 20165316 『网络对抗技术』Exp3:免杀原理与实践
  16. grunt,提示node不是内部命令也不是外部命令
  17. devexpress总结 accordionControl 加载panelcontrol 的快捷方式
  18. windows下django1.7 +python3.4.2搭建记录1
  19. 绘制ROC曲线
  20. python asyncio

热门文章

  1. Vue 插件和Preset
  2. 10.19JS日记
  3. PHP的网站主要攻击方式有哪些?
  4. Informatica_(1)安装
  5. C# System.Data.OracleClient requires Oracle client software version 8.1.7 or greater
  6. java学习笔记1--基础知识
  7. 网卡驱动如何设置组播MAC地址
  8. How to change and add some params to request in Laravel controller?
  9. [Jmeter] Run Command to generate a specific listener’s chart report
  10. Java JMX 监管