Skip to content

Add setting mappingContext in afterPropertiesSet() #2070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
package org.springframework.data.jdbc.repository.support;

import java.io.Serializable;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
Expand Down Expand Up @@ -49,6 +47,7 @@
* @author Hebert Coelho
* @author Chirag Tailor
* @author Mikhail Polivakha
* @author Sergey Korotaev
*/
public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
extends TransactionalRepositoryFactoryBeanSupport<T, S, ID> implements ApplicationEventPublisherAware {
Expand All @@ -58,7 +57,7 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
private RelationalMappingContext mappingContext;
private JdbcConverter converter;
private DataAccessStrategy dataAccessStrategy;
private QueryMappingConfiguration queryMappingConfiguration = QueryMappingConfiguration.EMPTY;
private QueryMappingConfiguration queryMappingConfiguration;
private NamedParameterJdbcOperations operations;
private EntityCallbacks entityCallbacks;
private Dialect dialect;
Expand Down Expand Up @@ -124,7 +123,6 @@ public void setDataAccessStrategy(DataAccessStrategy dataAccessStrategy) {
* @param queryMappingConfiguration can be {@literal null}. {@link #afterPropertiesSet()} defaults to
* {@link QueryMappingConfiguration#EMPTY} if {@literal null}.
*/
@Autowired(required = false)
public void setQueryMappingConfiguration(QueryMappingConfiguration queryMappingConfiguration) {

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

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

if (mappingContext == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can actually get the MappingContext from JdbcConverter and therefore, we don't require any further lookup.

I suggest that you also have a look at setQueryMappingConfiguration(…) and remove @Autowired(required = false) from there and fall back to a lookup (via ObjectProvider) in afterPropertiesSet.

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

this.mappingContext = beanFactory.getBean(RelationalMappingContext.class);
}

if (this.operations == null) {

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

if (this.queryMappingConfiguration == null) {
this.queryMappingConfiguration = QueryMappingConfiguration.EMPTY;
Assert.state(beanFactory != null, "If no QueryMappingConfiguration are set a BeanFactory must be available");

this.queryMappingConfiguration = beanFactory.getBeanProvider(QueryMappingConfiguration.class)
.getIfAvailable(() -> QueryMappingConfiguration.EMPTY);
}

if (this.dataAccessStrategy == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,14 @@ public void setUp() {

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

ObjectProvider<DataAccessStrategy> provider = mock(ObjectProvider.class);
when(beanFactory.getBeanProvider(DataAccessStrategy.class)).thenReturn(provider);
when(provider.getIfAvailable(any()))
ObjectProvider<DataAccessStrategy> dataAccessStrategyObjectProvider = mock(ObjectProvider.class);
ObjectProvider<QueryMappingConfiguration> queryMappingConfigurationObjectProvider = mock(ObjectProvider.class);
when(beanFactory.getBeanProvider(DataAccessStrategy.class)).thenReturn(dataAccessStrategyObjectProvider);
when(beanFactory.getBeanProvider(QueryMappingConfiguration.class)).thenReturn(queryMappingConfigurationObjectProvider);
when(beanFactory.getBean(RelationalMappingContext.class)).thenReturn(mappingContext);
when(dataAccessStrategyObjectProvider.getIfAvailable(any()))
.then((Answer<?>) invocation -> ((Supplier<?>) invocation.getArgument(0)).get());
when(queryMappingConfigurationObjectProvider.getIfAvailable(any()))
.then((Answer<?>) invocation -> ((Supplier<?>) invocation.getArgument(0)).get());
}

Expand Down Expand Up @@ -114,7 +119,6 @@ public void afterPropertiesThrowsExceptionWhenNoMappingContextSet() {
@Test // DATAJDBC-155
public void afterPropertiesSetDefaultsNullablePropertiesCorrectly() {

factoryBean.setMappingContext(mappingContext);
factoryBean.setConverter(new MappingJdbcConverter(mappingContext, dataAccessStrategy));
factoryBean.setApplicationEventPublisher(publisher);
factoryBean.setBeanFactory(beanFactory);
Expand All @@ -126,6 +130,8 @@ public void afterPropertiesSetDefaultsNullablePropertiesCorrectly() {
.isInstanceOf(DefaultDataAccessStrategy.class);
assertThat(ReflectionTestUtils.getField(factoryBean, "queryMappingConfiguration"))
.isEqualTo(QueryMappingConfiguration.EMPTY);
assertThat(ReflectionTestUtils.getField(factoryBean, "mappingContext"))
.isEqualTo(mappingContext);
}

private static class DummyEntity {
Expand Down