30
30
import org .springframework .data .repository .CrudRepository ;
31
31
import org .springframework .data .repository .core .EntityInformation ;
32
32
import org .springframework .data .repository .core .RepositoryInformation ;
33
+ import org .springframework .data .util .Lazy ;
33
34
import org .springframework .lang .Nullable ;
34
35
import org .springframework .util .Assert ;
35
36
import org .springframework .util .StringUtils ;
@@ -47,7 +48,7 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr
47
48
implements ConditionalGenericConverter , ApplicationContextAware {
48
49
49
50
private final T conversionService ;
50
- private Repositories repositories = Repositories .NONE ;
51
+ private Lazy < Repositories > repositories = Lazy . of ( Repositories .NONE ) ;
51
52
private Optional <ToEntityConverter > toEntityConverter = Optional .empty ();
52
53
private Optional <ToIdConverter > toIdConverter = Optional .empty ();
53
54
@@ -97,7 +98,7 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
97
98
* @return
98
99
*/
99
100
private Optional <? extends ConditionalGenericConverter > getConverter (TypeDescriptor targetType ) {
100
- return repositories .hasRepositoryFor (targetType .getType ()) ? toEntityConverter : toIdConverter ;
101
+ return repositories .get (). hasRepositoryFor (targetType .getType ()) ? toEntityConverter : toIdConverter ;
101
102
}
102
103
103
104
/*
@@ -106,13 +107,18 @@ private Optional<? extends ConditionalGenericConverter> getConverter(TypeDescrip
106
107
*/
107
108
public void setApplicationContext (ApplicationContext context ) {
108
109
109
- this .repositories = new Repositories ( context );
110
+ this .repositories = Lazy . of (() -> {
110
111
111
- this .toEntityConverter = Optional .of (new ToEntityConverter (this .repositories , this .conversionService ));
112
- this .toEntityConverter .ifPresent (it -> this .conversionService .addConverter (it ));
112
+ Repositories repositories = new Repositories (context );
113
113
114
- this .toIdConverter = Optional .of (new ToIdConverter ());
115
- this .toIdConverter .ifPresent (it -> this .conversionService .addConverter (it ));
114
+ this .toEntityConverter = Optional .of (new ToEntityConverter (repositories , conversionService ));
115
+ this .toEntityConverter .ifPresent (it -> conversionService .addConverter (it ));
116
+
117
+ this .toIdConverter = Optional .of (new ToIdConverter (repositories , conversionService ));
118
+ this .toIdConverter .ifPresent (it -> conversionService .addConverter (it ));
119
+
120
+ return repositories ;
121
+ });
116
122
}
117
123
118
124
/**
@@ -121,9 +127,11 @@ public void setApplicationContext(ApplicationContext context) {
121
127
* @author Oliver Gierke
122
128
* @since 1.10
123
129
*/
124
- private class ToEntityConverter implements ConditionalGenericConverter {
130
+ private static class ToEntityConverter implements ConditionalGenericConverter {
125
131
126
132
private final RepositoryInvokerFactory repositoryInvokerFactory ;
133
+ private final Repositories repositories ;
134
+ private final ConversionService conversionService ;
127
135
128
136
/**
129
137
* Creates a new {@link ToEntityConverter} for the given {@link Repositories} and {@link ConversionService}.
@@ -132,7 +140,10 @@ private class ToEntityConverter implements ConditionalGenericConverter {
132
140
* @param conversionService must not be {@literal null}.
133
141
*/
134
142
public ToEntityConverter (Repositories repositories , ConversionService conversionService ) {
143
+
135
144
this .repositoryInvokerFactory = new DefaultRepositoryInvokerFactory (repositories , conversionService );
145
+ this .repositories = repositories ;
146
+ this .conversionService = conversionService ;
136
147
}
137
148
138
149
/*
@@ -206,7 +217,16 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
206
217
* @author Oliver Gierke
207
218
* @since 1.10
208
219
*/
209
- class ToIdConverter implements ConditionalGenericConverter {
220
+ static class ToIdConverter implements ConditionalGenericConverter {
221
+
222
+ private final Repositories repositories ;
223
+ private final ConversionService conversionService ;
224
+
225
+ public ToIdConverter (Repositories repositories , ConversionService conversionService ) {
226
+
227
+ this .repositories = repositories ;
228
+ this .conversionService = conversionService ;
229
+ }
210
230
211
231
/*
212
232
* (non-Javadoc)
0 commit comments