Skip to content
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

DATAJDBC-395 - AbstractJdbcConfiguration no longer registers a bean of type DataAccessStrategy. #160

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-395-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-395-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-395-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-395-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.Optional;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -51,6 +52,8 @@
@Configuration
public abstract class AbstractJdbcConfiguration {

private DefaultDataAccessStrategy defaultDataAccessStrategy = null;

/**
* Register a {@link RelationalMappingContext} and apply an optional {@link NamingStrategy}.
*
Expand All @@ -76,17 +79,22 @@ public JdbcMappingContext jdbcMappingContext(Optional<NamingStrategy> namingStra
*/
@Bean
public JdbcConverter jdbcConverter(RelationalMappingContext mappingContext, NamedParameterJdbcOperations operations,
@Lazy RelationResolver relationResolver) {
ObjectProvider<RelationResolver> relationResolverProvider, Optional<NamingStrategy> namingStrategy,
JdbcConverter jdbcConverter) {

RelationResolver relationResolver = relationResolverProvider
.getIfAvailable(() -> dataAccessStrategy(operations, jdbcConverter, jdbcMappingContext(namingStrategy)));

DefaultJdbcTypeFactory jdbcTypeFactory = new DefaultJdbcTypeFactory(operations.getJdbcOperations());

return new BasicJdbcConverter(mappingContext, relationResolver, jdbcCustomConversions(),
new DefaultJdbcTypeFactory(operations.getJdbcOperations()));
return new BasicJdbcConverter(mappingContext, relationResolver, jdbcCustomConversions(), jdbcTypeFactory);
}

/**
* Register custom {@link Converter}s in a {@link JdbcCustomConversions} object if required. These
* {@link JdbcCustomConversions} will be registered with the
* {@link #jdbcConverter(RelationalMappingContext, NamedParameterJdbcOperations, RelationResolver)}. Returns an empty
* {@link JdbcCustomConversions} instance by default.
* {@link #jdbcConverter(RelationalMappingContext, NamedParameterJdbcOperations, ObjectProvider, Optional, JdbcConverter)}.
* Returns an empty {@link JdbcCustomConversions} instance by default.
*
* @return must not be {@literal null}.
*/
Expand All @@ -106,26 +114,33 @@ public JdbcCustomConversions jdbcCustomConversions() {
*/
@Bean
public JdbcAggregateTemplate jdbcAggregateTemplate(ApplicationEventPublisher publisher,
RelationalMappingContext context, JdbcConverter converter, DataAccessStrategy dataAccessStrategy) {
RelationalMappingContext context, JdbcConverter converter,
ObjectProvider<DataAccessStrategy> dataAccessStrategyProvider, NamedParameterJdbcOperations operations,
Optional<NamingStrategy> namingStrategy, @Lazy JdbcConverter jdbcConverter) {

DataAccessStrategy dataAccessStrategy = dataAccessStrategyProvider //
.getIfAvailable(() -> dataAccessStrategy(operations, jdbcConverter, jdbcMappingContext(namingStrategy)));

return new JdbcAggregateTemplate(publisher, context, converter, dataAccessStrategy);
}

/**
* Register a {@link DataAccessStrategy} as a bean for reuse in the {@link JdbcAggregateOperations} and the
* {@link RelationalConverter}.
* Create a {@link DataAccessStrategy} for reuse in the {@link JdbcAggregateOperations} and the
* {@link RelationalConverter}. It will return the same instance if called multiple times, regardless of the arguments
* provided. Register a bean of type {@link DataAccessStrategy} if your use case requires a more specialized
* DataAccessStrategy.
*
* @param operations
* @param namingStrategy
* @param jdbcConverter
* @return
* @return Guaranteed to be not {@literal null}.
*/
@Bean
public DataAccessStrategy dataAccessStrategy(NamedParameterJdbcOperations operations,
Optional<NamingStrategy> namingStrategy, JdbcConverter jdbcConverter) {
private DataAccessStrategy dataAccessStrategy(NamedParameterJdbcOperations operations, JdbcConverter jdbcConverter,
JdbcMappingContext context) {

if (defaultDataAccessStrategy == null) {

JdbcMappingContext context = jdbcMappingContext(namingStrategy);
return new DefaultDataAccessStrategy(new SqlGeneratorSource(context), context, jdbcConverter, operations);
defaultDataAccessStrategy = //
new DefaultDataAccessStrategy(new SqlGeneratorSource(context), context, jdbcConverter, operations);
}
return defaultDataAccessStrategy;
}

}
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-395-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-395-SNAPSHOT</version>
</parent>

<properties>
Expand Down
6 changes: 3 additions & 3 deletions src/main/asciidoc/jdbc.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,16 @@ References between those should be encoded as simple `id` values, which should m
[[jdbc.entity-persistence.custom-converters]]
=== Custom converters

Custom converters can be registered, for types that are not supported by default, by inheriting your configuration from `JdbcConfiguration` and overwriting the method `jdbcCustomConversions()`.
Custom converters can be registered, for types that are not supported by default, by inheriting your configuration from `AbstractJdbcConfiguration` and overwriting the method `jdbcCustomConversions()`.

====
[source, java]
----
@Configuration
public class DataJdbcConfiguration extends JdbcConfiguration {
public class DataJdbcConfiguration extends AbstractJdbcConfiguration {

@Override
protected JdbcCustomConversions jdbcCustomConversions() {
public JdbcCustomConversions jdbcCustomConversions() {

return new JdbcCustomConversions(Collections.singletonList(TimestampTzToDateConverter.INSTANCE));

Expand Down