Always use given fallback producer in case of TypeBootstrapContext

Closes gh-30924
This commit is contained in:
Juergen Hoeller 2023-08-01 23:52:33 +02:00
parent 148f5c459e
commit 18e72d5c01
1 changed files with 16 additions and 1 deletions

View File

@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory;
import org.hibernate.resource.beans.container.spi.BeanContainer;
import org.hibernate.resource.beans.container.spi.ContainedBean;
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
import org.hibernate.type.spi.TypeBootstrapContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException;
@ -180,14 +181,28 @@ public final class SpringBeanContainer implements BeanContainer {
try {
if (lifecycleOptions.useJpaCompliantCreation()) {
Object bean = null;
if (fallbackProducer instanceof TypeBootstrapContext) {
// Special Hibernate type construction rules, including TypeBootstrapContext resolution.
bean = fallbackProducer.produceBeanInstance(name, beanType);
}
if (this.beanFactory.containsBean(name)) {
Object bean = this.beanFactory.autowire(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
if (bean == null) {
bean = this.beanFactory.autowire(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
}
this.beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
this.beanFactory.applyBeanPropertyValues(bean, name);
bean = this.beanFactory.initializeBean(bean, name);
return new SpringContainedBean<>(bean, beanInstance -> this.beanFactory.destroyBean(name, beanInstance));
}
else if (bean != null) {
// No bean found by name but constructed with TypeBootstrapContext rules
this.beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
bean = this.beanFactory.initializeBean(bean, name);
return new SpringContainedBean<>(bean, this.beanFactory::destroyBean);
}
else {
// No bean found by name -> construct by type using createBean
return new SpringContainedBean<>( // to be replaced with plain createBean(Class)
this.beanFactory.createBean(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false),
this.beanFactory::destroyBean);