Skip to content

Commit 0a2611b

Browse files
committed
Harmonize NoUniqueBeanDefinitionException message
This commit makes sure that the programmatic exception that is thrown by the cache abstraction uses the same message structure as a default message produced by NoUniqueBeanDefinitionException. Closes gh-33305
1 parent 29dce74 commit 0a2611b

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ void multipleCacheManagerBeans() {
9595
}
9696
catch (NoUniqueBeanDefinitionException ex) {
9797
assertThat(ex.getMessage()).contains(
98-
"no CacheResolver specified and expected a single CacheManager bean, but found 2: [cm1,cm2]");
98+
"no CacheResolver specified and expected single matching CacheManager but found 2: cm1,cm2");
99+
assertThat(ex.getNumberOfBeansFound()).isEqualTo(2);
100+
assertThat(ex.getBeanNamesFound()).containsExactly("cm1", "cm2");
99101
}
100102
}
101103

spring-beans/src/main/java/org/springframework/beans/factory/NoUniqueBeanDefinitionException.java

Lines changed: 16 additions & 4 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.
@@ -29,6 +29,7 @@
2929
* multiple matching candidates have been found when only one matching bean was expected.
3030
*
3131
* @author Juergen Hoeller
32+
* @author Stephane Nicoll
3233
* @since 3.2.1
3334
* @see BeanFactory#getBean(Class)
3435
*/
@@ -41,6 +42,19 @@ public class NoUniqueBeanDefinitionException extends NoSuchBeanDefinitionExcepti
4142
private final Collection<String> beanNamesFound;
4243

4344

45+
/**
46+
* Create a new {@code NoUniqueBeanDefinitionException}.
47+
* @param type required type of the non-unique bean
48+
* @param beanNamesFound the names of all matching beans (as a Collection)
49+
* @param message detailed message describing the problem
50+
* @since 6.2
51+
*/
52+
public NoUniqueBeanDefinitionException(Class<?> type, Collection<String> beanNamesFound, String message) {
53+
super(type, message);
54+
this.numberOfBeansFound = beanNamesFound.size();
55+
this.beanNamesFound = new ArrayList<>(beanNamesFound);
56+
}
57+
4458
/**
4559
* Create a new {@code NoUniqueBeanDefinitionException}.
4660
* @param type required type of the non-unique bean
@@ -59,10 +73,8 @@ public NoUniqueBeanDefinitionException(Class<?> type, int numberOfBeansFound, St
5973
* @param beanNamesFound the names of all matching beans (as a Collection)
6074
*/
6175
public NoUniqueBeanDefinitionException(Class<?> type, Collection<String> beanNamesFound) {
62-
super(type, "expected single matching bean but found " + beanNamesFound.size() + ": " +
76+
this(type, beanNamesFound, "expected single matching bean but found " + beanNamesFound.size() + ": " +
6377
StringUtils.collectionToCommaDelimitedString(beanNamesFound));
64-
this.numberOfBeansFound = beanNamesFound.size();
65-
this.beanNamesFound = new ArrayList<>(beanNamesFound);
6678
}
6779

6880
/**

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,22 @@ public void afterSingletonsInstantiated() {
270270
setCacheManager(this.beanFactory.getBean(CacheManager.class));
271271
}
272272
catch (NoUniqueBeanDefinitionException 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("]");
273+
int numberOfBeansFound = ex.getNumberOfBeansFound();
274+
Collection<String> beanNamesFound = ex.getBeanNamesFound();
275+
276+
StringBuilder message = new StringBuilder("no CacheResolver specified and expected single matching CacheManager but found ");
277+
message.append(numberOfBeansFound);
278+
if (beanNamesFound != null) {
279+
message.append(": ").append(StringUtils.collectionToCommaDelimitedString(beanNamesFound));
280+
}
281+
String exceptionMessage = message.toString();
282+
283+
if (beanNamesFound != null) {
284+
throw new NoUniqueBeanDefinitionException(CacheManager.class, beanNamesFound, exceptionMessage);
285+
}
286+
else {
287+
throw new NoUniqueBeanDefinitionException(CacheManager.class, numberOfBeansFound, exceptionMessage);
277288
}
278-
message.append(" - mark one as primary or declare a specific CacheManager to use.");
279-
throw new NoUniqueBeanDefinitionException(CacheManager.class, ex.getNumberOfBeansFound(), message.toString());
280289
}
281290
catch (NoSuchBeanDefinitionException ex) {
282291
throw new NoSuchBeanDefinitionException(CacheManager.class, "no CacheResolver specified - "

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,12 @@ void multipleCacheManagerBeans() {
9292
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
9393
ctx.register(MultiCacheManagerConfig.class);
9494
assertThatThrownBy(ctx::refresh)
95-
.isInstanceOf(NoUniqueBeanDefinitionException.class)
96-
.hasMessageContaining("no CacheResolver specified and expected a single CacheManager bean, but found 2: [cm1,cm2]")
97-
.hasNoCause();
95+
.isInstanceOfSatisfying(NoUniqueBeanDefinitionException.class, ex -> {
96+
assertThat(ex.getMessage()).contains(
97+
"no CacheResolver specified and expected single matching CacheManager but found 2: cm1,cm2");
98+
assertThat(ex.getNumberOfBeansFound()).isEqualTo(2);
99+
assertThat(ex.getBeanNamesFound()).containsExactly("cm1", "cm2");
100+
}).hasNoCause();
98101
}
99102

100103
@Test

0 commit comments

Comments
 (0)