当前位置:   article > 正文

整合redistemplate报错: a bean of type 'org.springframework.data.redis.core.RedisTemplate' not find_required a bean of type 'org.springframework.data.

required a bean of type 'org.springframework.data.redis.core.redistemplate

 问题环境:SpringBoot 连接redis  集群

1.启动引导类:

  1. package com.zengjx;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * @ClassName HelloController
  6. * @Description TODO
  7. * @Author zengjx
  8. * @Company zengjx
  9. * @Date 2019/12/6 14:38
  10. * @Version V1.0
  11. */
  12. @SpringBootApplication
  13. public class RedisDemoApplication {
  14. public static void main(String[] args) {
  15. SpringApplication.run(RedisDemoApplication.class);
  16. }
  17. }

2.配置文件:appliction.yml

  1. spring:
  2. datasource:
  3. driver-class-name: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://192.168.211.132:3306/changgou_user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
  5. username: root
  6. password: 123456
  7. application:
  8. name: redis-demo
  9. #redis配置
  10. redis:
  11. cluster:
  12. nodes:
  13. - 192.168.211.132:7001
  14. - 192.168.211.132:7002
  15. - 192.168.211.132:7003
  16. - 192.168.211.132:7004
  17. - 192.168.211.132:7005
  18. - 192.168.211.132:7006
  19. server:
  20. ssl:
  21. enabled: false
  22. port: 9008
  23. mybatis:
  24. configuration:
  25. map-underscore-to-camel-case: true

3. 测试类

  1. import org.junit.Test;
  2. import org.junit.runner.RunWith;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.test.context.SpringBootTest;
  5. import org.springframework.data.redis.core.RedisTemplate;
  6. import org.springframework.test.context.junit4.SpringRunner;
  7. import javax.annotation.Resource;
  8. /**
  9. * @ClassName HelloController
  10. * @Description TODO
  11. * @Author zengjx
  12. * @Company zengjx
  13. * @Date 2019/12/6 14:39
  14. * @Version V1.0
  15. */
  16. @SpringBootTest(classes = RedisDemoApplicationTests.class)
  17. @RunWith(SpringRunner.class)
  18. public class RedisDemoApplicationTests {
  19. @Autowired
  20. // @Resource
  21. RedisTemplate<String, Object> redisTemplate;
  22. @Test
  23. public void contextLoads() {
  24. redisTemplate.boundValueOps("name").set("zengjx");
  25. System.out.println(redisTemplate.boundValueOps("name").get());
  26. }
  27. }

 4.  运行测试类出现问题

 

  1. Description:
  2. Field redisTemplate in RedisDemoApplicationTests required a bean of type 'org.springframework.data.redis.core.RedisTemplate' that could not be found.
  3. The injection point has the following annotations:
  4. - @org.springframework.beans.factory.annotation.Autowired(required=true)
  5. Action:
  6. Consider defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration.
  7. 2019-12-06 15:04:41.880 ERROR 25024 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@52719fb6] to prepare test instance [RedisDemoApplicationTests@1c807b1d]
  8. java.lang.IllegalStateException: Failed to load ApplicationContext

5.网上的一些解决方法

  • @Resource

https://blog.csdn.net/weixin_33913332/article/details/92395338

  • RedisTemplate< String, Object>注入

 https://blog.csdn.net/zhaoheng314/article/details/81564166

6.解决方法

原因是aplication.yml   redis 密码要改成 root  

问题1;测试类没有在一个包里面 

问题2:@SpringBootTest(启动类.class) 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/125779
推荐阅读
相关标签
  

闽ICP备14008679号