Skip to content

Commit 9492d88

Browse files
committed
Stop wrapping low-level exceptions in CacheAspectSupport initialization
This commit replaces the IllegalStateException thrown in CacheAspectSupport when a CacheManager cannot be determined. These were added to provide a dedicated error message, but it is possible to do so without hiding the more adequate exception type. Closes gh-22442
1 parent 9b85c93 commit 9492d88

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

spring-aspects/src/test/java/org/springframework/cache/aspectj/AspectJEnableCachingIsolatedTests.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.junit.jupiter.api.Disabled;
2121
import org.junit.jupiter.api.Test;
2222

23+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
24+
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
2325
import org.springframework.cache.CacheManager;
2426
import org.springframework.cache.annotation.CachingConfigurer;
2527
import org.springframework.cache.annotation.EnableCaching;
@@ -91,8 +93,9 @@ void multipleCacheManagerBeans() {
9193
try {
9294
load(MultiCacheManagerConfig.class);
9395
}
94-
catch (IllegalStateException ex) {
95-
assertThat(ex.getMessage()).contains("bean of type CacheManager");
96+
catch (NoUniqueBeanDefinitionException ex) {
97+
assertThat(ex.getMessage()).contains(
98+
"no CacheResolver specified and expected a single CacheManager bean, but found 2: [cm1,cm2]");
9699
}
97100
}
98101

@@ -116,8 +119,8 @@ void noCacheManagerBeans() {
116119
try {
117120
load(EmptyConfig.class);
118121
}
119-
catch (IllegalStateException ex) {
120-
assertThat(ex.getMessage()).contains("no bean of type CacheManager");
122+
catch (NoSuchBeanDefinitionException ex) {
123+
assertThat(ex.getMessage()).contains("no CacheResolver specified");
121124
}
122125
}
123126

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,17 @@ public void afterSingletonsInstantiated() {
270270
setCacheManager(this.beanFactory.getBean(CacheManager.class));
271271
}
272272
catch (NoUniqueBeanDefinitionException ex) {
273-
throw new IllegalStateException("No CacheResolver specified, and no unique bean of type " +
274-
"CacheManager found. Mark one as primary or declare a specific CacheManager to use.", ex);
273+
StringBuilder message = new StringBuilder("no CacheResolver specified and expected a single CacheManager bean, but found ");
274+
message.append(ex.getNumberOfBeansFound());
275+
if (ex.getBeanNamesFound() != null) {
276+
message.append(": [").append(StringUtils.collectionToCommaDelimitedString(ex.getBeanNamesFound())).append("]");
277+
}
278+
message.append(" - mark one as primary or declare a specific CacheManager to use.");
279+
throw new NoUniqueBeanDefinitionException(CacheManager.class, ex.getNumberOfBeansFound(), message.toString());
275280
}
276281
catch (NoSuchBeanDefinitionException ex) {
277-
throw new IllegalStateException("No CacheResolver specified, and no bean of type CacheManager found. " +
278-
"Register a CacheManager bean or remove the @EnableCaching annotation from your configuration.", ex);
282+
throw new NoSuchBeanDefinitionException(CacheManager.class, "no CacheResolver specified - "
283+
+ "register a CacheManager bean or remove the @EnableCaching annotation from your configuration.");
279284
}
280285
}
281286
this.initialized = true;

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -89,13 +89,12 @@ void singleCacheManagerBean() {
8989

9090
@Test
9191
void multipleCacheManagerBeans() {
92-
@SuppressWarnings("resource")
9392
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
9493
ctx.register(MultiCacheManagerConfig.class);
9594
assertThatThrownBy(ctx::refresh)
96-
.isInstanceOf(IllegalStateException.class)
97-
.hasMessageContaining("no unique bean of type CacheManager")
98-
.hasCauseInstanceOf(NoUniqueBeanDefinitionException.class);
95+
.isInstanceOf(NoUniqueBeanDefinitionException.class)
96+
.hasMessageContaining("no CacheResolver specified and expected a single CacheManager bean, but found 2: [cm1,cm2]")
97+
.hasNoCause();
9998
}
10099

101100
@Test
@@ -117,13 +116,14 @@ void multipleCachingConfigurers() {
117116

118117
@Test
119118
void noCacheManagerBeans() {
120-
@SuppressWarnings("resource")
121119
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
122120
ctx.register(EmptyConfig.class);
123121
assertThatThrownBy(ctx::refresh)
124-
.isInstanceOf(IllegalStateException.class)
125-
.hasMessageContaining("no bean of type CacheManager")
126-
.hasCauseInstanceOf(NoSuchBeanDefinitionException.class);
122+
.isInstanceOf(NoSuchBeanDefinitionException.class)
123+
.hasMessageContaining("no CacheResolver specified")
124+
.hasMessageContaining(
125+
"register a CacheManager bean or remove the @EnableCaching annotation from your configuration.")
126+
.hasNoCause();
127127
}
128128

129129
@Test

0 commit comments

Comments
 (0)