Browse Source

Provide nested cause when a suitable CacheManager cannot be found

Closes gh-25250
pull/26773/head
Stephane Nicoll 4 years ago
parent
commit
607d918340
  1. 6
      spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java
  2. 6
      spring-context/src/test/java/org/springframework/cache/config/EnableCachingTests.java

6
spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java vendored

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -221,11 +221,11 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
} }
catch (NoUniqueBeanDefinitionException ex) { catch (NoUniqueBeanDefinitionException ex) {
throw new IllegalStateException("No CacheResolver specified, and no unique bean of type " + 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."); "CacheManager found. Mark one as primary or declare a specific CacheManager to use.", ex);
} }
catch (NoSuchBeanDefinitionException ex) { catch (NoSuchBeanDefinitionException ex) {
throw new IllegalStateException("No CacheResolver specified, and no bean of type CacheManager found. " + 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."); "Register a CacheManager bean or remove the @EnableCaching annotation from your configuration.", ex);
} }
} }
this.initialized = true; this.initialized = true;

6
spring-context/src/test/java/org/springframework/cache/config/EnableCachingTests.java vendored

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,6 +19,8 @@ package org.springframework.cache.config;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.annotation.EnableCaching;
@ -87,6 +89,7 @@ public class EnableCachingTests extends AbstractCacheAnnotationTests {
} }
catch (IllegalStateException ex) { catch (IllegalStateException ex) {
assertThat(ex.getMessage().contains("no unique bean of type CacheManager")).isTrue(); assertThat(ex.getMessage().contains("no unique bean of type CacheManager")).isTrue();
assertThat(ex).hasCauseInstanceOf(NoUniqueBeanDefinitionException.class);
} }
} }
@ -121,6 +124,7 @@ public class EnableCachingTests extends AbstractCacheAnnotationTests {
} }
catch (IllegalStateException ex) { catch (IllegalStateException ex) {
assertThat(ex.getMessage().contains("no bean of type CacheManager")).isTrue(); assertThat(ex.getMessage().contains("no bean of type CacheManager")).isTrue();
assertThat(ex).hasCauseInstanceOf(NoSuchBeanDefinitionException.class);
} }
} }

Loading…
Cancel
Save