Spring 使用 @EnableCaching 报错

我的项目是 SSM+Shiro+redis
今天要做redis的缓存,采用Spring data redis注解缓存.

redis配置如下:

@Configuration
@EnableCaching
public class CacheConfig {
    
    //缓存管理器
      @Bean  
        public CacheManager cacheManager(RedisTemplate redisTemplate) {  
            RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);  
      
            // Number of seconds before expiration. Defaults to unlimited (0)  
            cacheManager.setDefaultExpiration(3000); // Sets the default expire time (in seconds)  
            return cacheManager;  
        } 
    
      //连接工厂
    @Bean
    public RedisConnectionFactory redisCF(){
        JedisConnectionFactory cf = new JedisConnectionFactory();
        cf.setHostName("192.168.80.155");
        cf.setPort(6379);
        
        return cf;
    }
    
    //RedisTemplate
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf){
        
        RedisTemplate<String,String> template = new RedisTemplate<String,String>();
        //key会被转换为String类型,非String类型会报错
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //设置序列化为 json的对象类型,必须实现 serializable接口
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        
        
        
        template.setConnectionFactory(cf);
        template.setKeySerializer(stringRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        
        return template;
    }
    
//    @Bean
//    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory cf){
//        return new StringRedisTemplate();
//    }

}

然后启动就会报错,再去掉 @EnableCaching,启动正常,不知道什么原因,麻烦大家给分析一下

报错信息:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityManager' defined in file [C:\apache-tomcat-8.5.20-windows-x64\apache-tomcat-8.5.20\webapps\shop\WEB-INF\classes\spring-context-shiro.xml]: Cannot resolve reference to bean 'systemAuthorizingRealm' while setting bean property 'realm'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'systemAuthorizingRealm' defined in file [C:\apache-tomcat-8.5.20-windows-x64\apache-tomcat-8.5.20\webapps\shop\WEB-INF\classes\com\jeeplus\modules\sys\security\SystemAuthorizingRealm.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.config.internalTransactionAdvisor': Cannot resolve reference to bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0' while setting bean property 'transactionAttributeSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': Invocation of init method failed; nested exception is java.lang.IllegalStateException: No bean of type CacheManager could be found. Register a CacheManager bean or remove the @EnableCaching annotation from your configuration.

最后一个异常信息:

exception is java.lang.IllegalStateException: No bean of type CacheManager could be found. Register a CacheManager bean or remove the @EnableCaching annotation from your configuration.

我在satckoverflow 上找到一个相似的问题,但是看不懂:地址:https://stackoverflow.com/que...

希望知情者解答一下,感激不尽

阅读 9.6k
1 个回答
新手上路,请多包涵

你的CacheConfig没有覆盖CachingConfigurerSupport

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题