条件:引用好架包

 <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年12月17日 ASP.NET 12个表单元素&&简单控件/复合控件

    12个表单元素可以分为三大类 第一类:文本类 <input type = "text" /> //普通文本框 <input type = "passwo ...

  2. java web 之Session

    1.Session简单介绍 由于Http是无状态的协议,所以服务端需要记录用户的状态时,就需要某种机制来识别具体的用户,实现这个机制的方式就是session. 典型的场景比如购物车,当你点击下单按钮时 ...

  3. css样式的优先顺序

    一.css样式的权重:!important(1000+) > 内联样式(  1000 ) > ID选择器(100 ) > 类选择器(10) > 标签选择器( 1 ) > ...

  4. img底部空白以及多余的空白文本节点解决方案

    1:img底部有空白的问题 img的css属性display的默认值是inline,这样会导致img的vertical-align的默认值是 baseline; baseline又不是bottom,只 ...

  5. vue双向绑定笔记

    原文:https://github.com/louzhedong/blog/issues/4 <!DOCTYPE html> <html lang="en"> ...

  6. 关于webuploader 在ie9上不能触发 input 的 change 事件

    上传文件用了 webuploader,ie9以上及其它浏览器正常执行js ,但是在ie9上点击input 无效,不能触发change 事件. 第一反应是ie9 需要使用flash 上传文件 原因: . ...

  7. ping外网

    /** * @author suncat * @category 判断是否有外网连接(普通方法不能判断外网的网络是否连接,比如连接上局域网) * @return */ public static fi ...

  8. Java初学者 编译能通过,但显示有错误,并且不会自动弹出方法的解决方法。

    因为使用了 @Data注解,关于注解的作用尚未深入理解,此处先做一个记录. 解决方法是,添加lombok插件

  9. July 06th 2017 Week 27th Thursday

    Knowledge is the antidote to fear. 知识可以解除恐惧. Fear always steps from unknown things. Once we know wha ...

  10. January 27 2017 Week 4 Friday

    Procrastination is the thief of time. 拖延是时光之贼. Procrastination is the thief of time, besides, it is ...