Skip to content

Commit

Permalink
recommit
Browse files Browse the repository at this point in the history
  • Loading branch information
kota65535 committed Nov 2, 2021
1 parent b1b5fe9 commit 380c61d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* @author Mark Paluch
* @author Fei Dong
* @author Antoine Sauray
* @author Tomohiko Ozawa
* @see AbstractJdbcConfiguration
*/
@Target(ElementType.TYPE)
Expand Down Expand Up @@ -131,4 +132,11 @@
*/
String transactionManagerRef() default "transactionManager";

/**
* Configures the name of the {@link org.springframework.data.jdbc.core.convert.JdbcConverter} bean definition to be
* used to create repositories discovered through this annotation. Defaults to {@code converter}.
*
* @since 2.3
*/
String jdbcConverterRef() default "converter";
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* @author Fei Dong
* @author Mark Paluch
* @author Antoine Sauray
* @author Tomohiko Ozawa
*/
public class JdbcRepositoryConfigExtension extends RepositoryConfigurationExtensionSupport {

Expand Down Expand Up @@ -85,6 +86,9 @@ public void postProcess(BeanDefinitionBuilder builder, RepositoryConfigurationSo

Optional<String> transactionManagerRef = source.getAttribute("transactionManagerRef");
builder.addPropertyValue("transactionManager", transactionManagerRef.orElse(DEFAULT_TRANSACTION_MANAGER_BEAN_NAME));

source.getAttribute("jdbcConverterRef").filter(StringUtils::hasText) //
.ifPresent(s -> builder.addPropertyReference("converter", s));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.annotation.Id;
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
import org.springframework.data.jdbc.core.convert.BasicJdbcConverter;
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
import org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy;
import org.springframework.data.jdbc.core.convert.DefaultJdbcTypeFactory;
import org.springframework.data.jdbc.core.convert.JdbcConverter;
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
import org.springframework.data.jdbc.core.convert.SqlGeneratorSource;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.jdbc.repository.QueryMappingConfiguration;
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositoriesIntegrationTests.TestConfiguration;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.mapping.NamingStrategy;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.repository.CrudRepository;
import org.springframework.jdbc.core.RowMapper;
Expand All @@ -60,6 +66,7 @@
* @author Greg Turnquist
* @author Evgeni Dimitrov
* @author Fei Dong
* @author Tomohiko Ozawa
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = TestConfiguration.class)
Expand All @@ -70,22 +77,26 @@ public class EnableJdbcRepositoriesIntegrationTests {
static final Field OPERATIONS = ReflectionUtils.findField(JdbcRepositoryFactoryBean.class, "operations");
static final Field DATA_ACCESS_STRATEGY = ReflectionUtils.findField(JdbcRepositoryFactoryBean.class,
"dataAccessStrategy");
static final Field CONVERTER = ReflectionUtils.findField(JdbcRepositoryFactoryBean.class, "converter");
public static final RowMapper DUMMY_ENTITY_ROW_MAPPER = mock(RowMapper.class);
public static final RowMapper STRING_ROW_MAPPER = mock(RowMapper.class);

@Autowired JdbcRepositoryFactoryBean factoryBean;
@Autowired DummyRepository repository;
@Autowired @Qualifier("namedParameterJdbcTemplate") NamedParameterJdbcOperations defaultOperations;
@Autowired @Qualifier("defaultDataAccessStrategy") DataAccessStrategy defaultDataAccessStrategy;
@Autowired @Qualifier("converter") JdbcConverter defaultJdbcConverter;
@Autowired @Qualifier("qualifierJdbcOperations") NamedParameterJdbcOperations qualifierJdbcOperations;
@Autowired @Qualifier("qualifierDataAccessStrategy") DataAccessStrategy qualifierDataAccessStrategy;
@Autowired @Qualifier("qualifierJdbcConverter") JdbcConverter qualifierJdbcConverter;

@BeforeAll
public static void setup() {

MAPPER_MAP.setAccessible(true);
OPERATIONS.setAccessible(true);
DATA_ACCESS_STRATEGY.setAccessible(true);
CONVERTER.setAccessible(true);
}

@Test // DATAJDBC-100
Expand Down Expand Up @@ -118,6 +129,9 @@ public void jdbcOperationsRef() {
DataAccessStrategy dataAccessStrategy = (DataAccessStrategy) ReflectionUtils.getField(DATA_ACCESS_STRATEGY,
factoryBean);
assertThat(dataAccessStrategy).isNotSameAs(defaultDataAccessStrategy).isSameAs(qualifierDataAccessStrategy);

JdbcConverter converter = (JdbcConverter) ReflectionUtils.getField(CONVERTER, factoryBean);
assertThat(converter).isNotSameAs(defaultJdbcConverter).isSameAs(qualifierJdbcConverter);
}

interface DummyRepository extends CrudRepository<DummyEntity, Long> {
Expand All @@ -133,7 +147,7 @@ static class DummyEntity {
@EnableJdbcRepositories(considerNestedRepositories = true,
includeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = DummyRepository.class),
jdbcOperationsRef = "qualifierJdbcOperations", dataAccessStrategyRef = "qualifierDataAccessStrategy",
repositoryBaseClass = DummyRepositoryBaseClass.class)
jdbcConverterRef = "qualifierJdbcConverter", repositoryBaseClass = DummyRepositoryBaseClass.class)
static class TestConfiguration {

@Bean
Expand Down Expand Up @@ -166,6 +180,19 @@ DataAccessStrategy defaultDataAccessStrategy(
Dialect jdbcDialect(@Qualifier("qualifierJdbcOperations") NamedParameterJdbcOperations operations) {
return DialectResolver.getDialect(operations.getJdbcOperations());
}

@Bean("qualifierJdbcConverter")
JdbcConverter qualifierJdbcConverter(Optional<NamingStrategy> namingStrategy,
@Qualifier("qualifierJdbcOperations") NamedParameterJdbcOperations operations,
@Lazy @Qualifier("qualifierDataAccessStrategy") DataAccessStrategy dataAccessStrategy) {
JdbcCustomConversions conversions = new JdbcCustomConversions();
JdbcMappingContext mappingContext = new JdbcMappingContext(namingStrategy.orElse(NamingStrategy.INSTANCE));
mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
DefaultJdbcTypeFactory jdbcTypeFactory = new DefaultJdbcTypeFactory(operations.getJdbcOperations());
Dialect dialect = DialectResolver.getDialect(operations.getJdbcOperations());
return new BasicJdbcConverter(mappingContext, dataAccessStrategy, conversions, jdbcTypeFactory,
dialect.getIdentifierProcessing());
}
}

private static class DummyRepositoryBaseClass<T, ID> implements CrudRepository<T, ID> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
* @author Fei Dong
* @author Myeonghyeon Lee
* @author Christoph Strobl
* @author Tomohiko Ozawa
*/
@Configuration
@ComponentScan // To pick up configuration classes (per activated profile)
Expand Down Expand Up @@ -135,7 +136,7 @@ private List<Object> storeConverters(Dialect dialect) {
}

@Bean
JdbcConverter relationalConverter(RelationalMappingContext mappingContext, @Lazy RelationResolver relationResolver,
JdbcConverter converter(RelationalMappingContext mappingContext, @Lazy RelationResolver relationResolver,
CustomConversions conversions, @Qualifier("namedParameterJdbcTemplate") NamedParameterJdbcOperations template,
Dialect dialect) {

Expand Down

0 comments on commit 380c61d

Please sign in to comment.