웹 개발/에러 해결
Cannot find cache named '' for Builder[] caches=[] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false' 에러 해결
희랍인 조르바
2020. 8. 6. 23:32
이 포스팅에서 제시하는 해결방법은 spring-cloud-aws를 사용하고 있을시에만 해당함.
java.lang.IllegalArgumentException:
Cannot find cache named '캐시 이름' for Builder[캐싱한 메서드 위치] caches=[캐시 이름]
| key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'
빠른 응답성을 위해 사용하는 Cache.
스프링에서도 이런 Caching 기능을 제공하고 있는데, @Cacheable을 사용하고 있는 메서드에서 위와 같은 에러가 발생했다.
최근 헤로쿠에서 AWS로 옮겨가면서 spring cloud aws 디펜던시들이 몇개 추가됐었는데, 이것들이 원인이었다.
spring cloud aws에서 autoConfiguration으로 spring이 caching할 때 ElastiCache를 사용하도록 강제해버린다.
spring-cloud-aws-autoconfigure에서 ElastiCacheAutoConfiguration이 존재하고 있고, @EnableElastiCache이란 애너테이션을 통해 스프링은 Elastic cache를 caching 도구로 사용하는 것이다.
ElastiCacheAutoConfiguration 클래스
package org.springframework.cloud.aws.autoconfigure.cache;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.aws.autoconfigure.context.ContextCredentialsAutoConfiguration;
import org.springframework.cloud.aws.cache.config.annotation.EnableElastiCache;
import org.springframework.cloud.aws.context.annotation.ConditionalOnAwsCloudEnvironment;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* @author Agim Emruli
*/
@Configuration(proxyBeanMethods = false)
@Import(ContextCredentialsAutoConfiguration.class)
@EnableElastiCache
@ConditionalOnClass(name = "com.amazonaws.services.elasticache.AmazonElastiCache")
@ConditionalOnAwsCloudEnvironment
public class ElastiCacheAutoConfiguration {
}
@EnableElastiCache
@EnableCaching // 스프링의 캐싱 기능을 사용하게 해주는 애너테이션
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import(ElastiCacheCachingConfiguration.class)
public @interface EnableElastiCache {
// 길어서 생략..
}
기존 설정을 해둔 것과 달리 ElastiCache를 강제로 사용하니 정상동작하지 않는 것이다.
해결 방법
원래의 설정에 맞춰 동작하게 해주려면 기존 @EnableCaching을 선언하고 Configuration으로 사용하는 클래스에서 아래처럼 AutoConfiguration에서 ElastiCacheAutoConfiguration를 읽지 않도록 코드를 추가해주면 된다.
쉽고 간단하다!
@EnableAutoConfiguration(exclude = ElastiCacheAutoConfiguration.class) // ElastiCacheAutoConfiguration를 읽지 않도록 제외시킨다.
@Configuration
@EnableCaching
public class CacheConfiguration {
}