整合redistemplate报错: a bean of type 'org.springframework.data.redis.core.RedisTemplate' not find
·
问题环境:SpringBoot 连接redis 集群
1.启动引导类:
package com.zengjx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @ClassName HelloController
* @Description TODO
* @Author zengjx
* @Company zengjx
* @Date 2019/12/6 14:38
* @Version V1.0
*/
@SpringBootApplication
public class RedisDemoApplication {
public static void main(String[] args) {
SpringApplication.run(RedisDemoApplication.class);
}
}
2.配置文件:appliction.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.211.132:3306/changgou_user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
application:
name: redis-demo
#redis配置
redis:
cluster:
nodes:
- 192.168.211.132:7001
- 192.168.211.132:7002
- 192.168.211.132:7003
- 192.168.211.132:7004
- 192.168.211.132:7005
- 192.168.211.132:7006
server:
ssl:
enabled: false
port: 9008
mybatis:
configuration:
map-underscore-to-camel-case: true
3. 测试类
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
/**
* @ClassName HelloController
* @Description TODO
* @Author zengjx
* @Company zengjx
* @Date 2019/12/6 14:39
* @Version V1.0
*/
@SpringBootTest(classes = RedisDemoApplicationTests.class)
@RunWith(SpringRunner.class)
public class RedisDemoApplicationTests {
@Autowired
// @Resource
RedisTemplate<String, Object> redisTemplate;
@Test
public void contextLoads() {
redisTemplate.boundValueOps("name").set("zengjx");
System.out.println(redisTemplate.boundValueOps("name").get());
}
}
4. 运行测试类出现问题

Description:
Field redisTemplate in RedisDemoApplicationTests required a bean of type 'org.springframework.data.redis.core.RedisTemplate' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration.
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]
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)
更多推荐


所有评论(0)