Consider defining a bean of type ‘com.bsj.system.service.RedisService‘ in your configuration

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: Consider defining a bean of type ‘com.bsj.system.service.RedisService‘ in your configuration

今天遇到这个注入失败的问题 看了很多帖子解决办法都是加@MapperScan之类的注解 但是试了不行 最后奉上我自己的解决办法


要加入下面两个Redis的配置类

@Configuration
public class RedisConfig extends BaseRedisConfig {
}
public class BaseRedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisSerializer<Object> serializer = redisSerializer();
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(serializer);
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(serializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
    @Bean
    public RedisSerializer<Object> redisSerializer() {
        //创建JSON序列化器
        Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        //必须设置,否则无法将JSON转化为对象,会转化成Map类型
        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(objectMapper);
        return serializer;
    }
    @Bean
    public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
        //设置Redis缓存有效期为1天
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer())).entryTtl(Duration.ofDays(1));
        return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
    }
    @Bean
    public RedisService redisService(){
        return new RedisServiceImpl();
    }
}
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
9月前
|
Java 数据库连接 mybatis
Consider defining a bean of type ‘com.example.democrud.democurd.usermapper.DaoMapper‘ in your config
Consider defining a bean of type ‘com.example.democrud.democurd.usermapper.DaoMapper‘ in your config
101 0
|
6月前
|
监控 Dubbo 搜索推荐
No application config found or it‘s not a valid config! Please add <dubbo:application name=“...“ />
No application config found or it‘s not a valid config! Please add <dubbo:application name=“...“ />
517 1
|
5月前
Error starting ApplicationContext. To display the auto-configuration report re-run your application
Error starting ApplicationContext. To display the auto-configuration report re-run your application
|
8月前
|
Java
ApplicationContext is unlikely to start due to a @ComponentScan of the default package
解决办法: 1、一般发出这个警告的原因是你把启动类直接放在的src目录下面。 2、你需要在src目录下面再建一个包,然后把启动类放到下面。 3、或者你错将启动类放到java文件中了,与项目包成了同级文件,你只需要将启动类放到项目包里就可以。
39 0
|
9月前
|
消息中间件 Java RocketMQ
【Java】Error creating bean with name ‘functionBindingRegistrar‘ defined in class path resource的一种解决方式
【Java】Error creating bean with name ‘functionBindingRegistrar‘ defined in class path resource的一种解决方式
51 0
|
9月前
|
XML Java 程序员
Spring启动报错--class path resource [Beans.xml] cannot be opened because it does not exist
程序员不是在去生产bug的路上,那就是在去解决bug的路上。🤣🤣🤣🤣
85 1
|
10月前
|
druid
A bean with that name has already been defined in class path resource and overriding is disabled.
A bean with that name has already been defined in class path resource and overriding is disabled.
147 0
|
10月前
Field userDao in com.sd.sbmb.service.impl.UserServiceImpl required a bean of type ‘com.sd.sbmb.dao.U
Field userDao in com.sd.sbmb.service.impl.UserServiceImpl required a bean of type ‘com.sd.sbmb.dao.U
146 0
|
12月前
|
Java 测试技术 Spring
Configuration problem: Failed to import bean definitions from URL location classpath:spring-mvc.xml
Configuration problem: Failed to import bean definitions from URL location classpath:spring-mvc.xml
607 0
|
Nacos
Nacos报错:Error creating bean with name ‘authFilterRegistration‘ defined in class path resource
Nacos报错:Error creating bean with name ‘authFilterRegistration‘ defined in class path resource
354 0