Skip to content

DATACMNS-1734 - Defer initialization of Repositories in DomainClassConverter. #445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.4.0-SNAPSHOT</version>
<version>2.4.0-DATACMNS-1734-SNAPSHOT</version>

<name>Spring Data Core</name>

Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.util.Lazy;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -47,7 +48,7 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr
implements ConditionalGenericConverter, ApplicationContextAware {

private final T conversionService;
private Repositories repositories = Repositories.NONE;
private Lazy<Repositories> repositories = Lazy.of(Repositories.NONE);
private Optional<ToEntityConverter> toEntityConverter = Optional.empty();
private Optional<ToIdConverter> toIdConverter = Optional.empty();

@@ -97,7 +98,7 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
* @return
*/
private Optional<? extends ConditionalGenericConverter> getConverter(TypeDescriptor targetType) {
return repositories.hasRepositoryFor(targetType.getType()) ? toEntityConverter : toIdConverter;
return repositories.get().hasRepositoryFor(targetType.getType()) ? toEntityConverter : toIdConverter;
}

/*
@@ -106,13 +107,18 @@ private Optional<? extends ConditionalGenericConverter> getConverter(TypeDescrip
*/
public void setApplicationContext(ApplicationContext context) {

this.repositories = new Repositories(context);
this.repositories = Lazy.of(() -> {

this.toEntityConverter = Optional.of(new ToEntityConverter(this.repositories, this.conversionService));
this.toEntityConverter.ifPresent(it -> this.conversionService.addConverter(it));
Repositories repositories = new Repositories(context);

this.toIdConverter = Optional.of(new ToIdConverter());
this.toIdConverter.ifPresent(it -> this.conversionService.addConverter(it));
this.toEntityConverter = Optional.of(new ToEntityConverter(repositories, conversionService));
this.toEntityConverter.ifPresent(it -> conversionService.addConverter(it));

this.toIdConverter = Optional.of(new ToIdConverter(repositories, conversionService));
this.toIdConverter.ifPresent(it -> conversionService.addConverter(it));

return repositories;
});
}

/**
@@ -121,9 +127,11 @@ public void setApplicationContext(ApplicationContext context) {
* @author Oliver Gierke
* @since 1.10
*/
private class ToEntityConverter implements ConditionalGenericConverter {
private static class ToEntityConverter implements ConditionalGenericConverter {

private final RepositoryInvokerFactory repositoryInvokerFactory;
private final Repositories repositories;
private final ConversionService conversionService;

/**
* Creates a new {@link ToEntityConverter} for the given {@link Repositories} and {@link ConversionService}.
@@ -132,7 +140,10 @@ private class ToEntityConverter implements ConditionalGenericConverter {
* @param conversionService must not be {@literal null}.
*/
public ToEntityConverter(Repositories repositories, ConversionService conversionService) {

this.repositoryInvokerFactory = new DefaultRepositoryInvokerFactory(repositories, conversionService);
this.repositories = repositories;
this.conversionService = conversionService;
}

/*
@@ -206,7 +217,16 @@ public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
* @author Oliver Gierke
* @since 1.10
*/
class ToIdConverter implements ConditionalGenericConverter {
static class ToIdConverter implements ConditionalGenericConverter {

private final Repositories repositories;
private final ConversionService conversionService;

public ToIdConverter(Repositories repositories, ConversionService conversionService) {

this.repositories = repositories;
this.conversionService = conversionService;
}

/*
* (non-Javadoc)
Original file line number Diff line number Diff line change
@@ -29,7 +29,6 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import org.springframework.aop.framework.Advised;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@@ -181,6 +180,7 @@ void supportsConversionFromEntityToString() {
void toIdConverterDoesNotMatchIfTargetTypeIsAssignableFromSource() throws NoSuchMethodException {

converter.setApplicationContext(initContextWithRepo());
assertMatches(false);

@SuppressWarnings("rawtypes")
Optional<ToIdConverter> toIdConverter = (Optional<ToIdConverter>) ReflectionTestUtils.getField(converter,