Skip to content

Commit d2eb4d2

Browse files
committed
SpringBeanContainer falls back to Hibernate's default producer
Issue: SPR-17010
1 parent 620e83c commit d2eb4d2

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

spring-orm/src/main/java/org/springframework/orm/hibernate5/SpringBeanContainer.java

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
import java.util.Map;
2020
import java.util.function.Consumer;
2121

22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
2224
import org.hibernate.resource.beans.container.spi.BeanContainer;
2325
import org.hibernate.resource.beans.container.spi.ContainedBean;
2426
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
2527

28+
import org.springframework.beans.factory.BeanCreationException;
2629
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
2730
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2831
import org.springframework.lang.Nullable;
@@ -72,6 +75,8 @@
7275
*/
7376
public final class SpringBeanContainer implements BeanContainer {
7477

78+
private static final Log logger = LogFactory.getLog(SpringBeanContainer.class);
79+
7580
private final ConfigurableListableBeanFactory beanFactory;
7681

7782
private final Map<Object, SpringContainedBean<?>> beanCache = new ConcurrentReferenceHashMap<>();
@@ -96,12 +101,12 @@ public <B> ContainedBean<B> getBean(
96101
if (lifecycleOptions.canUseCachedReferences()) {
97102
bean = this.beanCache.get(beanType);
98103
if (bean == null) {
99-
bean = createBean(beanType, lifecycleOptions);
104+
bean = createBean(beanType, lifecycleOptions, fallbackProducer);
100105
this.beanCache.put(beanType, bean);
101106
}
102107
}
103108
else {
104-
bean = createBean(beanType, lifecycleOptions);
109+
bean = createBean(beanType, lifecycleOptions, fallbackProducer);
105110
}
106111
return (SpringContainedBean<B>) bean;
107112
}
@@ -119,12 +124,12 @@ public <B> ContainedBean<B> getBean(
119124
if (lifecycleOptions.canUseCachedReferences()) {
120125
bean = this.beanCache.get(name);
121126
if (bean == null) {
122-
bean = createBean(name, beanType, lifecycleOptions);
127+
bean = createBean(name, beanType, lifecycleOptions, fallbackProducer);
123128
this.beanCache.put(name, bean);
124129
}
125130
}
126131
else {
127-
bean = createBean(name, beanType, lifecycleOptions);
132+
bean = createBean(name, beanType, lifecycleOptions, fallbackProducer);
128133
}
129134
return (SpringContainedBean<B>) bean;
130135
}
@@ -136,26 +141,48 @@ public void stop() {
136141
}
137142

138143

139-
private SpringContainedBean<?> createBean(Class<?> beanType, LifecycleOptions lifecycleOptions) {
140-
if (lifecycleOptions.useJpaCompliantCreation()) {
141-
return new SpringContainedBean<>(
142-
this.beanFactory.createBean(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false),
143-
this.beanFactory::destroyBean);
144+
private SpringContainedBean<?> createBean(
145+
Class<?> beanType, LifecycleOptions lifecycleOptions, BeanInstanceProducer fallbackProducer) {
146+
147+
try {
148+
if (lifecycleOptions.useJpaCompliantCreation()) {
149+
return new SpringContainedBean<>(
150+
this.beanFactory.createBean(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false),
151+
this.beanFactory::destroyBean);
152+
}
153+
else {
154+
return new SpringContainedBean<>(this.beanFactory.getBean(beanType));
155+
}
144156
}
145-
else {
146-
return new SpringContainedBean<>(this.beanFactory.getBean(beanType));
157+
catch (BeanCreationException ex) {
158+
if (logger.isDebugEnabled()) {
159+
logger.debug("Falling back to Hibernate's default producer after bean creation failure for " +
160+
beanType + ": " + ex);
161+
}
162+
return new SpringContainedBean<>(fallbackProducer.produceBeanInstance(beanType));
147163
}
148164
}
149165

150-
private SpringContainedBean<?> createBean(String name, Class<?> beanType, LifecycleOptions lifecycleOptions) {
151-
if (lifecycleOptions.useJpaCompliantCreation()) {
152-
Object bean = this.beanFactory.autowire(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
153-
this.beanFactory.applyBeanPropertyValues(bean, name);
154-
this.beanFactory.initializeBean(bean, name);
155-
return new SpringContainedBean<>(bean, beanInstance -> this.beanFactory.destroyBean(name, beanInstance));
166+
private SpringContainedBean<?> createBean(
167+
String name, Class<?> beanType, LifecycleOptions lifecycleOptions, BeanInstanceProducer fallbackProducer) {
168+
169+
try {
170+
if (lifecycleOptions.useJpaCompliantCreation()) {
171+
Object bean = this.beanFactory.autowire(beanType, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
172+
this.beanFactory.applyBeanPropertyValues(bean, name);
173+
this.beanFactory.initializeBean(bean, name);
174+
return new SpringContainedBean<>(bean, beanInstance -> this.beanFactory.destroyBean(name, beanInstance));
175+
}
176+
else {
177+
return new SpringContainedBean<>(this.beanFactory.getBean(name, beanType));
178+
}
156179
}
157-
else {
158-
return new SpringContainedBean<>(this.beanFactory.getBean(name, beanType));
180+
catch (BeanCreationException ex) {
181+
if (logger.isDebugEnabled()) {
182+
logger.debug("Falling back to Hibernate's default producer after bean creation failure for " +
183+
beanType + ": " + ex);
184+
}
185+
return new SpringContainedBean<>(fallbackProducer.produceBeanInstance(name, beanType));
159186
}
160187
}
161188

0 commit comments

Comments
 (0)