diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/NoUniqueBeanDefinitionException.java b/spring-beans/src/main/java/org/springframework/beans/factory/NoUniqueBeanDefinitionException.java index 5744a73b1b..9e30f2f72c 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/NoUniqueBeanDefinitionException.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/NoUniqueBeanDefinitionException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.beans.factory; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -61,7 +62,7 @@ public class NoUniqueBeanDefinitionException extends NoSuchBeanDefinitionExcepti super(type, "expected single matching bean but found " + beanNamesFound.size() + ": " + StringUtils.collectionToCommaDelimitedString(beanNamesFound)); this.numberOfBeansFound = beanNamesFound.size(); - this.beanNamesFound = beanNamesFound; + this.beanNamesFound = new ArrayList<>(beanNamesFound); } /** @@ -83,7 +84,7 @@ public class NoUniqueBeanDefinitionException extends NoSuchBeanDefinitionExcepti super(type, "expected single matching bean but found " + beanNamesFound.size() + ": " + StringUtils.collectionToCommaDelimitedString(beanNamesFound)); this.numberOfBeansFound = beanNamesFound.size(); - this.beanNamesFound = beanNamesFound; + this.beanNamesFound = new ArrayList<>(beanNamesFound); } /** diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/DependencyDescriptor.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/DependencyDescriptor.java index c161e9994e..4825563735 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/DependencyDescriptor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/DependencyDescriptor.java @@ -21,7 +21,6 @@ import java.io.ObjectInputStream; import java.io.Serializable; import java.lang.annotation.Annotation; import java.lang.reflect.Field; -import java.util.HashSet; import java.util.Map; import java.util.Optional; @@ -216,7 +215,7 @@ public class DependencyDescriptor extends InjectionPoint implements Serializable */ @Nullable public Object resolveNotUnique(ResolvableType type, Map matchingBeans) throws BeansException { - throw new NoUniqueBeanDefinitionException(type, new HashSet<>( matchingBeans.keySet() )); + throw new NoUniqueBeanDefinitionException(type, matchingBeans.keySet()); } /** diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index ee9e89d16e..d8c54764cd 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -30,7 +30,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Comparator; -import java.util.HashSet; import java.util.IdentityHashMap; import java.util.Iterator; import java.util.LinkedHashSet; @@ -1297,7 +1296,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto return new NamedBeanHolder<>(candidateName, (T) beanInstance); } if (!nonUniqueAsNull) { - throw new NoUniqueBeanDefinitionException(requiredType, new HashSet<>(candidates.keySet())); + throw new NoUniqueBeanDefinitionException(requiredType, candidates.keySet()); } }