From dff7aa4d4b6aee490c559e153a8024bc8c05ec97 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 17 Jun 2023 11:38:57 +0200 Subject: [PATCH] Fall back to type-based creation if no bean of the given name exists Closes gh-30683 --- .../orm/hibernate5/SpringBeanContainer.java | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate5/SpringBeanContainer.java b/spring-orm/src/main/java/org/springframework/orm/hibernate5/SpringBeanContainer.java index e33013c4488..6e73747a592 100644 --- a/spring-orm/src/main/java/org/springframework/orm/hibernate5/SpringBeanContainer.java +++ b/spring-orm/src/main/java/org/springframework/orm/hibernate5/SpringBeanContainer.java @@ -143,7 +143,7 @@ public final class SpringBeanContainer implements BeanContainer { try { if (lifecycleOptions.useJpaCompliantCreation()) { - return new SpringContainedBean<>( + return new SpringContainedBean<>( // to be replaced with plain createBean(Class) this.beanFactory.createBean(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false), this.beanFactory::destroyBean); } @@ -180,14 +180,23 @@ public final class SpringBeanContainer implements BeanContainer { try { if (lifecycleOptions.useJpaCompliantCreation()) { - Object 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)); + if (this.beanFactory.containsBean(name)) { + Object 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 { + return new SpringContainedBean<>( // to be replaced with plain createBean(Class) + this.beanFactory.createBean(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false), + this.beanFactory::destroyBean); + } } else { - return new SpringContainedBean<>(this.beanFactory.getBean(name, beanType)); + return (this.beanFactory.containsBean(name) ? + new SpringContainedBean<>(this.beanFactory.getBean(name, beanType)) : + new SpringContainedBean<>(this.beanFactory.getBean(beanType))); } } catch (BeansException ex) {