Skip to content

Commit f9657a1

Browse files
Added setting mappingContext and mappingConfiguration in afterPropertiesSet() and removed Autowired
Signed-off-by: Sergey Korotaev <sergey.evgen.kor2501@gmail.com>
1 parent e82befd commit f9657a1

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
package org.springframework.data.jdbc.repository.support;
1717

1818
import java.io.Serializable;
19-
2019
import org.springframework.beans.factory.BeanFactory;
21-
import org.springframework.beans.factory.annotation.Autowired;
2220
import org.springframework.context.ApplicationEventPublisher;
2321
import org.springframework.context.ApplicationEventPublisherAware;
2422
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
@@ -49,6 +47,7 @@
4947
* @author Hebert Coelho
5048
* @author Chirag Tailor
5149
* @author Mikhail Polivakha
50+
* @author Sergey Korotaev
5251
*/
5352
public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
5453
extends TransactionalRepositoryFactoryBeanSupport<T, S, ID> implements ApplicationEventPublisherAware {
@@ -58,7 +57,7 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
5857
private RelationalMappingContext mappingContext;
5958
private JdbcConverter converter;
6059
private DataAccessStrategy dataAccessStrategy;
61-
private QueryMappingConfiguration queryMappingConfiguration = QueryMappingConfiguration.EMPTY;
60+
private QueryMappingConfiguration queryMappingConfiguration;
6261
private NamedParameterJdbcOperations operations;
6362
private EntityCallbacks entityCallbacks;
6463
private Dialect dialect;
@@ -124,7 +123,6 @@ public void setDataAccessStrategy(DataAccessStrategy dataAccessStrategy) {
124123
* @param queryMappingConfiguration can be {@literal null}. {@link #afterPropertiesSet()} defaults to
125124
* {@link QueryMappingConfiguration#EMPTY} if {@literal null}.
126125
*/
127-
@Autowired(required = false)
128126
public void setQueryMappingConfiguration(QueryMappingConfiguration queryMappingConfiguration) {
129127

130128
Assert.notNull(queryMappingConfiguration, "QueryMappingConfiguration must not be null");
@@ -157,9 +155,14 @@ public void setBeanFactory(BeanFactory beanFactory) {
157155
@Override
158156
public void afterPropertiesSet() {
159157

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

160+
if (mappingContext == null) {
161+
Assert.state(beanFactory != null, "If no MappingContext are set a BeanFactory must be available");
162+
163+
this.mappingContext = beanFactory.getBean(RelationalMappingContext.class);
164+
}
165+
163166
if (this.operations == null) {
164167

165168
Assert.state(beanFactory != null, "If no JdbcOperations are set a BeanFactory must be available");
@@ -168,7 +171,10 @@ public void afterPropertiesSet() {
168171
}
169172

170173
if (this.queryMappingConfiguration == null) {
171-
this.queryMappingConfiguration = QueryMappingConfiguration.EMPTY;
174+
Assert.state(beanFactory != null, "If no QueryMappingConfiguration are set a BeanFactory must be available");
175+
176+
this.queryMappingConfiguration = beanFactory.getBeanProvider(QueryMappingConfiguration.class)
177+
.getIfAvailable(() -> QueryMappingConfiguration.EMPTY);
172178
}
173179

174180
if (this.dataAccessStrategy == null) {

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,14 @@ public void setUp() {
7878

7979
when(beanFactory.getBean(NamedParameterJdbcOperations.class)).thenReturn(mock(NamedParameterJdbcOperations.class));
8080

81-
ObjectProvider<DataAccessStrategy> provider = mock(ObjectProvider.class);
82-
when(beanFactory.getBeanProvider(DataAccessStrategy.class)).thenReturn(provider);
83-
when(provider.getIfAvailable(any()))
81+
ObjectProvider<DataAccessStrategy> dataAccessStrategyObjectProvider = mock(ObjectProvider.class);
82+
ObjectProvider<QueryMappingConfiguration> queryMappingConfigurationObjectProvider = mock(ObjectProvider.class);
83+
when(beanFactory.getBeanProvider(DataAccessStrategy.class)).thenReturn(dataAccessStrategyObjectProvider);
84+
when(beanFactory.getBeanProvider(QueryMappingConfiguration.class)).thenReturn(queryMappingConfigurationObjectProvider);
85+
when(beanFactory.getBean(RelationalMappingContext.class)).thenReturn(mappingContext);
86+
when(dataAccessStrategyObjectProvider.getIfAvailable(any()))
87+
.then((Answer<?>) invocation -> ((Supplier<?>) invocation.getArgument(0)).get());
88+
when(queryMappingConfigurationObjectProvider.getIfAvailable(any()))
8489
.then((Answer<?>) invocation -> ((Supplier<?>) invocation.getArgument(0)).get());
8590
}
8691

@@ -114,7 +119,6 @@ public void afterPropertiesThrowsExceptionWhenNoMappingContextSet() {
114119
@Test // DATAJDBC-155
115120
public void afterPropertiesSetDefaultsNullablePropertiesCorrectly() {
116121

117-
factoryBean.setMappingContext(mappingContext);
118122
factoryBean.setConverter(new MappingJdbcConverter(mappingContext, dataAccessStrategy));
119123
factoryBean.setApplicationEventPublisher(publisher);
120124
factoryBean.setBeanFactory(beanFactory);
@@ -126,6 +130,8 @@ public void afterPropertiesSetDefaultsNullablePropertiesCorrectly() {
126130
.isInstanceOf(DefaultDataAccessStrategy.class);
127131
assertThat(ReflectionTestUtils.getField(factoryBean, "queryMappingConfiguration"))
128132
.isEqualTo(QueryMappingConfiguration.EMPTY);
133+
assertThat(ReflectionTestUtils.getField(factoryBean, "mappingContext"))
134+
.isEqualTo(mappingContext);
129135
}
130136

131137
private static class DummyEntity {

0 commit comments

Comments
 (0)