From 61894af0bde55562662c12bb6d11e32b732dfd5e Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 28 Jun 2024 17:55:45 +0200 Subject: [PATCH 1/2] Expose FactoryBean attribute exception as BeanDefinitionStoreException Closes gh-33117 --- .../support/AbstractAutowireCapableBeanFactory.java | 13 ++++++++++--- .../beans/factory/support/AbstractBeanFactory.java | 11 ++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java index 04bc1ede340f..6569f446fece 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java @@ -843,10 +843,17 @@ protected Class getTypeForFactoryMethod(String beanName, RootBeanDefinition m */ @Override protected ResolvableType getTypeForFactoryBean(String beanName, RootBeanDefinition mbd, boolean allowInit) { + ResolvableType result; + // Check if the bean definition itself has defined the type with an attribute - ResolvableType result = getTypeForFactoryBeanFromAttributes(mbd); - if (result != ResolvableType.NONE) { - return result; + try { + result = getTypeForFactoryBeanFromAttributes(mbd); + if (result != ResolvableType.NONE) { + return result; + } + } + catch (IllegalArgumentException ex) { + throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, ex.getMessage()); } // For instance supplied beans, try the target type and bean class immediately diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java index d8b8d0c60956..9d9ba6cc4638 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java @@ -1716,9 +1716,14 @@ protected boolean isFactoryBean(String beanName, RootBeanDefinition mbd) { * @see #getBean(String) */ protected ResolvableType getTypeForFactoryBean(String beanName, RootBeanDefinition mbd, boolean allowInit) { - ResolvableType result = getTypeForFactoryBeanFromAttributes(mbd); - if (result != ResolvableType.NONE) { - return result; + try { + ResolvableType result = getTypeForFactoryBeanFromAttributes(mbd); + if (result != ResolvableType.NONE) { + return result; + } + } + catch (IllegalArgumentException ex) { + throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, ex.getMessage()); } if (allowInit && mbd.isSingleton()) { From 61adf2dd255b2b32ccba7f051f8b11f40b567056 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 28 Jun 2024 18:09:39 +0200 Subject: [PATCH 2/2] Formal null safety for exception message through String.valueOf See gh-33117 --- .../factory/support/AbstractAutowireCapableBeanFactory.java | 3 ++- .../beans/factory/support/AbstractBeanFactory.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java index 6569f446fece..ae9656818cc0 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java @@ -853,7 +853,8 @@ protected ResolvableType getTypeForFactoryBean(String beanName, RootBeanDefiniti } } catch (IllegalArgumentException ex) { - throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, ex.getMessage()); + throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, + String.valueOf(ex.getMessage())); } // For instance supplied beans, try the target type and bean class immediately diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java index 9d9ba6cc4638..824d86e29e22 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java @@ -1723,7 +1723,8 @@ protected ResolvableType getTypeForFactoryBean(String beanName, RootBeanDefiniti } } catch (IllegalArgumentException ex) { - throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, ex.getMessage()); + throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, + String.valueOf(ex.getMessage())); } if (allowInit && mbd.isSingleton()) {