Rethrow original BeanCreationException if fallback producer fails
Closes gh-22689
This commit is contained in:
@@ -26,6 +26,7 @@ import org.hibernate.resource.beans.container.spi.ContainedBean;
|
||||
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.lang.Nullable;
|
||||
@@ -155,7 +156,22 @@ public final class SpringBeanContainer implements BeanContainer {
|
||||
logger.debug("Falling back to Hibernate's default producer after bean creation failure for " +
|
||||
beanType + ": " + ex);
|
||||
}
|
||||
return new SpringContainedBean<>(fallbackProducer.produceBeanInstance(beanType));
|
||||
try {
|
||||
return new SpringContainedBean<>(fallbackProducer.produceBeanInstance(beanType));
|
||||
}
|
||||
catch (RuntimeException ex2) {
|
||||
if (ex instanceof BeanCreationException) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Fallback producer failed for " + beanType + ": " + ex2);
|
||||
}
|
||||
// Rethrow original Spring exception from first attempt.
|
||||
throw ex;
|
||||
}
|
||||
else {
|
||||
// Throw fallback producer exception since original was probably NoSuchBeanDefinitionException.
|
||||
throw ex2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,9 +193,24 @@ public final class SpringBeanContainer implements BeanContainer {
|
||||
catch (BeansException ex) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Falling back to Hibernate's default producer after bean creation failure for " +
|
||||
beanType + ": " + ex);
|
||||
beanType + " with name '" + name + "': " + ex);
|
||||
}
|
||||
try {
|
||||
return new SpringContainedBean<>(fallbackProducer.produceBeanInstance(name, beanType));
|
||||
}
|
||||
catch (RuntimeException ex2) {
|
||||
if (ex instanceof BeanCreationException) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Fallback producer failed for " + beanType + " with name '" + name + "': " + ex2);
|
||||
}
|
||||
// Rethrow original Spring exception from first attempt.
|
||||
throw ex;
|
||||
}
|
||||
else {
|
||||
// Throw fallback producer exception since original was probably NoSuchBeanDefinitionException.
|
||||
throw ex2;
|
||||
}
|
||||
}
|
||||
return new SpringContainedBean<>(fallbackProducer.produceBeanInstance(name, beanType));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user