Skip to content

Commit

Permalink
Always use given fallback producer in case of TypeBootstrapContext
Browse files Browse the repository at this point in the history
Closes gh-30924
  • Loading branch information
jhoeller committed Aug 1, 2023
1 parent 148f5c4 commit 18e72d5
Showing 1 changed file with 16 additions and 1 deletion.
Expand Up @@ -24,6 +24,7 @@
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;
Expand Down Expand Up @@ -180,14 +181,28 @@ private SpringContainedBean<?> createBean(

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);
Expand Down

0 comments on commit 18e72d5

Please sign in to comment.