使用SpringBoot整合SpringDataRedis

备忘录模式Memento使用场景录入大批人员资料。正在录入当前人资料时,发现上一个人录错了,此时需要恢复上一个人的资料,再进行修改。Word文档编辑时,忽然电脑死机或断电,再打开时,可以看到word提示你恢复到以前的文档管理系统中,公文撤回功能。公文发送出去后,想撤回来核心内容  就是保存某个对象内部状态的拷贝,这样以后就可以将该对象恢复到原先的状态结构 说明源发起类Originator 负责创建一个备忘录Memento,用以记录当前时刻它的内部状态,并可使用备忘录恢复内部...
摘要由CSDN通过智能技术生成

SpringBoot整合SpringDataRedis


1.创建项目添加依赖


  创建SpringBoot项目,并添加如下依赖:

<dependencies>
    <!-- springBoot 的启动器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Spring Data Redis 的启动器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
    </dependency>
</dependencies>

2.设置application.properties文件

 

spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.min-idle=5
spring.redis.pool.max-total=20
spring.redis.hostName=192.168.88.120
spring.redis.port=6379
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 15
    评论
Spring Boot中,可以通过配置文件或编程方式来设置Redis线程池。以下是通过编程方式实现整合Redis并设置Redis线程池的步骤: 1. 首先,在你的Spring Boot项目中,添加对Redis和Lettuce(RedisJava客户端)的依赖。可以在项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </dependency> ``` 2. 在application.properties或application.yml配置文件中添加Redis相关配置,包括主机名、端口号、密码等。例如: ```yaml spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= ``` 3. 创建Redis连接工厂类,可以使用LettuceConnectionFactory。在该类中,你可以设置与Redis连接相关的属性,包括连接池配置。下面是一个示例: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; @Configuration public class RedisConfig { @Value("${spring.redis.host}") private String redisHost; @Value("${spring.redis.port}") private int redisPort; @Value("${spring.redis.password}") private String redisPassword; @Bean public LettuceConnectionFactory redisConnectionFactory() { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort); config.setPassword(redisPassword); return new LettuceConnectionFactory(config); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory()); return redisTemplate; } } ``` 在上述示例中,LettuceConnectionFactory被创建为一个Bean,并通过RedisStandaloneConfiguration设置了连接的主机名、端口号和密码。然后,将该连接工厂设置到RedisTemplate中。 4. 在需要使用Redis的地方,注入RedisTemplate,并调用其相关方法来操作Redis。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; @Service public class MyService { private final RedisTemplate<String, Object> redisTemplate; @Autowired public MyService(RedisTemplate<String, Object> redisTemplate) { this.redisTemplate = redisTemplate; } public void saveData(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object getData(String key) { return redisTemplate.opsForValue().get(key); } } ``` 上述示例中的MyService类注入了RedisTemplate,并通过redisTemplate实现了保存数据和获取数据的方法。 这样,你就可以使用Spring Boot整合Redis并设置Redis线程池了。注意,在实际应用中,你可能还需要做一些其他配置和异常处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值