Skip to content

Commit

Permalink
Fall back to type-based creation if no bean of the given name exists
Browse files Browse the repository at this point in the history
Closes gh-30683

(cherry picked from commit dff7aa4)
  • Loading branch information
jhoeller committed Jun 17, 2023
1 parent 40a9ae9 commit 1071778
Showing 1 changed file with 16 additions and 7 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -180,14 +180,23 @@ private SpringContainedBean<?> createBean(

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<>(
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) {
Expand Down

0 comments on commit 1071778

Please sign in to comment.