简单示例:
application.properties:
## Redis数据库索引(使用的数据库(0-15),默认为0)
spring.redis.database=0
## 连接URL,将覆盖主机,端口和密码(用户将被忽略),
## 例如:redis://user:password@example.com:6379
spring.redis.url=
## Redis服务器主机: 127.0.0.1
spring.redis.host=localhost
## Redis服务器连接密码(默认为空)
spring.redis.password=
## Redis服务器连接端口
spring.redis.port=6379
## 启用SSL支持
spring.redis.ssl=false
## 连接超时时间(毫秒)
spring.redis.timeout=0
## 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
## 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
## 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
## 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
## cluster [ˈklʌstə(r)] 集群
spring.redis.cluster.nodes=
spring.redis.cluster.max-redirects=
## sentinel [ˈsentɪnl] 哨兵
spring.redis.sentinel.master=
spring.redis.sentinel.nodes=
## lettuce [ˈletɪs] 莴苣、生菜
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
spring.redis.lettuce.shutdown-timeout=100
pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
定义redis的操作类
@Repository
public class RedisDao {
@Autowired
private StringRedisTemplate stringRedisTemplate;
public void setKey(String key, String value) {
ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
valueOperations.set(key, value, 1, TimeUnit.MINUTES);//1分钟过期
}
public String getValue(String key) {
ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
return valueOperations.get(key);
}
}
测试:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Test
public void contextLoads() {
}
public static Logger logger = LoggerFactory.getLogger(DemoApplicationTests.class);
@Autowired
RedisDao redisDao;
@Test
public void testRedis() {
redisDao.setKey("name", "xq");
redisDao.setKey("age", "18");
logger.info(redisDao.getValue("name"));
logger.info(redisDao.getValue("age"));
}
}
其他
StringRedisTemplate 与 RedisTemplate关系
public class StringRedisTemplate extends RedisTemplate<String, String> {
RedisAutoConfiguration
@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
@ConditionalOnClass :
该注解的参数对应的类必须存在,否则不解析该注解修饰的配置类;
@ConditionalOnMissingBean :
该注解表示,如果存在它修饰的类的bean,则不需要再创建这个bean;
可以给该注解传入参数例如@ConditionOnMissingBean(name = “example”),
这个表示如果name为“example”的bean存在,这该注解修饰的代码块不执行。
判断是否执行初始化代码,即如果用户已经创建了bean,则相关的初始化代码不再执行。