Skip to content

Commit 1ea4955

Browse files
Add setting mappingContext in afterPropertiesSet()
Signed-off-by: Sergey Korotaev <sergey.evgen.kor2501@gmail.com>
1 parent e82befd commit 1ea4955

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBean.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* @author Hebert Coelho
5050
* @author Chirag Tailor
5151
* @author Mikhail Polivakha
52+
* @author Sergey Korotaev
5253
*/
5354
public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
5455
extends TransactionalRepositoryFactoryBeanSupport<T, S, ID> implements ApplicationEventPublisherAware {
@@ -157,9 +158,14 @@ public void setBeanFactory(BeanFactory beanFactory) {
157158
@Override
158159
public void afterPropertiesSet() {
159160

160-
Assert.state(this.mappingContext != null, "MappingContext is required and must not be null");
161161
Assert.state(this.converter != null, "RelationalConverter is required and must not be null");
162162

163+
if (mappingContext == null) {
164+
Assert.state(beanFactory != null, "If no MappingContext are set a BeanFactory must be available");
165+
166+
this.mappingContext = beanFactory.getBean(RelationalMappingContext.class);
167+
}
168+
163169
if (this.operations == null) {
164170

165171
Assert.state(beanFactory != null, "If no JdbcOperations are set a BeanFactory must be available");

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryFactoryBeanUnitTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public void setUp() {
8080

8181
ObjectProvider<DataAccessStrategy> provider = mock(ObjectProvider.class);
8282
when(beanFactory.getBeanProvider(DataAccessStrategy.class)).thenReturn(provider);
83+
when(beanFactory.getBean(RelationalMappingContext.class)).thenReturn(mappingContext);
8384
when(provider.getIfAvailable(any()))
8485
.then((Answer<?>) invocation -> ((Supplier<?>) invocation.getArgument(0)).get());
8586
}
@@ -114,7 +115,6 @@ public void afterPropertiesThrowsExceptionWhenNoMappingContextSet() {
114115
@Test // DATAJDBC-155
115116
public void afterPropertiesSetDefaultsNullablePropertiesCorrectly() {
116117

117-
factoryBean.setMappingContext(mappingContext);
118118
factoryBean.setConverter(new MappingJdbcConverter(mappingContext, dataAccessStrategy));
119119
factoryBean.setApplicationEventPublisher(publisher);
120120
factoryBean.setBeanFactory(beanFactory);
@@ -126,6 +126,8 @@ public void afterPropertiesSetDefaultsNullablePropertiesCorrectly() {
126126
.isInstanceOf(DefaultDataAccessStrategy.class);
127127
assertThat(ReflectionTestUtils.getField(factoryBean, "queryMappingConfiguration"))
128128
.isEqualTo(QueryMappingConfiguration.EMPTY);
129+
assertThat(ReflectionTestUtils.getField(factoryBean, "mappingContext"))
130+
.isEqualTo(mappingContext);
129131
}
130132

131133
private static class DummyEntity {

0 commit comments

Comments
 (0)