CacheManger

황상익·2024년 8월 22일
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'searching_program.search_product.service.ItemServiceIntegrationTest': Unsatisfied dependency expressed through field 'cacheManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cache.CacheManager' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Test 도중 Cache 하는 부분이 Bean 인식을 못했다는 이야기인 것 같다..

  1. SpringCache 활성화
    @EnableCaching 어노테이션을 추가해 캐시 활성화.

  2. SpringBootTest 통합 테스트 수행
    통합 테스트에서는 애플리케이션의 모든 컨텍스트가 로드.

  3. 캐시 설정 확인
    application.properties에서 캐시와 관련된 설정을 명시

spring.cache.type=simple

CachManger 빈을 직접 정의

import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CacheConfig {

    @Bean
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager("itemsByName");
    }
}

나는 일단 test 돌리는 중이다 보니 EnableCaching을 선택했다.
하지만 추후에 작업을 위해 Config로 구성해서 Cache 설정을 직접 해주는게 좋다고 본다.

profile
개발자를 향해 가는 중입니다~! 항상 겸손

0개의 댓글