20
20
import java .util .Collections ;
21
21
import java .util .Optional ;
22
22
23
+ import org .springframework .beans .BeansException ;
24
+ import org .springframework .context .ApplicationContext ;
25
+ import org .springframework .context .ApplicationContextAware ;
23
26
import org .springframework .context .annotation .Bean ;
24
27
import org .springframework .context .annotation .Configuration ;
25
28
import org .springframework .core .convert .converter .Converter ;
37
40
import org .springframework .data .relational .core .conversion .BasicRelationalConverter ;
38
41
import org .springframework .data .relational .core .mapping .NamingStrategy ;
39
42
import org .springframework .data .relational .core .mapping .RelationalMappingContext ;
43
+ import org .springframework .lang .Nullable ;
40
44
import org .springframework .util .Assert ;
41
45
42
46
/**
48
52
* @see DatabaseClient
49
53
* @see org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories
50
54
*/
51
- @ Configuration
52
- public abstract class AbstractR2dbcConfiguration {
55
+ @ Configuration (proxyBeanMethods = false )
56
+ public abstract class AbstractR2dbcConfiguration implements ApplicationContextAware {
57
+
58
+ private @ Nullable ApplicationContext context ;
59
+
60
+ /*
61
+ * (non-Javadoc)
62
+ * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
63
+ */
64
+ @ Override
65
+ public void setApplicationContext (ApplicationContext applicationContext ) throws BeansException {
66
+ this .context = applicationContext ;
67
+ }
53
68
54
69
/**
55
70
* Return a R2DBC {@link ConnectionFactory}. Annotate with {@link Bean} in case you want to expose a
@@ -91,7 +106,7 @@ public DatabaseClient databaseClient(ReactiveDataAccessStrategy dataAccessStrate
91
106
Assert .notNull (exceptionTranslator , "ExceptionTranslator must not be null!" );
92
107
93
108
return DatabaseClient .builder () //
94
- .connectionFactory (connectionFactory ()) //
109
+ .connectionFactory (lookupConnectionFactory ()) //
95
110
.dataAccessStrategy (dataAccessStrategy ) //
96
111
.exceptionTranslator (exceptionTranslator ) //
97
112
.build ();
@@ -137,7 +152,7 @@ public ReactiveDataAccessStrategy reactiveDataAccessStrategy(RelationalMappingCo
137
152
138
153
MappingR2dbcConverter converter = new MappingR2dbcConverter (mappingContext , r2dbcCustomConversions );
139
154
140
- return new DefaultReactiveDataAccessStrategy (getDialect (connectionFactory ()), converter );
155
+ return new DefaultReactiveDataAccessStrategy (getDialect (lookupConnectionFactory ()), converter );
141
156
}
142
157
143
158
/**
@@ -160,7 +175,7 @@ public R2dbcCustomConversions r2dbcCustomConversions() {
160
175
*/
161
176
protected StoreConversions getStoreConversions () {
162
177
163
- Dialect dialect = getDialect (connectionFactory ());
178
+ Dialect dialect = getDialect (lookupConnectionFactory ());
164
179
return StoreConversions .of (dialect .getSimpleTypeHolder (), R2dbcCustomConversions .STORE_CONVERTERS );
165
180
}
166
181
@@ -172,6 +187,18 @@ protected StoreConversions getStoreConversions() {
172
187
*/
173
188
@ Bean
174
189
public R2dbcExceptionTranslator exceptionTranslator () {
175
- return new SqlErrorCodeR2dbcExceptionTranslator (connectionFactory ());
190
+ return new SqlErrorCodeR2dbcExceptionTranslator (lookupConnectionFactory ());
191
+ }
192
+
193
+ private ConnectionFactory lookupConnectionFactory () {
194
+
195
+ ApplicationContext context = this .context ;
196
+ Assert .notNull (context , "ApplicationContext is not yet initialized" );
197
+
198
+ if (context .getBeanNamesForType (ConnectionFactory .class ).length != 0 ) {
199
+ return context .getBean (ConnectionFactory .class );
200
+ }
201
+
202
+ return connectionFactory ();
176
203
}
177
204
}
0 commit comments