条件:引用好架包

 <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. 9、springboot之处理静态资源

    在springboot项目中的resource根目录下建立三个文件夹static.public.resources 里面都放同样名字的图片 但是图片内容不一样 启动springboot之后输入 htt ...

  2. Web前端面试指导(二十):JavaScript中如何翻转一个字符串?

    题目点评 字符串作在程序中是非常常见的,因为程序中绝大部分的数据都可以当作字符串来处理.需要对字符的处理方法比较熟悉,在回答的时候尽量能够说出多种解决方法更好! 字符串翻转的方法 1)使用字符串函数 ...

  3. vue中数组删除,页面没重新渲染

    创建一个组件时,数据类型是数组,在删除这个数组中的数据时,数组中的数据是对的,但页面渲染的数据却不对. 举例:(不一定复现) <ul> <li v-for="(item, ...

  4. C#基础拾遗系列之二:使用ILSpy探索C#7.0新增功能点

    C#基础拾遗系列之二:使用ILSpy探索C#7.0新增功能点   第一部分: C#是一种通用的,类型安全的,面向对象的编程语言.有如下特点: (1)面向对象:c# 是面向对象的范例的一个丰富实现, 它 ...

  5. C语言——顺序表插入、删除、定位运算算法

    说明:将元素x插入到顺序表L的第i个数据元素之前,这个i是从1开始的,但是程序中数组都是从0算起的,不要混淆了. 头文件: header.h // 顺序表的结构定义 #define Maxsize 1 ...

  6. Android ListView复制、删除的实现

    适配器MyAdapter: package com.zihao.adapter; import java.util.List; import com.zihao.popdemo.R; import c ...

  7. Oracle基础之count(1)和count(*)的区别

    在数据库中Count(*)或者Count(1)或者Count([列])或许是最常用的聚合函数.很多人其实对这三者之间是区分不清的.本文会阐述这三者的作用,关系以及背后的原理. 我在网上看到一些所谓的优 ...

  8. android测试Code

    <!--android:layout_alignParentTop="true"--><com.koooke.platform.View.CenterImage ...

  9. Python log 模块介绍

    刚用Python log模块写了一个例子,记录一下. import logging import logging.handlers import os from datetime import dat ...

  10. python vscode在centos下安装

    对于centos则如下: sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc sudo sh -c 'echo -e ...