条件:引用好架包

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

一、使用xml进行配置

1、xml进行配置JedisPoolConfig、JedisConnectionFactory、Spring RedisTemplate-

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置JedisPoolConfig-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="50"/>
<property name="maxTotal" value="100"/>
<property name="maxWaitMillis" value="20000"/>
</bean>
<!--配置JedisConnectionFactory-->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost"/>
<property name="port" value="6379"/>
<property name="poolConfig" ref="poolConfig"/>
</bean>
<!--使用字符串进行序列化-->
<bean id="stringReadisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<!--使用JDK的序列化器进行转化-->
<bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<!--配置Spring RedisTemplate-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="keySerializer" ref="stringReadisSerializer"/>
<property name="valueSerializer" ref="stringReadisSerializer"/>
</bean>
</beans>

2、使用:

   ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
redisTemplate.opsForValue().set("key1","value1");
redisTemplate.opsForValue().set("key2","value2");
String value1 = (String) redisTemplate.opsForValue().get("key1");
System.out.println(value1);

二、使用java方式

1、创建RedisConfg配置类

package com.wbg.mr.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration
public class RedisConfig { @Bean
public JedisConnectionFactory jedisConnectionFactory(){
JedisConnectionFactory jcf = new JedisConnectionFactory();
jcf.setHostName("localhost");
return jcf;
}
@Bean
public RedisTemplate redisTemplate(){
RedisTemplate rt = new RedisTemplate();
rt.setConnectionFactory(jedisConnectionFactory());
rt.setKeySerializer(new StringRedisSerializer());
rt.setValueSerializer(new StringRedisSerializer());
return rt;
} }

2、使用

  ApplicationContext applicationContext = new AnnotationConfigApplicationContext(RedisConfig.class);
RedisConfig redisConfig = applicationContext.getBean(RedisConfig.class);
RedisTemplate redisTemplate = redisConfig.redisTemplate();
redisTemplate.opsForValue().set("key11","value11");
redisTemplate.opsForValue().set("key12","value12");
String value11 = (String) redisTemplate.opsForValue().get("key11");
System.out.println(value11);

测试:

package com.wbg.mr.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate; public class Main {
public static void main(String[] args) {
annotationConfigApplicationContext();
}
public static void classPathXmlApplicationContext(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
redisTemplate.opsForValue().set("key1","value1");
redisTemplate.opsForValue().set("key2","value2");
String value1 = (String) redisTemplate.opsForValue().get("key1");
System.out.println(value1); } public static void annotationConfigApplicationContext(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(RedisConfig.class);
RedisConfig redisConfig = applicationContext.getBean(RedisConfig.class);
RedisTemplate redisTemplate = redisConfig.redisTemplate();
redisTemplate.opsForValue().set("key11","value11");
redisTemplate.opsForValue().set("key12","value12");
String value11 = (String) redisTemplate.opsForValue().get("key11");
System.out.println(value11);
}
}

spring配置redis(xml+java方式)(最底层)的更多相关文章

  1. web.xml中配置Spring中applicationContext.xml的方式

    2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...

  2. spring配置datasource三种方式

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp34 spring配置datasource三种方式 1.使用org.spri ...

  3. Spring配置redis及使用

    一.redis简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库 Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用. ...

  4. 配置RedisTemplate、JedisPoolConfig、JedisConnectionFactory+自定义序列化 (xml+java方式)+使用

    java方式配置RedisTemplate //spring注入ben //@Bean(name = "redisTemplate") public RedisTemplate i ...

  5. spring配置redis注解缓存

    前几天在spring整合Redis的时候使用了手动的方式,也就是可以手动的向redis添加缓存与清除缓存,参考:http://www.cnblogs.com/qlqwjy/p/8562703.html ...

  6. spring配置datasource三种方式 数据库连接池

    尊重原创(原文链接):http://blog.csdn.net/kunkun378263/article/details/8506355 1.使用org.springframework.jdbc.da ...

  7. spring配置datasource三种方式及具体信息

    1.使用org.springframework.jdbc.datasource.DriverManagerDataSource说明:DriverManagerDataSource建立连接是只要有连接就 ...

  8. spring 配置 redis

    1.maven相关pom.xml <dependencies> <!--spring redis--> <dependency> <groupId>or ...

  9. spring配置JNDI(Java Naming and Directory Interface,Java命名和目录接口)数据源

    1.在tomcat下的server.xml的 <GlobalNamingResources> </GlobalNamingResources>添加下面代码 <Resour ...

随机推荐

  1. 2017年11月27日 C#MDI窗体创建&记事本打印&记事本查找、自动换行

    MDI窗体第一个父窗体 把属性里的IsMdiContainer设置为true就可以了 父窗体连接子窗体 //创建一个新的类,用来连接别的窗体,并且别的窗体为唯一窗体 List<Form> ...

  2. javaweb之jsp的属性范围

    1.什么是jsp的属性范围? 所谓的属性范围就是一个属性设置之后,可以经过多少个其他页面后仍然可以保存并继续使用.jsp提供了四种属性范围,如下: 当前页,对应的jsp对象为pageContext,属 ...

  3. poj3260 平衡问题(二维01背包)

    http://www.cnblogs.com/ziyi--caolu/p/3228090.html http://blog.csdn.net/lyy289065406/article/details/ ...

  4. vim lua对齐indent无效

    查了半天,打开命令 :filetype一看是关闭的 filetype detection:ON  plugin:ON  indent:OFF 在vimrc里打开 filetype indent on ...

  5. PHPStorm-Mintfy-And-Color-Schema

    美化Php-storm 1.隐藏一些工具条 打开一个项目后我习惯把一些工具条隐藏,在view菜单中把Tool buttons,Status bar,Navigation bar. CTRL+E 切换当 ...

  6. [国家集训队2012]middle(陈立杰)

    我是萌萌的传送门 我是另一个萌萌的传送门 脑残错误毁一下午…… 其实题解早就烂大街了,然而很久之前我只知道是二分答案+主席树却想不出来这俩玩意儿怎么一块儿用的……今天又翻了几篇题解才恍然大悟,是把权值 ...

  7. easyui window窗口 随body的滚动条 滚动

    问题描述: 当easyui window窗口弹出的时候,依然可以滚动body 的滚动条,而且window窗口也会随它一起滚动 思路:bootstrap 模态框弹出的时候,给body 添加了 .moda ...

  8. Windows 8 Metro风格颜色表-Metro colours

    http://huaban.com/pins/538986818

  9. Android MediaRecorder实现暂停断点录音功能

    基本原理如下:MediaRecorder通过MIC录音,系统没有自带的pause功能,每次暂停录音,都会结束本次的录音.现在本人的设计思路是:MediaRecorder录音暂停时,保存这段所录下的音频 ...

  10. 【Python】卸载完Python3 之后 Python2 无法打开IDLE

    安装官方的Python带Idle但是却无法打开,百度谷歌了几种解决方法,加上自己的实际境况予以解决. 我的python是直接安装在C盘下的. 1.首先是设置环境变量: Path=C:\Python27 ...