spring集成redis主要依靠spring-integration-redis来进行集成。因为主流的Java redis客户端有三种,jedis、Lettuce和Jedisson。而spring-integration-redis只支持jedis和Lettuce。下面简单介绍两种客户端的配置方式。
一 集成jedis
首先通过maven配置依赖:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.integration/spring-integration-redis -->
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-redis</artifactId>
<version>5.5.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.8.0</version>
</dependency>
</dependencies>
``
因为都是第三方jar,如果不使用xml的话,就只能使用@Bean注解来配置了,java代码如下:
```java
@Configuration
public class SpringJedisDemo {
@Bean
public RedisStandaloneConfiguration redisStandaloneConfiguration() {
final RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setHostName("localhost");
configuration.setPort(6379);
return configuration;
}
@Bean
public RedisConnectionFactory redisConnectionFactory(RedisStandaloneConfiguration configuration) {
final JedisConnectionFactory factory = new JedisConnectionFactory(configuration);
factory.afterPropertiesSet();
return factory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
final RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
return template;
}
public static void main(String[] args) {
final AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringJedisDemo.class);
final RedisTemplate<String, Object> redisTemplate = ctx.getBean(RedisTemplate.class);
final ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
opsForValue.set("China", "Peking");
System.out.println(opsForValue.get("China"));
}
}
二 集成lettuce
集成lettuce与jedis大同小异。因为spring优雅的设计,两者实现了同一个接口,所以只需要改动一点点就行了。首先是依赖:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.integration/spring-integration-redis -->
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-redis</artifactId>
<version>5.5.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.14</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.lettuce/lettuce-core -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>6.0.9.RELEASE</version>
</dependency>
</dependencies>
Java代码:
@Configuration
public class SpringLettuceDemo {
@Bean
public RedisStandaloneConfiguration redisStandaloneConfiguration() {
final RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setHostName("localhost");
configuration.setPort(6379);
return configuration;
}
@Bean
public RedisConnectionFactory redisConnectionFactory(RedisStandaloneConfiguration configuration) {
final LettuceConnectionFactory factory = new LettuceConnectionFactory(configuration);
factory.afterPropertiesSet();
return factory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
final RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
return template;
}
public static void main(String[] args) {
final AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringLettuceDemo.class);
final RedisTemplate<String, Object> redisTemplate = ctx.getBean(RedisTemplate.class);
final ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
opsForValue.set("China", "Peking");
System.out.println(opsForValue.get("China"));
}
}