spring配置redis(xml+java方式)(最底层)

条件:引用好架包

 <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
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> JedisConnectionFactory jedisConnectionFactory(){
    JedisConnectionFactory jcf </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisConnectionFactory();
    jcf.setHostName(</span>"localhost"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> jcf;
}
@Bean
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> RedisTemplate redisTemplate(){
    RedisTemplate rt </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RedisTemplate();
    rt.setConnectionFactory(jedisConnectionFactory());
    rt.setKeySerializer(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StringRedisSerializer());
    rt.setValueSerializer(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StringRedisSerializer());
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> 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);

}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> annotationConfigApplicationContext(){
    ApplicationContext applicationContext </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> AnnotationConfigApplicationContext(RedisConfig.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    RedisConfig redisConfig </span>= applicationContext.getBean(RedisConfig.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">);
    RedisTemplate redisTemplate </span>=<span style="color: rgba(0, 0, 0, 1)"> redisConfig.redisTemplate();
    redisTemplate.opsForValue().set(</span>"key11","value11"<span style="color: rgba(0, 0, 0, 1)">);
    redisTemplate.opsForValue().set(</span>"key12","value12"<span style="color: rgba(0, 0, 0, 1)">);
    String value11 </span>= (String) redisTemplate.opsForValue().get("key11"<span style="color: rgba(0, 0, 0, 1)">);
    System.out.println(value11);
}

}

View Code