From 1b409d5290bf5d6a1598a76623af894ab1c57beb Mon Sep 17 00:00:00 2001 From: Per Lundberg Date: Thu, 29 Dec 2022 15:04:47 +0200 Subject: [PATCH 1/2] Make sure NoUniqueBeanDefinitionException to be serializable See gh-29753 --- .../beans/factory/config/DependencyDescriptor.java | 3 ++- .../beans/factory/support/DefaultListableBeanFactory.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) 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 4825563735f..c161e9994ec 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,6 +21,7 @@ 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; @@ -215,7 +216,7 @@ public class DependencyDescriptor extends InjectionPoint implements Serializable */ @Nullable public Object resolveNotUnique(ResolvableType type, Map matchingBeans) throws BeansException { - throw new NoUniqueBeanDefinitionException(type, matchingBeans.keySet()); + throw new NoUniqueBeanDefinitionException(type, new HashSet<>( 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 d8c54764cd2..ee9e89d16e0 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,6 +30,7 @@ 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; @@ -1296,7 +1297,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto return new NamedBeanHolder<>(candidateName, (T) beanInstance); } if (!nonUniqueAsNull) { - throw new NoUniqueBeanDefinitionException(requiredType, candidates.keySet()); + throw new NoUniqueBeanDefinitionException(requiredType, new HashSet<>(candidates.keySet())); } } From 1396daa4b62b6f15f6d759b6ef0d2d3f271231b3 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Sun, 27 Aug 2023 17:59:04 +0200 Subject: [PATCH 2/2] Polish "Make sure NoUniqueBeanDefinitionException to be serializable" See gh-29753 --- .../beans/factory/NoUniqueBeanDefinitionException.java | 7 ++++--- .../beans/factory/config/DependencyDescriptor.java | 3 +-- .../beans/factory/support/DefaultListableBeanFactory.java | 3 +-- 3 files changed, 6 insertions(+), 7 deletions(-) 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 5744a73b1b0..9e30f2f72c5 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 c161e9994ec..4825563735f 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 ee9e89d16e0..d8c54764cd2 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()); } }