[JAVA笔记]-redis-spring配置redis(xml+java方式)

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
     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);
     }
 } 

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);
     }
 }

image

更新时间:2019-10-18 15:32:49

本文由 Alicyu 创作,如果您觉得本文不错,请随意赞赏
采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
原文链接:https://www.alicyu.com/archives/spring-redis
最后更新:2019-10-18 15:32:49

Your browser is out of date!

Update your browser to view this website correctly. Update my browser now

×