19
19
import java .util .Map ;
20
20
import java .util .function .Consumer ;
21
21
22
+ import org .apache .commons .logging .Log ;
23
+ import org .apache .commons .logging .LogFactory ;
22
24
import org .hibernate .resource .beans .container .spi .BeanContainer ;
23
25
import org .hibernate .resource .beans .container .spi .ContainedBean ;
24
26
import org .hibernate .resource .beans .spi .BeanInstanceProducer ;
25
27
28
+ import org .springframework .beans .factory .BeanCreationException ;
26
29
import org .springframework .beans .factory .config .AutowireCapableBeanFactory ;
27
30
import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
28
31
import org .springframework .lang .Nullable ;
72
75
*/
73
76
public final class SpringBeanContainer implements BeanContainer {
74
77
78
+ private static final Log logger = LogFactory .getLog (SpringBeanContainer .class );
79
+
75
80
private final ConfigurableListableBeanFactory beanFactory ;
76
81
77
82
private final Map <Object , SpringContainedBean <?>> beanCache = new ConcurrentReferenceHashMap <>();
@@ -96,12 +101,12 @@ public <B> ContainedBean<B> getBean(
96
101
if (lifecycleOptions .canUseCachedReferences ()) {
97
102
bean = this .beanCache .get (beanType );
98
103
if (bean == null ) {
99
- bean = createBean (beanType , lifecycleOptions );
104
+ bean = createBean (beanType , lifecycleOptions , fallbackProducer );
100
105
this .beanCache .put (beanType , bean );
101
106
}
102
107
}
103
108
else {
104
- bean = createBean (beanType , lifecycleOptions );
109
+ bean = createBean (beanType , lifecycleOptions , fallbackProducer );
105
110
}
106
111
return (SpringContainedBean <B >) bean ;
107
112
}
@@ -119,12 +124,12 @@ public <B> ContainedBean<B> getBean(
119
124
if (lifecycleOptions .canUseCachedReferences ()) {
120
125
bean = this .beanCache .get (name );
121
126
if (bean == null ) {
122
- bean = createBean (name , beanType , lifecycleOptions );
127
+ bean = createBean (name , beanType , lifecycleOptions , fallbackProducer );
123
128
this .beanCache .put (name , bean );
124
129
}
125
130
}
126
131
else {
127
- bean = createBean (name , beanType , lifecycleOptions );
132
+ bean = createBean (name , beanType , lifecycleOptions , fallbackProducer );
128
133
}
129
134
return (SpringContainedBean <B >) bean ;
130
135
}
@@ -136,26 +141,48 @@ public void stop() {
136
141
}
137
142
138
143
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
+ }
144
156
}
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 ));
147
163
}
148
164
}
149
165
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
+ }
156
179
}
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 ));
159
186
}
160
187
}
161
188
0 commit comments