SpringBoot配置Redis

spring.factories配置类文件

#redis 配置

#接口
spring.redis.host=192.168.80.130
#接口端口号
spring.redis.port=6379
#连接池最大连接量
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.timeout=30000

配置类

@RestController
public class MyController {

     @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public void test(){
        stringRedisTemplate.opsForValue().set("123","123");
        System.out.println("123142123");
    }
}

激活

@SpringBootApplication
public class RedisApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext bean = SpringApplication.run(RedisApplication.class, args);
        MyController bean1 = bean.getBean(MyController.class);
        bean1.test();
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以为你提供有关springboot配置redis的相关信息,你可以参考以下步骤: 1.首先,在pom.xml文件中添加redis依赖 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2.在application.properties文件中配置redis的连接信息 ```properties spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.redis.database=0 ``` 其中,host和port是redis服务器的地址和端口,password是redis密码(如果有设置的话),database是redis数据库的编号。 3.在java代码中使用redis 添加一个RedisConfig类,用来配置redisTemplate ```java @Configuration public class RedisConfig { @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); //设置序列化方式 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; } } ``` 在其他类中使用redisTemplate进行redis操作 ```java @Autowired private RedisTemplate<Object, Object> redisTemplate; public void set(String key, Object value) { redisTemplate.opsForValue().set(key, value); } public Object get(String key) { return redisTemplate.opsForValue().get(key); } ``` 以上就是关于如何使用springboot配置redis的简单介绍,希望对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值