redisspringboot
时间: 2023-11-07 22:01:56 浏览: 37
在Spring Boot项目中使用Redis,主要有以下几个步骤:
1. 需要添加Redis的依赖Jar,可以在pom.xml文件中添加以下代码:
```
<!-- redis依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>${spring-boot.version}</version>
</dependency>
```
2. 只需要在配置文件application.properties中添加Redis的连接配置即可。可以在配置文件中添加以下代码:
```
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.0.24
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=200
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=10
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=1000
```
3. 如果需要自定义注入Redis的bean组件,可以在配置类中添加注解,如下所示:
```
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
// 其他配置...
return template;
}
}
```