找不到类型为'org.springframework.data.redis.cache.RedisCacheManager'的bean

时间:2019-12-23 10:00:15

标签: spring spring-boot redis spring-data-redis

application.properties

spring:
  cache.type: redis
  redis.host: localhost
  redis.port: 6379
  redis.password: test123
  redis.timeout: 5000

CacheConfig.java

@Configuration
public class CacheConfig extends CachingConfigurerSupport {

    public static final String TIME_ZONE_CACHE = "TimeZone";

    private static final Integer THIRTY_MINUTES_TTL = 30;


    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        Map<String, RedisCacheConfiguration> cacheNamesConfigurationMap = new HashMap<>();

        cacheNamesConfigurationMap.put(TIME_ZONE_CACHE, RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(THIRTY_MINUTES_TTL)));
        return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(connectionFactory).withInitialCacheConfigurations(cacheNamesConfigurationMap).build();
    }

}

RedisCacheService.java

@Component("RedisCacheService")
public class RedisCacheService implements IRedisCacheService {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RedisCacheManager redisCacheManager;

    @Override
    @Transactional
    public void clearCacheForCacheName(String cacheName) {
        Cache cache = redisCacheManager.getCache(cacheName);
        if(cache == null) {
            throw new IllegalArgumentException("No Cache available...");
        }
        cache.clear();
        log.info("Cache has been evicted for {}",cacheName);
    }

}

启动服务器时出现错误消息。

*************************** APPLICATION FAILED TO START
***************************

Description:

Field redisCacheManager in com.practice.services.RedisCacheService required a bean of type 'org.springframework.data.redis.cache.RedisCacheManager' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.data.redis.cache.RedisCacheManager' in your configuration.

请帮助我解决这个问题。

0 个答案:

没有答案