Skip to content

Commit 266130f

Browse files
committed
add jdbcAggregateOperationsRef to @EnableJdbcRepositories
1 parent f3a5b27 commit 266130f

16 files changed

+181
-110
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateOperations.java

+17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.springframework.data.domain.Page;
2323
import org.springframework.data.domain.Pageable;
2424
import org.springframework.data.domain.Sort;
25+
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
26+
import org.springframework.data.jdbc.core.convert.JdbcConverter;
2527
import org.springframework.data.relational.core.query.Query;
2628
import org.springframework.lang.Nullable;
2729

@@ -34,6 +36,7 @@
3436
* @author Chirag Tailor
3537
* @author Diego Krupitza
3638
* @author Myeonghyeon Lee
39+
* @author Tomohiko Ozawa
3740
*/
3841
public interface JdbcAggregateOperations {
3942

@@ -312,4 +315,18 @@ default <T> void delete(T aggregateRoot, Class<T> domainType) {
312315
default <T> void deleteAll(Iterable<? extends T> aggregateRoots, Class<T> domainType) {
313316
deleteAll(aggregateRoots);
314317
}
318+
319+
/**
320+
* Returns the {@link JdbcConverter}.
321+
*
322+
* @return the {@link JdbcConverter}.
323+
*/
324+
JdbcConverter getConverter();
325+
326+
/**
327+
* Return the {@link DataAccessStrategy}
328+
*
329+
* @return the {@link DataAccessStrategy}
330+
*/
331+
DataAccessStrategy getDataAccessStrategy();
315332
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java

+20-13
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
* @author Myeonghyeon Lee
6868
* @author Chirag Tailor
6969
* @author Diego Krupitza
70+
* @author Tomohiko Ozawa
7071
*/
7172
public class JdbcAggregateTemplate implements JdbcAggregateOperations {
7273

@@ -86,22 +87,20 @@ public class JdbcAggregateTemplate implements JdbcAggregateOperations {
8687
* {@link DataAccessStrategy}.
8788
*
8889
* @param publisher must not be {@literal null}.
89-
* @param context must not be {@literal null}.
9090
* @param dataAccessStrategy must not be {@literal null}.
9191
* @since 1.1
9292
*/
93-
public JdbcAggregateTemplate(ApplicationContext publisher, RelationalMappingContext context, JdbcConverter converter,
93+
public JdbcAggregateTemplate(ApplicationContext publisher, JdbcConverter converter,
9494
DataAccessStrategy dataAccessStrategy) {
9595

9696
Assert.notNull(publisher, "ApplicationContext must not be null");
97-
Assert.notNull(context, "RelationalMappingContext must not be null");
9897
Assert.notNull(converter, "RelationalConverter must not be null");
9998
Assert.notNull(dataAccessStrategy, "DataAccessStrategy must not be null");
10099

101100
this.eventDelegate.setPublisher(publisher);
102-
this.context = context;
103-
this.accessStrategy = dataAccessStrategy;
104101
this.converter = converter;
102+
this.accessStrategy = dataAccessStrategy;
103+
this.context = converter.getMappingContext();
105104

106105
this.jdbcEntityDeleteWriter = new RelationalEntityDeleteWriter(context);
107106

@@ -115,21 +114,19 @@ public JdbcAggregateTemplate(ApplicationContext publisher, RelationalMappingCont
115114
* {@link RelationalMappingContext} and {@link DataAccessStrategy}.
116115
*
117116
* @param publisher must not be {@literal null}.
118-
* @param context must not be {@literal null}.
119117
* @param dataAccessStrategy must not be {@literal null}.
120118
*/
121-
public JdbcAggregateTemplate(ApplicationEventPublisher publisher, RelationalMappingContext context,
122-
JdbcConverter converter, DataAccessStrategy dataAccessStrategy) {
119+
public JdbcAggregateTemplate(ApplicationEventPublisher publisher, JdbcConverter converter,
120+
DataAccessStrategy dataAccessStrategy) {
123121

124122
Assert.notNull(publisher, "ApplicationEventPublisher must not be null");
125-
Assert.notNull(context, "RelationalMappingContext must not be null");
126123
Assert.notNull(converter, "RelationalConverter must not be null");
127124
Assert.notNull(dataAccessStrategy, "DataAccessStrategy must not be null");
128125

129126
this.eventDelegate.setPublisher(publisher);
130-
this.context = context;
131-
this.accessStrategy = dataAccessStrategy;
132127
this.converter = converter;
128+
this.accessStrategy = dataAccessStrategy;
129+
this.context = converter.getMappingContext();
133130

134131
this.jdbcEntityDeleteWriter = new RelationalEntityDeleteWriter(context);
135132
this.executor = new AggregateChangeExecutor(converter, accessStrategy);
@@ -656,9 +653,19 @@ private <T> T triggerBeforeDelete(@Nullable T aggregateRoot, Object id, MutableA
656653
return null;
657654
}
658655

659-
private record EntityAndPreviousVersion<T> (T entity, @Nullable Number version) {
656+
private record EntityAndPreviousVersion<T>(T entity, @Nullable Number version) {
660657
}
661658

662-
private record EntityAndChangeCreator<T> (T entity, Function<T, RootAggregateChange<T>> changeCreator) {
659+
private record EntityAndChangeCreator<T>(T entity, Function<T, RootAggregateChange<T>> changeCreator) {
660+
}
661+
662+
@Override
663+
public DataAccessStrategy getDataAccessStrategy() {
664+
return accessStrategy;
665+
}
666+
667+
@Override
668+
public JdbcConverter getConverter() {
669+
return converter;
663670
}
664671
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/DialectResolver.java spring-data-jdbc/src/main/java/org/springframework/data/jdbc/dialect/DialectResolver.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.jdbc.repository.config;
16+
package org.springframework.data.jdbc.dialect;
1717

1818
import java.sql.Connection;
1919
import java.sql.DatabaseMetaData;

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/AbstractJdbcConfiguration.java

+16-23
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.apache.commons.logging.Log;
2727
import org.apache.commons.logging.LogFactory;
2828
import org.springframework.beans.BeansException;
29-
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3029
import org.springframework.context.ApplicationContext;
3130
import org.springframework.context.ApplicationContextAware;
3231
import org.springframework.context.annotation.Bean;
@@ -40,6 +39,7 @@
4039
import org.springframework.data.jdbc.core.dialect.JdbcDialect;
4140
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
4241
import org.springframework.data.jdbc.core.mapping.JdbcSimpleTypes;
42+
import org.springframework.data.jdbc.dialect.DialectResolver;
4343
import org.springframework.data.mapping.model.SimpleTypeHolder;
4444
import org.springframework.data.relational.RelationalManagedTypes;
4545
import org.springframework.data.relational.core.conversion.RelationalConverter;
@@ -61,6 +61,7 @@
6161
* @author Christoph Strobl
6262
* @author Myeonghyeon Lee
6363
* @author Chirag Tailor
64+
* @author Tomohiko Ozawa
6465
* @since 1.1
6566
*/
6667
@Configuration(proxyBeanMethods = false)
@@ -103,7 +104,7 @@ public RelationalManagedTypes jdbcManagedTypes() throws ClassNotFoundException {
103104
*
104105
* @param namingStrategy optional {@link NamingStrategy}. Use
105106
* {@link org.springframework.data.relational.core.mapping.DefaultNamingStrategy#INSTANCE} as fallback.
106-
* @param customConversions see {@link #jdbcCustomConversions()}.
107+
* @param customConversions see {@link #jdbcCustomConversions(Optional)}.
107108
* @param jdbcManagedTypes JDBC managed types, typically discovered through {@link #jdbcManagedTypes() an entity
108109
* scan}.
109110
* @return must not be {@literal null}.
@@ -124,7 +125,7 @@ public JdbcMappingContext jdbcMappingContext(Optional<NamingStrategy> namingStra
124125
* {@link #jdbcMappingContext(Optional, JdbcCustomConversions, RelationalManagedTypes)}.
125126
*
126127
* @see #jdbcMappingContext(Optional, JdbcCustomConversions, RelationalManagedTypes)
127-
* @see #jdbcCustomConversions()
128+
* @see #jdbcCustomConversions(Optional)
128129
* @return must not be {@literal null}.
129130
*/
130131
@Bean
@@ -147,23 +148,17 @@ public JdbcConverter jdbcConverter(JdbcMappingContext mappingContext, NamedParam
147148
* @return will never be {@literal null}.
148149
*/
149150
@Bean
150-
public JdbcCustomConversions jdbcCustomConversions() {
151-
152-
try {
153-
154-
Dialect dialect = applicationContext.getBean(Dialect.class);
155-
SimpleTypeHolder simpleTypeHolder = dialect.simpleTypes().isEmpty() ? JdbcSimpleTypes.HOLDER
156-
: new SimpleTypeHolder(dialect.simpleTypes(), JdbcSimpleTypes.HOLDER);
157-
158-
return new JdbcCustomConversions(
159-
CustomConversions.StoreConversions.of(simpleTypeHolder, storeConverters(dialect)), userConverters());
160-
161-
} catch (NoSuchBeanDefinitionException exception) {
162-
151+
public JdbcCustomConversions jdbcCustomConversions(Optional<Dialect> dialect) {
152+
return dialect.map(d -> {
153+
SimpleTypeHolder simpleTypeHolder = d.simpleTypes().isEmpty()
154+
? JdbcSimpleTypes.HOLDER
155+
: new SimpleTypeHolder(d.simpleTypes(), JdbcSimpleTypes.HOLDER);
156+
return new JdbcCustomConversions(CustomConversions.StoreConversions.of(simpleTypeHolder, storeConverters(d)),
157+
userConverters());
158+
}).orElseGet(() -> {
163159
LOG.warn("No dialect found; CustomConversions will be configured without dialect specific conversions");
164-
165160
return new JdbcCustomConversions();
166-
}
161+
});
167162
}
168163

169164
protected List<?> userConverters() {
@@ -191,7 +186,7 @@ private List<Object> storeConverters(Dialect dialect) {
191186
public JdbcAggregateTemplate jdbcAggregateTemplate(ApplicationContext applicationContext,
192187
JdbcMappingContext mappingContext, JdbcConverter converter, DataAccessStrategy dataAccessStrategy) {
193188

194-
return new JdbcAggregateTemplate(applicationContext, mappingContext, converter, dataAccessStrategy);
189+
return new JdbcAggregateTemplate(applicationContext, converter, dataAccessStrategy);
195190
}
196191

197192
/**
@@ -207,8 +202,7 @@ public DataAccessStrategy dataAccessStrategyBean(NamedParameterJdbcOperations op
207202

208203
SqlGeneratorSource sqlGeneratorSource = new SqlGeneratorSource(context, jdbcConverter, dialect);
209204
DataAccessStrategyFactory factory = new DataAccessStrategyFactory(sqlGeneratorSource, jdbcConverter, operations,
210-
new SqlParametersFactory(context, jdbcConverter),
211-
new InsertStrategyFactory(operations, dialect));
205+
new SqlParametersFactory(context, jdbcConverter), new InsertStrategyFactory(operations, dialect));
212206

213207
return factory.create();
214208
}
@@ -219,8 +213,7 @@ public DataAccessStrategy dataAccessStrategyBean(NamedParameterJdbcOperations op
219213
* @param operations the {@link NamedParameterJdbcOperations} allowing access to a {@link java.sql.Connection}.
220214
* @return the {@link Dialect} to be used.
221215
* @since 2.0
222-
* @throws org.springframework.data.jdbc.repository.config.DialectResolver.NoDialectException if the {@link Dialect}
223-
* cannot be determined.
216+
* @throws DialectResolver.NoDialectException if the {@link Dialect} cannot be determined.
224217
*/
225218
@Bean
226219
public Dialect jdbcDialect(NamedParameterJdbcOperations operations) {

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/EnableJdbcRepositories.java

+8
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@
122122
/**
123123
* Configures the name of the {@link org.springframework.data.jdbc.core.convert.DataAccessStrategy} bean definition to
124124
* be used to create repositories discovered through this annotation. Defaults to {@code defaultDataAccessStrategy}.
125+
* @deprecated since 3.3 use {@link #jdbcAggregateOperationsRef()} instead
125126
*/
127+
@Deprecated(since = "3.3")
126128
String dataAccessStrategyRef() default "";
127129

128130
/**
@@ -133,6 +135,12 @@
133135
*/
134136
String transactionManagerRef() default "transactionManager";
135137

138+
/**
139+
* Configure the name of the {@link org.springframework.data.jdbc.core.JdbcAggregateOperations} bean definition to be
140+
* used to create repositories discovered through this annotation.
141+
*/
142+
String jdbcAggregateOperationsRef() default "";
143+
136144
/**
137145
* Returns the key of the {@link QueryLookupStrategy} to be used for lookup queries for query methods. Defaults to
138146
* {@link QueryLookupStrategy.Key#CREATE_IF_NOT_FOUND}.

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/JdbcRepositoryConfigExtension.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* @author Fei Dong
4141
* @author Mark Paluch
4242
* @author Antoine Sauray
43+
* @author Tomohiko Ozawa
4344
*/
4445
public class JdbcRepositoryConfigExtension extends RepositoryConfigurationExtensionSupport {
4546

@@ -79,9 +80,15 @@ public void postProcess(BeanDefinitionBuilder builder, RepositoryConfigurationSo
7980
Optional<String> transactionManagerRef = source.getAttribute("transactionManagerRef");
8081
builder.addPropertyValue("transactionManager", transactionManagerRef.orElse(DEFAULT_TRANSACTION_MANAGER_BEAN_NAME));
8182

82-
builder.addPropertyValue("mappingContext", new RuntimeBeanReference(JdbcMappingContext.class));
83-
builder.addPropertyValue("dialect", new RuntimeBeanReference(Dialect.class));
84-
builder.addPropertyValue("converter", new RuntimeBeanReference(JdbcConverter.class));
83+
Optional<String> jdbcAggregateOperationsRef = source.getAttribute("jdbcAggregateOperationsRef");
84+
85+
if (jdbcAggregateOperationsRef.isPresent()) {
86+
builder.addPropertyReference("jdbcAggregateOperations", jdbcAggregateOperationsRef.get());
87+
} else {
88+
builder.addPropertyValue("mappingContext", new RuntimeBeanReference(JdbcMappingContext.class));
89+
builder.addPropertyValue("dialect", new RuntimeBeanReference(Dialect.class));
90+
builder.addPropertyValue("converter", new RuntimeBeanReference(JdbcConverter.class));
91+
}
8592
}
8693

8794
/**

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919

2020
import org.springframework.beans.factory.BeanFactory;
2121
import org.springframework.context.ApplicationEventPublisher;
22+
import org.springframework.data.jdbc.core.JdbcAggregateOperations;
2223
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
2324
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
2425
import org.springframework.data.jdbc.core.convert.JdbcConverter;
26+
import org.springframework.data.jdbc.dialect.DialectResolver;
2527
import org.springframework.data.jdbc.repository.QueryMappingConfiguration;
2628
import org.springframework.data.mapping.callback.EntityCallbacks;
2729
import org.springframework.data.relational.core.dialect.Dialect;
@@ -48,6 +50,7 @@
4850
* @author Hebert Coelho
4951
* @author Diego Krupitza
5052
* @author Christopher Klein
53+
* @author Tomohiko Ozawa
5154
*/
5255
public class JdbcRepositoryFactory extends RepositoryFactorySupport {
5356

@@ -62,6 +65,20 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
6265
private QueryMappingConfiguration queryMappingConfiguration = QueryMappingConfiguration.EMPTY;
6366
private EntityCallbacks entityCallbacks;
6467

68+
public JdbcRepositoryFactory(ApplicationEventPublisher publisher, JdbcAggregateOperations jdbcAggregateOperations,
69+
NamedParameterJdbcOperations operations) {
70+
Assert.notNull(publisher, "ApplicationEventPublisher must not be null");
71+
Assert.notNull(jdbcAggregateOperations, "JdbcAggregateOperations must not be null");
72+
Assert.notNull(operations, "NamedParameterJdbcOperations must not be null");
73+
74+
this.converter = jdbcAggregateOperations.getConverter();
75+
this.accessStrategy = jdbcAggregateOperations.getDataAccessStrategy();
76+
this.context = jdbcAggregateOperations.getConverter().getMappingContext();
77+
this.dialect = DialectResolver.getDialect(operations.getJdbcOperations());
78+
this.operations = operations;
79+
this.publisher = publisher;
80+
}
81+
6582
/**
6683
* Creates a new {@link JdbcRepositoryFactory} for the given {@link DataAccessStrategy},
6784
* {@link RelationalMappingContext} and {@link ApplicationEventPublisher}.
@@ -114,7 +131,7 @@ public <T, ID> EntityInformation<T, ID> getEntityInformation(Class<T> aClass) {
114131
@Override
115132
protected Object getTargetRepository(RepositoryInformation repositoryInformation) {
116133

117-
JdbcAggregateTemplate template = new JdbcAggregateTemplate(publisher, context, converter, accessStrategy);
134+
JdbcAggregateTemplate template = new JdbcAggregateTemplate(publisher, converter, accessStrategy);
118135

119136
if (entityCallbacks != null) {
120137
template.setEntityCallbacks(entityCallbacks);

0 commit comments

Comments
 (0)