条件:引用好架包
org.springframework.data
spring-data-redis
2.1.3.RELEASE
redis.clients
jedis
2.9.0
一、使用xml进行配置
1、创建xml文件
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配置类
packagecom.wbg.mr.spring;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.data.redis.connection.jedis.JedisConnectionFactory;importorg.springframework.data.redis.core.RedisTemplate;importorg.springframework.data.redis.serializer.StringRedisSerializer;
@Configurationpublic classRedisConfig {
@BeanpublicJedisConnectionFactory jedisConnectionFactory(){
JedisConnectionFactory jcf= newJedisConnectionFactory();
jcf.setHostName("localhost");returnjcf;
}
@BeanpublicRedisTemplate redisTemplate(){
RedisTemplate rt= newRedisTemplate();
rt.setConnectionFactory(jedisConnectionFactory());
rt.setKeySerializer(newStringRedisSerializer());
rt.setValueSerializer(newStringRedisSerializer());returnrt;
}
}
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);
测试:
packagecom.wbg.mr.spring;importorg.springframework.context.ApplicationContext;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importorg.springframework.data.redis.core.RedisTemplate;public classMain {public static voidmain(String[] args) {
annotationConfigApplicationContext();
}public static voidclassPathXmlApplicationContext(){
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 voidannotationConfigApplicationContext(){
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);
}
}
View Code
原文:https://www.cnblogs.com/weibanggang/p/10173180.html