Skip to content

Commit 1396daa

Browse files
committed
Polish "Make sure NoUniqueBeanDefinitionException to be serializable"
See gh-29753
1 parent 1b409d5 commit 1396daa

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.beans.factory;
1818

19+
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.Collection;
2122

@@ -61,7 +62,7 @@ public NoUniqueBeanDefinitionException(Class<?> type, Collection<String> beanNam
6162
super(type, "expected single matching bean but found " + beanNamesFound.size() + ": " +
6263
StringUtils.collectionToCommaDelimitedString(beanNamesFound));
6364
this.numberOfBeansFound = beanNamesFound.size();
64-
this.beanNamesFound = beanNamesFound;
65+
this.beanNamesFound = new ArrayList<>(beanNamesFound);
6566
}
6667

6768
/**
@@ -83,7 +84,7 @@ public NoUniqueBeanDefinitionException(ResolvableType type, Collection<String> b
8384
super(type, "expected single matching bean but found " + beanNamesFound.size() + ": " +
8485
StringUtils.collectionToCommaDelimitedString(beanNamesFound));
8586
this.numberOfBeansFound = beanNamesFound.size();
86-
this.beanNamesFound = beanNamesFound;
87+
this.beanNamesFound = new ArrayList<>(beanNamesFound);
8788
}
8889

8990
/**

spring-beans/src/main/java/org/springframework/beans/factory/config/DependencyDescriptor.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.io.Serializable;
2222
import java.lang.annotation.Annotation;
2323
import java.lang.reflect.Field;
24-
import java.util.HashSet;
2524
import java.util.Map;
2625
import java.util.Optional;
2726

@@ -216,7 +215,7 @@ public boolean isEager() {
216215
*/
217216
@Nullable
218217
public Object resolveNotUnique(ResolvableType type, Map<String, Object> matchingBeans) throws BeansException {
219-
throw new NoUniqueBeanDefinitionException(type, new HashSet<>( matchingBeans.keySet() ));
218+
throw new NoUniqueBeanDefinitionException(type, matchingBeans.keySet());
220219
}
221220

222221
/**

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.Arrays;
3131
import java.util.Collection;
3232
import java.util.Comparator;
33-
import java.util.HashSet;
3433
import java.util.IdentityHashMap;
3534
import java.util.Iterator;
3635
import java.util.LinkedHashSet;
@@ -1297,7 +1296,7 @@ else if (candidateNames.length > 1) {
12971296
return new NamedBeanHolder<>(candidateName, (T) beanInstance);
12981297
}
12991298
if (!nonUniqueAsNull) {
1300-
throw new NoUniqueBeanDefinitionException(requiredType, new HashSet<>(candidates.keySet()));
1299+
throw new NoUniqueBeanDefinitionException(requiredType, candidates.keySet());
13011300
}
13021301
}
13031302

0 commit comments

Comments
 (0)