DATACMNS-1764 - Allow registration of RepositoryFactory customizers.

Original Pull Request: #455
This commit is contained in:
Mark Paluch
2020-07-08 15:55:40 +02:00
committed by Christoph Strobl
parent e77947f0a5
commit 35a3d3cde9
3 changed files with 82 additions and 1 deletions

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.repository.core.support;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@@ -71,6 +72,7 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>,
private BeanFactory beanFactory;
private boolean lazyInit = false;
private Optional<QueryMethodEvaluationContextProvider> evaluationContextProvider = Optional.empty();
private List<RepositoryFactoryCustomizer> repositoryFactoryCustomizers = new ArrayList<>();
private ApplicationEventPublisher publisher;
private Lazy<T> repository;
@@ -154,6 +156,19 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>,
this.evaluationContextProvider = Optional.of(evaluationContextProvider);
}
/**
* Register a {@link RepositoryFactoryCustomizer} to customize the {@link RepositoryFactorySupport repository factor}
* before creating the repository.
*
* @param customizer must not be {@literal null}.
* @since 2.4
*/
public void addRepositoryFactoryCustomizer(RepositoryFactoryCustomizer customizer) {
Assert.notNull(customizer, "RepositoryFactoryCustomizer must not be null");
this.repositoryFactoryCustomizers.add(customizer);
}
/**
* Configures whether to initialize the repository proxy lazily. This defaults to {@literal false}.
*
@@ -293,6 +308,8 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>,
repositoryBaseClass.ifPresent(this.factory::setRepositoryBaseClass);
this.repositoryFactoryCustomizers.forEach(customizer -> customizer.customize(this.factory));
RepositoryFragments customImplementationFragment = customImplementation //
.map(RepositoryFragments::just) //
.orElseGet(RepositoryFragments::empty);

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.repository.core.support;
/**
* Callback interface that can be used to to customize a {@link RepositoryFactorySupport repository factory}.
*
* @author Mark Paluch
* @since 2.4
*/
public interface RepositoryFactoryCustomizer {
/**
* Callback to customize a {@link RepositoryFactorySupport} instance.
*
* @param repositoryFactory repository factory to customize.
*/
void customize(RepositoryFactorySupport repositoryFactory);
}

View File

@@ -18,6 +18,11 @@ package org.springframework.data.repository.core.support;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.jupiter.api.Test;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
@@ -69,6 +74,29 @@ class RepositoryFactoryBeanSupportUnitTests {
assertThat(information.getQueryMethods()).isEmpty();
}
@Test // DATACMNS-1688
void customizesRepositoryFactory() {
RepositoryFactoryBeanSupport<SampleRepository, Object, Long> factoryBean = //
new DummyRepositoryFactoryBean<>(SampleRepository.class);
factoryBean.addRepositoryFactoryCustomizer(repositoryFactory -> repositoryFactory
.addRepositoryProxyPostProcessor((factory, repositoryInformation) -> factory.addAdvice(new MethodInterceptor() {
@Nullable
@Override
public Object invoke(@Nonnull MethodInvocation invocation) {
throw new UnsupportedOperationException();
}
})));
factoryBean.afterPropertiesSet();
try {
factoryBean.getObject().someMethod();
fail("Missing UnsupportedOperationException");
} catch (UnsupportedOperationException e) {
// all good
}
}
@Test // DATACMNS-1345
void reportsMappingContextUnavailableForPersistentEntityLookup() {
@@ -87,7 +115,11 @@ class RepositoryFactoryBeanSupportUnitTests {
.isThrownBy(() -> bean.getPersistentEntity());
}
interface SampleRepository extends Repository<Object, Long> {}
interface SampleRepository extends Repository<Object, Long> {
void someMethod();
}
interface SampleWithQuerydslRepository extends Repository<Object, Long>, QuerydslPredicateExecutor<Object> {}
}