19
19
import java .util .Map ;
20
20
import java .util .function .Supplier ;
21
21
22
+ import javax .persistence .EntityManager ;
22
23
import javax .persistence .EntityManagerFactory ;
24
+ import javax .persistence .spi .PersistenceProvider ;
25
+ import javax .persistence .spi .PersistenceUnitInfo ;
23
26
import javax .sql .DataSource ;
24
27
25
28
import org .springframework .beans .BeansException ;
32
35
import org .springframework .boot .jdbc .EmbeddedDatabaseConnection ;
33
36
import org .springframework .context .ApplicationContext ;
34
37
import org .springframework .context .annotation .ImportBeanDefinitionRegistrar ;
38
+ import org .springframework .core .task .AsyncTaskExecutor ;
35
39
import org .springframework .core .type .AnnotationMetadata ;
40
+ import org .springframework .orm .jpa .JpaDialect ;
41
+ import org .springframework .orm .jpa .JpaVendorAdapter ;
42
+ import org .springframework .orm .jpa .LocalContainerEntityManagerFactoryBean ;
36
43
37
44
/**
38
45
* {@link BeanPostProcessor} used to fire {@link DataSourceSchemaCreatedEvent}s. Should
@@ -55,6 +62,10 @@ class DataSourceInitializedPublisher implements BeanPostProcessor {
55
62
@ Override
56
63
public Object postProcessBeforeInitialization (Object bean , String beanName )
57
64
throws BeansException {
65
+ if (bean instanceof LocalContainerEntityManagerFactoryBean ) {
66
+ LocalContainerEntityManagerFactoryBean factory = (LocalContainerEntityManagerFactoryBean ) bean ;
67
+ factory .setJpaVendorAdapter (new DataSourceSchemeCreatedPublisher (factory ));
68
+ }
58
69
return bean ;
59
70
}
60
71
@@ -71,9 +82,6 @@ public Object postProcessAfterInitialization(Object bean, String beanName)
71
82
if (bean instanceof HibernateProperties ) {
72
83
this .hibernateProperties = (HibernateProperties ) bean ;
73
84
}
74
- if (bean instanceof EntityManagerFactory ) {
75
- publishEventIfRequired ((EntityManagerFactory ) bean );
76
- }
77
85
return bean ;
78
86
}
79
87
@@ -88,8 +96,8 @@ private void publishEventIfRequired(EntityManagerFactory entityManagerFactory) {
88
96
private DataSource findDataSource (EntityManagerFactory entityManagerFactory ) {
89
97
Object dataSource = entityManagerFactory .getProperties ()
90
98
.get ("javax.persistence.nonJtaDataSource" );
91
- return (dataSource != null && dataSource instanceof DataSource )
92
- ? ( DataSource ) dataSource : this .dataSource ;
99
+ return (dataSource instanceof DataSource ) ? ( DataSource ) dataSource
100
+ : this .dataSource ;
93
101
}
94
102
95
103
private boolean isInitializingDatabase (DataSource dataSource ) {
@@ -132,4 +140,66 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
132
140
133
141
}
134
142
143
+ final class DataSourceSchemeCreatedPublisher implements JpaVendorAdapter {
144
+
145
+ private final JpaVendorAdapter delegate ;
146
+
147
+ private final LocalContainerEntityManagerFactoryBean factory ;
148
+
149
+ private DataSourceSchemeCreatedPublisher (
150
+ LocalContainerEntityManagerFactoryBean factory ) {
151
+ this .delegate = factory .getJpaVendorAdapter ();
152
+ this .factory = factory ;
153
+ }
154
+
155
+ @ Override
156
+ public PersistenceProvider getPersistenceProvider () {
157
+ return this .delegate .getPersistenceProvider ();
158
+ }
159
+
160
+ @ Override
161
+ public String getPersistenceProviderRootPackage () {
162
+ return this .delegate .getPersistenceProviderRootPackage ();
163
+ }
164
+
165
+ @ Override
166
+ public Map <String , ?> getJpaPropertyMap (PersistenceUnitInfo pui ) {
167
+ return this .delegate .getJpaPropertyMap (pui );
168
+ }
169
+
170
+ @ Override
171
+ public Map <String , ?> getJpaPropertyMap () {
172
+ return this .delegate .getJpaPropertyMap ();
173
+ }
174
+
175
+ @ Override
176
+ public JpaDialect getJpaDialect () {
177
+ return this .delegate .getJpaDialect ();
178
+ }
179
+
180
+ @ Override
181
+ public Class <? extends EntityManagerFactory > getEntityManagerFactoryInterface () {
182
+ return this .delegate .getEntityManagerFactoryInterface ();
183
+ }
184
+
185
+ @ Override
186
+ public Class <? extends EntityManager > getEntityManagerInterface () {
187
+ return this .delegate .getEntityManagerInterface ();
188
+ }
189
+
190
+ @ Override
191
+ public void postProcessEntityManagerFactory (EntityManagerFactory emf ) {
192
+ this .delegate .postProcessEntityManagerFactory (emf );
193
+ AsyncTaskExecutor bootstrapExecutor = this .factory .getBootstrapExecutor ();
194
+ if (bootstrapExecutor != null ) {
195
+ bootstrapExecutor .execute (() -> DataSourceInitializedPublisher .this
196
+ .publishEventIfRequired (emf ));
197
+ }
198
+ else {
199
+ DataSourceInitializedPublisher .this .publishEventIfRequired (emf );
200
+ }
201
+ }
202
+
203
+ }
204
+
135
205
}
0 commit comments