Skip to content

Commit 8705171

Browse files
committed
Fix ReactiveQueryMethodEvaluationContextProvider initialization in R2dbcRepositoryFactoryBean.
We now correctly in initialize ReactiveQueryMethodEvaluationContextProvider in the repository factory bean. Previously, we used an empty instance of ReactiveQueryMethodEvaluationContextProvider that didn't consider registered extensions. Closes #658
1 parent 712992c commit 8705171

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

src/main/java/org/springframework/data/r2dbc/repository/support/R2dbcRepositoryFactoryBean.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
package org.springframework.data.r2dbc.repository.support;
1717

1818
import java.io.Serializable;
19+
import java.util.Optional;
1920

2021
import org.springframework.beans.BeansException;
22+
import org.springframework.beans.factory.ListableBeanFactory;
2123
import org.springframework.context.ApplicationContext;
2224
import org.springframework.context.ApplicationContextAware;
2325
import org.springframework.data.mapping.context.MappingContext;
@@ -27,7 +29,8 @@
2729
import org.springframework.data.repository.Repository;
2830
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
2931
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
30-
import org.springframework.data.repository.query.ReactiveQueryMethodEvaluationContextProvider;
32+
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
33+
import org.springframework.data.repository.query.ReactiveExtensionAwareQueryMethodEvaluationContextProvider;
3134
import org.springframework.lang.Nullable;
3235
import org.springframework.r2dbc.core.DatabaseClient;
3336
import org.springframework.util.Assert;
@@ -58,7 +61,6 @@ public class R2dbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
5861
*/
5962
public R2dbcRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
6063
super(repositoryInterface);
61-
setEvaluationContextProvider(ReactiveQueryMethodEvaluationContextProvider.DEFAULT);
6264
}
6365

6466
/**
@@ -104,6 +106,16 @@ protected final RepositoryFactorySupport createRepositoryFactory() {
104106
: getFactoryInstance(this.client, this.dataAccessStrategy);
105107
}
106108

109+
/*
110+
* (non-Javadoc)
111+
* @see org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport#createDefaultQueryMethodEvaluationContextProvider(ListableBeanFactory)
112+
*/
113+
@Override
114+
protected Optional<QueryMethodEvaluationContextProvider> createDefaultQueryMethodEvaluationContextProvider(
115+
ListableBeanFactory beanFactory) {
116+
return Optional.of(new ReactiveExtensionAwareQueryMethodEvaluationContextProvider(beanFactory));
117+
}
118+
107119
/**
108120
* Creates and initializes a {@link RepositoryFactorySupport} instance.
109121
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.r2dbc.repository.support;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.beans.factory.ListableBeanFactory;
24+
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
25+
import org.springframework.data.r2dbc.dialect.H2Dialect;
26+
import org.springframework.data.r2dbc.repository.R2dbcRepository;
27+
import org.springframework.data.repository.query.ReactiveExtensionAwareQueryMethodEvaluationContextProvider;
28+
import org.springframework.r2dbc.core.DatabaseClient;
29+
import org.springframework.test.util.ReflectionTestUtils;
30+
31+
/**
32+
* Unit tests for {@link R2dbcRepositoryFactoryBean}.
33+
*
34+
* @author Mark Paluch
35+
*/
36+
class R2dbcRepositoryFactoryBeanUnitTests {
37+
38+
@Test // #658
39+
void shouldConfigureReactiveExtensionAwareQueryMethodEvaluationContextProvider() {
40+
41+
R2dbcRepositoryFactoryBean<PersonRepository, Person, String> factoryBean = new R2dbcRepositoryFactoryBean<>(
42+
PersonRepository.class);
43+
factoryBean.setBeanFactory(mock(ListableBeanFactory.class));
44+
R2dbcEntityTemplate operations = new R2dbcEntityTemplate(mock(DatabaseClient.class), H2Dialect.INSTANCE);
45+
factoryBean.setEntityOperations(operations);
46+
factoryBean.setLazyInit(true);
47+
factoryBean.afterPropertiesSet();
48+
49+
Object factory = ReflectionTestUtils.getField(factoryBean, "factory");
50+
Object evaluationContextProvider = ReflectionTestUtils.getField(factory, "evaluationContextProvider");
51+
52+
assertThat(evaluationContextProvider).isInstanceOf(ReactiveExtensionAwareQueryMethodEvaluationContextProvider.class)
53+
.isNotEqualTo(ReactiveExtensionAwareQueryMethodEvaluationContextProvider.DEFAULT);
54+
}
55+
56+
static class Person {}
57+
58+
interface PersonRepository extends R2dbcRepository<Person, String>
59+
60+
{}
61+
}

0 commit comments

Comments
 (0)