报错:
[ERROR] Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisUtils': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'stringRedisTemplate' is expected to be of type 'org.springframework.data.redis.core.StringRedisTemplate' but was actually of type 'org.springframework.data.redis.core.RedisTemplate':
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'stringRedisTemplate' is expected to be of type 'org.springframework.data.redis.core.StringRedisTemplate' but was actually of type 'org.springframework.data.redis.core.RedisTemplate'
项目A依赖于项目B,引入的B的包
B中有一工具类RedisUtils被依赖进来
@Component
public class RedisUtils {
@Resource
private StringRedisTemplate stringRedisTemplate;
...
}
B中StringRedisTemplate的类型是
org.springframework.data.redis.core.StringRedisTemplate
而A也引入了StringRedisTemplate,类型是
org.springframework.data.redis.core.RedisTemplate
因为@Resource,根据名称自动注入,所以引入了A里的StringRedisTemplate,导致了报错
解决方式:将B的工具类中,@Resource改为@Autowired(required=false)(根据类型引入,没有也可以)即可