DATACMNS-1538 - Repository proxy post processors in CDI.
Add support for custom repository proxy post processors in CDI. Original pull request: #396.
This commit is contained in:
committed by
Jens Schauder
parent
0f1d4970ef
commit
098842765d
@@ -58,6 +58,7 @@ import org.springframework.util.StringUtils;
|
||||
* @author Peter Rietzler
|
||||
* @author Jens Schauder
|
||||
* @author Christoph Strobl
|
||||
* @author Ariel Carrera
|
||||
*/
|
||||
public abstract class CdiRepositoryBean<T> implements Bean<T>, PassivationCapable {
|
||||
|
||||
@@ -484,6 +485,8 @@ public abstract class CdiRepositoryBean<T> implements Bean<T>, PassivationCapabl
|
||||
configuration.getNamedQueries().ifPresent(repositoryFactory::setNamedQueries);
|
||||
configuration.getQueryLookupStrategy().ifPresent(repositoryFactory::setQueryLookupStrategyKey);
|
||||
configuration.getRepositoryBeanClass().ifPresent(repositoryFactory::setRepositoryBaseClass);
|
||||
configuration.getRepositoryProxyPostProcessors().forEach(repositoryFactory::addRepositoryProxyPostProcessor);
|
||||
configuration.getQueryCreationListeners().forEach(repositoryFactory::addQueryCreationListener);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,9 +16,13 @@
|
||||
|
||||
package org.springframework.data.repository.cdi;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.repository.core.NamedQueries;
|
||||
import org.springframework.data.repository.core.support.QueryCreationListener;
|
||||
import org.springframework.data.repository.core.support.RepositoryProxyPostProcessor;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy;
|
||||
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
|
||||
|
||||
@@ -27,6 +31,7 @@ import org.springframework.data.repository.query.QueryMethodEvaluationContextPro
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Fabian Henniges
|
||||
* @author Ariel Carrera
|
||||
*/
|
||||
public interface CdiRepositoryConfiguration {
|
||||
|
||||
@@ -79,4 +84,28 @@ public interface CdiRepositoryConfiguration {
|
||||
default String getRepositoryImplementationPostfix() {
|
||||
return "Impl";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of {@link RepositoryProxyPostProcessor} to be used during repository proxy creation. Can be
|
||||
* {@link Collections#emptyList()} .
|
||||
*
|
||||
* @return the list of repository proxy post processors to use, can be {@link Collections#emptyList()}, must not be
|
||||
* {@literal null}.
|
||||
* @since 2.2
|
||||
*/
|
||||
default List<RepositoryProxyPostProcessor> getRepositoryProxyPostProcessors() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of {@link QueryCreationListener} to be used during repository proxy creation. Can be
|
||||
* {@link Collections#emptyList()} .
|
||||
*
|
||||
* @return the list query creation listeners to use, can be {@link Collections#emptyList()}, must not be
|
||||
* {@literal null}.
|
||||
* @since 2.2
|
||||
*/
|
||||
default List<QueryCreationListener<?>> getQueryCreationListeners() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,13 +15,16 @@
|
||||
*/
|
||||
package org.springframework.data.repository.cdi;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -36,20 +39,26 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.config.CustomRepositoryImplementationDetector;
|
||||
import org.springframework.data.repository.config.ImplementationLookupConfiguration;
|
||||
import org.springframework.data.repository.core.NamedQueries;
|
||||
import org.springframework.data.repository.core.RepositoryInformation;
|
||||
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
|
||||
import org.springframework.data.repository.core.support.QueryCreationListener;
|
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
|
||||
import org.springframework.data.repository.core.support.RepositoryProxyPostProcessor;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
||||
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
|
||||
import org.springframework.data.repository.query.RepositoryQuery;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link CdiRepositoryBean}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
* @author Ariel Carrera
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CdiRepositoryBeanUnitTests {
|
||||
@@ -180,6 +189,8 @@ public class CdiRepositoryBeanUnitTests {
|
||||
verify(repositoryFactory).setNamedQueries(PropertiesBasedNamedQueries.EMPTY);
|
||||
verify(repositoryFactory).setRepositoryBaseClass(String.class);
|
||||
verify(repositoryFactory).setQueryLookupStrategyKey(Key.CREATE);
|
||||
verify(repositoryFactory).addRepositoryProxyPostProcessor(DummyRepositoryProxyPostProcessor.INSTANCE);
|
||||
verify(repositoryFactory).addQueryCreationListener(DummyQueryCreationListener.INSTANCE);
|
||||
}
|
||||
|
||||
static class DummyCdiRepositoryBean<T> extends CdiRepositoryBean<T> {
|
||||
@@ -228,5 +239,32 @@ public class CdiRepositoryBeanUnitTests {
|
||||
public Optional<Class<?>> getRepositoryBeanClass() {
|
||||
return Optional.of(String.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<RepositoryProxyPostProcessor> getRepositoryProxyPostProcessors() {
|
||||
return Arrays.asList(DummyRepositoryProxyPostProcessor.INSTANCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<QueryCreationListener<?>> getQueryCreationListeners() {
|
||||
return Arrays.asList(DummyQueryCreationListener.INSTANCE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class DummyRepositoryProxyPostProcessor implements RepositoryProxyPostProcessor {
|
||||
|
||||
static final DummyRepositoryProxyPostProcessor INSTANCE = new DummyRepositoryProxyPostProcessor();
|
||||
|
||||
@Override
|
||||
public void postProcess(ProxyFactory factory, RepositoryInformation repositoryInformation) {}
|
||||
}
|
||||
|
||||
static class DummyQueryCreationListener implements QueryCreationListener<RepositoryQuery> {
|
||||
|
||||
public static final DummyQueryCreationListener INSTANCE = new DummyQueryCreationListener();
|
||||
|
||||
@Override
|
||||
public void onCreation(RepositoryQuery query) {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,10 +15,16 @@
|
||||
*/
|
||||
package org.springframework.data.repository.core.support;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assume.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assume.assumeThat;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
@@ -37,6 +43,7 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.core.SpringVersion;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
@@ -70,6 +77,7 @@ import org.springframework.util.concurrent.ListenableFuture;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
* @author Ariel Carrera
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class RepositoryFactorySupportUnitTests {
|
||||
@@ -87,6 +95,8 @@ public class RepositoryFactorySupportUnitTests {
|
||||
@Mock MyQueryCreationListener listener;
|
||||
@Mock PlainQueryCreationListener otherListener;
|
||||
|
||||
@Mock RepositoryProxyPostProcessor repositoryPostProcessor;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
factory = new DummyRepositoryFactory(backingRepo);
|
||||
@@ -110,6 +120,16 @@ public class RepositoryFactorySupportUnitTests {
|
||||
verify(otherListener, times(2)).onCreation(Mockito.any(RepositoryQuery.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokesCustomRepositoryProxyPostProcessor() throws Exception {
|
||||
|
||||
factory.addRepositoryProxyPostProcessor(repositoryPostProcessor);
|
||||
factory.getRepository(ObjectRepository.class);
|
||||
|
||||
verify(repositoryPostProcessor, times(1)).postProcess(Mockito.any(ProxyFactory.class),
|
||||
Mockito.any(RepositoryInformation.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void routesCallToRedeclaredMethodIntoTarget() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user