DATAJDBC-330 - Polishing.
Replace lambdas with method references. Remove extraneous overrides. Inline methods. Formatting. Original pull request: #115.
This commit is contained in:
@@ -17,9 +17,7 @@ package org.springframework.data.jdbc.repository.config;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean;
|
||||
import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
|
||||
import org.springframework.data.repository.config.RepositoryConfigurationSource;
|
||||
@@ -31,14 +29,13 @@ import org.springframework.util.StringUtils;
|
||||
*
|
||||
* @author Jens Schauder
|
||||
* @author Fei Dong
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class JdbcRepositoryConfigExtension extends RepositoryConfigurationExtensionSupport {
|
||||
|
||||
private ListableBeanFactory beanFactory;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtension#getModuleName()
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtension#getModuleName()
|
||||
*/
|
||||
@Override
|
||||
public String getModuleName() {
|
||||
@@ -46,8 +43,8 @@ public class JdbcRepositoryConfigExtension extends RepositoryConfigurationExtens
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getRepositoryFactoryBeanClassName()
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getRepositoryFactoryBeanClassName()
|
||||
*/
|
||||
@Override
|
||||
public String getRepositoryFactoryBeanClassName() {
|
||||
@@ -55,25 +52,14 @@ public class JdbcRepositoryConfigExtension extends RepositoryConfigurationExtens
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getModulePrefix()
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getModulePrefix()
|
||||
*/
|
||||
@Override
|
||||
protected String getModulePrefix() {
|
||||
return getModuleName().toLowerCase(Locale.US);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtension#registerBeansForRoot(org.springframework.beans.factory.support.BeanDefinitionRegistry, org.springframework.data.repository.config.RepositoryConfigurationSource)
|
||||
*/
|
||||
public void registerBeansForRoot(BeanDefinitionRegistry registry, RepositoryConfigurationSource configurationSource) {
|
||||
|
||||
if (registry instanceof ListableBeanFactory) {
|
||||
this.beanFactory = (ListableBeanFactory) registry;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#postProcess(org.springframework.beans.factory.support.BeanDefinitionBuilder, org.springframework.data.repository.config.RepositoryConfigurationSource)
|
||||
@@ -82,12 +68,11 @@ public class JdbcRepositoryConfigExtension extends RepositoryConfigurationExtens
|
||||
public void postProcess(BeanDefinitionBuilder builder, RepositoryConfigurationSource source) {
|
||||
|
||||
source.getAttribute("jdbcOperationsRef") //
|
||||
.filter(s -> !StringUtils.isEmpty(s)) //
|
||||
.filter(StringUtils::hasText) //
|
||||
.ifPresent(s -> builder.addPropertyReference("jdbcOperations", s));
|
||||
|
||||
source.getAttribute("dataAccessStrategyRef") //
|
||||
.filter(s -> !StringUtils.isEmpty(s)) //
|
||||
.filter(StringUtils::hasText) //
|
||||
.ifPresent(s -> builder.addPropertyReference("dataAccessStrategy", s));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -150,36 +150,24 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
|
||||
Assert.state(this.mappingContext != null, "MappingContext is required and must not be null!");
|
||||
Assert.state(this.converter != null, "RelationalConverter is required and must not be null!");
|
||||
|
||||
ensureJdbcOperationsIsInitialized();
|
||||
ensureDataAccessStrategyIsInitialized();
|
||||
if (this.operations == null) {
|
||||
this.operations = beanFactory.getBean(NamedParameterJdbcOperations.class);
|
||||
}
|
||||
|
||||
if (queryMappingConfiguration == null) {
|
||||
if (this.dataAccessStrategy == null) {
|
||||
this.dataAccessStrategy = this.beanFactory.getBeanProvider(DataAccessStrategy.class) //
|
||||
.getIfAvailable(() -> {
|
||||
|
||||
SqlGeneratorSource sqlGeneratorSource = new SqlGeneratorSource(this.mappingContext);
|
||||
return new DefaultDataAccessStrategy(sqlGeneratorSource, this.mappingContext, this.converter,
|
||||
this.operations);
|
||||
});
|
||||
}
|
||||
|
||||
if (this.queryMappingConfiguration == null) {
|
||||
this.queryMappingConfiguration = QueryMappingConfiguration.EMPTY;
|
||||
}
|
||||
|
||||
super.afterPropertiesSet();
|
||||
}
|
||||
|
||||
private void ensureJdbcOperationsIsInitialized() {
|
||||
|
||||
if (operations != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
operations = beanFactory.getBean(NamedParameterJdbcOperations.class);
|
||||
}
|
||||
|
||||
private void ensureDataAccessStrategyIsInitialized() {
|
||||
|
||||
if (dataAccessStrategy != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
dataAccessStrategy = beanFactory.getBeanProvider(DataAccessStrategy.class).getIfAvailable(() -> {
|
||||
|
||||
SqlGeneratorSource sqlGeneratorSource = new SqlGeneratorSource(mappingContext);
|
||||
return new DefaultDataAccessStrategy(sqlGeneratorSource, mappingContext, converter, operations);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,12 +18,12 @@ package org.springframework.data.jdbc.repository.support;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
@@ -41,8 +41,6 @@ import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Tests the dependency injection for {@link JdbcRepositoryFactoryBean}.
|
||||
*
|
||||
@@ -72,13 +70,11 @@ public class JdbcRepositoryFactoryBeanUnitTests {
|
||||
// Setup standard configuration
|
||||
factoryBean = new JdbcRepositoryFactoryBean<>(DummyEntityRepository.class);
|
||||
|
||||
|
||||
when(beanFactory.getBean(NamedParameterJdbcOperations.class)).thenReturn(mock(NamedParameterJdbcOperations.class));
|
||||
|
||||
ObjectProvider<DataAccessStrategy> provider = mock(ObjectProvider.class);
|
||||
when(beanFactory.getBeanProvider(DataAccessStrategy.class)).thenReturn(provider);
|
||||
when(provider.getIfAvailable(any()))
|
||||
.then((Answer) invocation -> ((Supplier)invocation.getArgument(0)).get());
|
||||
when(provider.getIfAvailable(any())).then((Answer) invocation -> ((Supplier) invocation.getArgument(0)).get());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -126,6 +122,7 @@ public class JdbcRepositoryFactoryBeanUnitTests {
|
||||
}
|
||||
|
||||
private static class DummyEntity {
|
||||
|
||||
@Id private Long id;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user