본문 바로가기
트러블슈팅

RedisConfig의 복수의 CacheManager 문제

by 우 석 2024. 4. 15.

[문제 상황]

프로젝트를 실행중 아래의 Exception 발생 No CacheResolver specified, and no unique bean of type CacheManager found. Mark one as primary or declare a specific CacheManager to use. excetion

 

[원인]

설정이 다른 서비스를 위해 RedisConfig에 추가로 CacheManager를 생성하였습니다. 여러개의 CacheManager가 프로젝트 실행시에 올라와서 발생한 문제였습니다.

// CacheAspectSupport.class
@Override
public void afterSingletonsInstantiated() {
	if (getCacheResolver() == null) {
		// Lazily initialize cache resolver via default cache manager
		Assert.state(this.beanFactory != null, "CacheResolver or BeanFactory must be set on cache aspect");
		try {
			setCacheManager(this.beanFactory.getBean(CacheManager.class));
		}
		catch (NoUniqueBeanDefinitionException ex) {
			throw new IllegalStateException("No CacheResolver specified, and no unique bean of type " +
					"CacheManager found. Mark one as primary or declare a specific CacheManager to use.", ex);
		}
		catch (NoSuchBeanDefinitionException ex) {
			throw new IllegalStateException("No CacheResolver specified, and no bean of type CacheManager found. " +
					"Register a CacheManager bean or remove the @EnableCaching annotation from your configuration.", ex);
		}
	}
	this.initialized = true;
	}

 

[해결 과정]

CacheManager중 하나를 Primary로 등록해주어야 하며 하나의 CacheManager에 @Primary 어노테이션 붙혀서 기본 CacheManager 값을 지정하여 해결

    @Bean(name = "cacheManager")
    @Primary
    public CacheManager cacheManager() {
        RedisCacheManager.RedisCacheManagerBuilder builder =
            RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(
                redisConnectionFactory());

        RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig()
            .serializeValuesWith(
                RedisSerializationContext.SerializationPair.fromSerializer(
                    new GenericJackson2JsonRedisSerializer())) // Value Serializer 변경
            .disableCachingNullValues()
            .entryTtl(Duration.ofMinutes(10L));

        builder.cacheDefaults(configuration);

        return builder.build();
    }

'트러블슈팅' 카테고리의 다른 글

동시성 제어  (0) 2024.05.08
스케쥴링  (0) 2024.04.25
배포전 로컬 테스트 중 데이터베이스 연결 문제  (0) 2024.04.19
Spring Day 22 : MySQL 테스트 데이터 생성  (0) 2024.02.23
.gitignore : 파일 숨기기  (2) 2024.01.31