1. 引入redis相关包
<!-- redis 相关包--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.6.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
2. 配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.shsxt" /> <context:annotation-config/> <bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration"> <constructor-arg name="master" value="mymaster" /> <constructor-arg name="sentinelHostAndPorts"> <set> <value>172.16.54.135:26380</value> </set> </constructor-arg> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="password" value="123456" /> <property name="usePool" value="true" /> <constructor-arg name="sentinelConfig" ref="sentinelConfiguration" /> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean> </beans>
3. 编写redis配置文件
/** * Java 配置的操作步骤: * 1、编写 RedisConfig 的配置类 * 2、编写测试类 * 3、添加 Spring 的配置文件 * 4、启动测试查看结果 */ //@Configuration public class RedisConfig { @Bean public StringRedisTemplate redisTemplate(){ StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); stringRedisTemplate.setConnectionFactory(jedisConnectionFactory()); return stringRedisTemplate; } @Bean public static RedisConnectionFactory jedisConnectionFactory(){ RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration().master("mymaster") .sentinel("172.16.54.135",26380); JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(sentinelConfiguration); jedisConnectionFactory.setPassword("123456"); return jedisConnectionFactory; } }
4. 编写启动文件,支持xml和java配置两种方法
package com.shsxt; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.BoundValueOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; public class Application { /** * 方法一、java配置 * @param args */ /* public static void main(String[] args) { try { ApplicationContext context = new FileSystemXmlApplicationContext("classpath:applicationContext.xml"); JedisConnectionFactory connectionFactory = context.getBean(JedisConnectionFactory.class); StringRedisTemplate redisTemplate = new StringRedisTemplate(connectionFactory); BoundValueOperations<String,String> boundValueOperations = redisTemplate.boundValueOps("abcd"); boundValueOperations.set("11111"); System.out.println("设置完毕。。。"); String result = boundValueOperations.get(); System.out.println("获取值为:" + result); } catch (Exception e) { e.printStackTrace(); } } */ /** * 方法二、xml 配置 * @param args */ public static void main(String[] args) { try { ApplicationContext context = new FileSystemXmlApplicationContext("classpath:applicationContext.xml"); RedisTemplate redisTemplate = context.getBean(RedisTemplate.class); BoundValueOperations<String,String> boundValueOperations = redisTemplate.boundValueOps("abde111"); boundValueOperations.set("111111"); System.out.println("设置完毕"); String result = boundValueOperations.get(); System.out.println("获取值为:"+ result); } catch (BeansException e) { e.printStackTrace(); } } }