Rethrow original BeanCreationException if fallback producer fails

Closes gh-22689
This commit is contained in:
Juergen Hoeller
2019-03-27 16:01:18 +01:00
parent c54355784e
commit ca24fd50b0
4 changed files with 95 additions and 30 deletions

View File

@@ -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));
}
}