3030import org .springframework .data .repository .CrudRepository ;
3131import org .springframework .data .repository .core .EntityInformation ;
3232import org .springframework .data .repository .core .RepositoryInformation ;
33+ import org .springframework .data .util .Lazy ;
3334import org .springframework .lang .Nullable ;
3435import org .springframework .util .Assert ;
3536import org .springframework .util .StringUtils ;
@@ -47,7 +48,7 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr
4748 implements ConditionalGenericConverter , ApplicationContextAware {
4849
4950 private final T conversionService ;
50- private Repositories repositories = Repositories .NONE ;
51+ private Lazy < Repositories > repositories = Lazy . of ( Repositories .NONE ) ;
5152 private Optional <ToEntityConverter > toEntityConverter = Optional .empty ();
5253 private Optional <ToIdConverter > toIdConverter = Optional .empty ();
5354
@@ -97,7 +98,7 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
9798 * @return
9899 */
99100 private Optional <? extends ConditionalGenericConverter > getConverter (TypeDescriptor targetType ) {
100- return repositories .hasRepositoryFor (targetType .getType ()) ? toEntityConverter : toIdConverter ;
101+ return repositories .get (). hasRepositoryFor (targetType .getType ()) ? toEntityConverter : toIdConverter ;
101102 }
102103
103104 /*
@@ -106,13 +107,18 @@ private Optional<? extends ConditionalGenericConverter> getConverter(TypeDescrip
106107 */
107108 public void setApplicationContext (ApplicationContext context ) {
108109
109- this .repositories = new Repositories ( context );
110+ this .repositories = Lazy . of (() -> {
110111
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 );
113113
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+ });
116122 }
117123
118124 /**
@@ -121,9 +127,11 @@ public void setApplicationContext(ApplicationContext context) {
121127 * @author Oliver Gierke
122128 * @since 1.10
123129 */
124- private class ToEntityConverter implements ConditionalGenericConverter {
130+ private static class ToEntityConverter implements ConditionalGenericConverter {
125131
126132 private final RepositoryInvokerFactory repositoryInvokerFactory ;
133+ private final Repositories repositories ;
134+ private final ConversionService conversionService ;
127135
128136 /**
129137 * Creates a new {@link ToEntityConverter} for the given {@link Repositories} and {@link ConversionService}.
@@ -132,7 +140,10 @@ private class ToEntityConverter implements ConditionalGenericConverter {
132140 * @param conversionService must not be {@literal null}.
133141 */
134142 public ToEntityConverter (Repositories repositories , ConversionService conversionService ) {
143+
135144 this .repositoryInvokerFactory = new DefaultRepositoryInvokerFactory (repositories , conversionService );
145+ this .repositories = repositories ;
146+ this .conversionService = conversionService ;
136147 }
137148
138149 /*
@@ -206,7 +217,16 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
206217 * @author Oliver Gierke
207218 * @since 1.10
208219 */
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+ }
210230
211231 /*
212232 * (non-Javadoc)
0 commit comments