Skip to content

Commit b8116d5

Browse files
committed
Clean up and improve Java DSL for JDBC
Follow up for: #10081 * Rename `StoredProcExecutorConfigurer` to `StoredProcExecutorSpec` * Add `Jdbc.storedProcExecutorSpec()` factory * Improve logic of `StoredProcExecutor`-based Java DSL classes to handle an external `StoredProcExecutor` as well * Add `package-info.java` into the `jdbc/dsl` package * Add `/jdbc/dsl.adoc` and respective `What's New` entry
1 parent cd15675 commit b8116d5

File tree

15 files changed

+508
-270
lines changed

15 files changed

+508
-270
lines changed

spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/dsl/Jdbc.java

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* Factory class for JDBC components.
2727
*
2828
* @author Jiandong Ma
29+
* @author Artem Bilan
2930
*
3031
* @since 7.0
3132
*/
@@ -109,7 +110,9 @@ public static JdbcOutboundGatewaySpec outboundGateway(JdbcOperations jdbcOperati
109110
* @param selectQuery the select query to build on
110111
* @return the {@link JdbcOutboundGatewaySpec} instance
111112
*/
112-
public static JdbcOutboundGatewaySpec outboundGateway(JdbcOperations jdbcOperations, String updateQuery, String selectQuery) {
113+
public static JdbcOutboundGatewaySpec outboundGateway(JdbcOperations jdbcOperations, String updateQuery,
114+
String selectQuery) {
115+
113116
return new JdbcOutboundGatewaySpec(jdbcOperations, updateQuery, selectQuery);
114117
}
115118

@@ -119,7 +122,18 @@ public static JdbcOutboundGatewaySpec outboundGateway(JdbcOperations jdbcOperati
119122
* @return the {@link JdbcStoredProcInboundChannelAdapterSpec} instance
120123
*/
121124
public static JdbcStoredProcInboundChannelAdapterSpec storedProcInboundAdapter(DataSource dataSource) {
122-
return new JdbcStoredProcInboundChannelAdapterSpec(new StoredProcExecutor(dataSource));
125+
return new JdbcStoredProcInboundChannelAdapterSpec(storedProcExecutorSpec(dataSource));
126+
}
127+
128+
/**
129+
* The factory to produce a {@link JdbcStoredProcInboundChannelAdapterSpec}.
130+
* @param storedProcExecutor the {@link StoredProcExecutor} to use
131+
* @return the {@link JdbcStoredProcInboundChannelAdapterSpec} instance
132+
*/
133+
public static JdbcStoredProcInboundChannelAdapterSpec storedProcInboundAdapter(
134+
StoredProcExecutor storedProcExecutor) {
135+
136+
return new JdbcStoredProcInboundChannelAdapterSpec(storedProcExecutor);
123137
}
124138

125139
/**
@@ -128,7 +142,18 @@ public static JdbcStoredProcInboundChannelAdapterSpec storedProcInboundAdapter(D
128142
* @return the {@link JdbcStoredProcOutboundChannelAdapterSpec} instance
129143
*/
130144
public static JdbcStoredProcOutboundChannelAdapterSpec storedProcOutboundAdapter(DataSource dataSource) {
131-
return new JdbcStoredProcOutboundChannelAdapterSpec(new StoredProcExecutor(dataSource));
145+
return new JdbcStoredProcOutboundChannelAdapterSpec(storedProcExecutorSpec(dataSource));
146+
}
147+
148+
/**
149+
* The factory to produce a {@link JdbcStoredProcOutboundChannelAdapterSpec}.
150+
* @param storedProcExecutor the {@link StoredProcExecutor} to use
151+
* @return the {@link JdbcStoredProcOutboundChannelAdapterSpec} instance
152+
*/
153+
public static JdbcStoredProcOutboundChannelAdapterSpec storedProcOutboundAdapter(
154+
StoredProcExecutor storedProcExecutor) {
155+
156+
return new JdbcStoredProcOutboundChannelAdapterSpec(storedProcExecutor);
132157
}
133158

134159
/**
@@ -137,7 +162,25 @@ public static JdbcStoredProcOutboundChannelAdapterSpec storedProcOutboundAdapter
137162
* @return the {@link JdbcStoredProcOutboundGatewaySpec} instance
138163
*/
139164
public static JdbcStoredProcOutboundGatewaySpec storedProcOutboundGateway(DataSource dataSource) {
140-
return new JdbcStoredProcOutboundGatewaySpec(new StoredProcExecutor(dataSource));
165+
return new JdbcStoredProcOutboundGatewaySpec(storedProcExecutorSpec(dataSource));
166+
}
167+
168+
/**
169+
* The factory to produce a {@link JdbcStoredProcOutboundGatewaySpec}.
170+
* @param storedProcExecutor the {@link StoredProcExecutor} to use
171+
* @return the {@link JdbcStoredProcOutboundGatewaySpec} instance
172+
*/
173+
public static JdbcStoredProcOutboundGatewaySpec storedProcOutboundGateway(StoredProcExecutor storedProcExecutor) {
174+
return new JdbcStoredProcOutboundGatewaySpec(storedProcExecutor);
175+
}
176+
177+
/**
178+
* The factory to produce a {@link StoredProcExecutorSpec}.
179+
* @param dataSource the {@link DataSource} to build on
180+
* @return the {@link StoredProcExecutorSpec} instance
181+
*/
182+
public static StoredProcExecutorSpec storedProcExecutorSpec(DataSource dataSource) {
183+
return new StoredProcExecutorSpec(dataSource);
141184
}
142185

143186
private Jdbc() {

spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/dsl/JdbcInboundChannelAdapterSpec.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@
2727
* A {@link MessageSourceSpec} for a {@link JdbcInboundChannelAdapterSpec}.
2828
*
2929
* @author Jiandong Ma
30+
* @author Artem Bilan
3031
*
3132
* @since 7.0
3233
*/
33-
public class JdbcInboundChannelAdapterSpec extends MessageSourceSpec<JdbcInboundChannelAdapterSpec, JdbcPollingChannelAdapter> {
34+
public class JdbcInboundChannelAdapterSpec
35+
extends MessageSourceSpec<JdbcInboundChannelAdapterSpec, JdbcPollingChannelAdapter> {
3436

3537
protected JdbcInboundChannelAdapterSpec(JdbcOperations jdbcOperations, String selectQuery) {
3638
this.target = new JdbcPollingChannelAdapter(jdbcOperations, selectQuery);
@@ -43,7 +45,7 @@ protected JdbcInboundChannelAdapterSpec(JdbcOperations jdbcOperations, String se
4345
*/
4446
public JdbcInboundChannelAdapterSpec rowMapper(RowMapper<?> rowMapper) {
4547
this.target.setRowMapper(rowMapper);
46-
return _this();
48+
return this;
4749
}
4850

4951
/**
@@ -53,7 +55,7 @@ public JdbcInboundChannelAdapterSpec rowMapper(RowMapper<?> rowMapper) {
5355
*/
5456
public JdbcInboundChannelAdapterSpec updateSql(String updateSql) {
5557
this.target.setUpdateSql(updateSql);
56-
return _this();
58+
return this;
5759
}
5860

5961
/**
@@ -63,17 +65,19 @@ public JdbcInboundChannelAdapterSpec updateSql(String updateSql) {
6365
*/
6466
public JdbcInboundChannelAdapterSpec updatePerRow(boolean updatePerRow) {
6567
this.target.setUpdatePerRow(updatePerRow);
66-
return _this();
68+
return this;
6769
}
6870

6971
/**
7072
* @param sqlParameterSourceFactory the sqlParameterSourceFactory
7173
* @return the spec
7274
* @see JdbcPollingChannelAdapter#setUpdateSqlParameterSourceFactory(SqlParameterSourceFactory)
7375
*/
74-
public JdbcInboundChannelAdapterSpec updateSqlParameterSourceFactory(SqlParameterSourceFactory sqlParameterSourceFactory) {
76+
public JdbcInboundChannelAdapterSpec updateSqlParameterSourceFactory(
77+
SqlParameterSourceFactory sqlParameterSourceFactory) {
78+
7579
this.target.setUpdateSqlParameterSourceFactory(sqlParameterSourceFactory);
76-
return _this();
80+
return this;
7781
}
7882

7983
/**
@@ -83,7 +87,7 @@ public JdbcInboundChannelAdapterSpec updateSqlParameterSourceFactory(SqlParamete
8387
*/
8488
public JdbcInboundChannelAdapterSpec selectSqlParameterSource(SqlParameterSource sqlQueryParameterSource) {
8589
this.target.setSelectSqlParameterSource(sqlQueryParameterSource);
86-
return _this();
90+
return this;
8791
}
8892

8993
/**
@@ -93,7 +97,7 @@ public JdbcInboundChannelAdapterSpec selectSqlParameterSource(SqlParameterSource
9397
*/
9498
public JdbcInboundChannelAdapterSpec maxRows(int maxRows) {
9599
this.target.setMaxRows(maxRows);
96-
return _this();
100+
return this;
97101
}
98102

99103
}

spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/dsl/JdbcOutboundChannelAdapterSpec.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
* A {@link MessageHandlerSpec} for a {@link JdbcOutboundChannelAdapterSpec}.
2727
*
2828
* @author Jiandong Ma
29+
* @author Artem Bilan
2930
*
3031
* @since 7.0
3132
*/
32-
public class JdbcOutboundChannelAdapterSpec extends MessageHandlerSpec<JdbcOutboundChannelAdapterSpec, JdbcMessageHandler> {
33+
public class JdbcOutboundChannelAdapterSpec
34+
extends MessageHandlerSpec<JdbcOutboundChannelAdapterSpec, JdbcMessageHandler> {
3335

3436
protected JdbcOutboundChannelAdapterSpec(JdbcOperations jdbcOperations, String updateQuery) {
3537
this.target = new JdbcMessageHandler(jdbcOperations, updateQuery);
@@ -42,17 +44,19 @@ protected JdbcOutboundChannelAdapterSpec(JdbcOperations jdbcOperations, String u
4244
*/
4345
public JdbcOutboundChannelAdapterSpec keysGenerated(boolean keysGenerated) {
4446
this.target.setKeysGenerated(keysGenerated);
45-
return _this();
47+
return this;
4648
}
4749

4850
/**
4951
* @param sqlParameterSourceFactory the sqlParameterSourceFactory
5052
* @return the spec
5153
* @see JdbcMessageHandler#setSqlParameterSourceFactory(SqlParameterSourceFactory)
5254
*/
53-
public JdbcOutboundChannelAdapterSpec sqlParameterSourceFactory(SqlParameterSourceFactory sqlParameterSourceFactory) {
55+
public JdbcOutboundChannelAdapterSpec sqlParameterSourceFactory(
56+
SqlParameterSourceFactory sqlParameterSourceFactory) {
57+
5458
this.target.setSqlParameterSourceFactory(sqlParameterSourceFactory);
55-
return _this();
59+
return this;
5660
}
5761

5862
/**
@@ -62,17 +66,19 @@ public JdbcOutboundChannelAdapterSpec sqlParameterSourceFactory(SqlParameterSour
6266
*/
6367
public JdbcOutboundChannelAdapterSpec usePayloadAsParameterSource(boolean usePayloadAsParameterSource) {
6468
this.target.setUsePayloadAsParameterSource(usePayloadAsParameterSource);
65-
return _this();
69+
return this;
6670
}
6771

6872
/**
6973
* @param preparedStatementSetter the preparedStatementSetter
7074
* @return the spec
7175
* @see JdbcMessageHandler#setPreparedStatementSetter(MessagePreparedStatementSetter)
7276
*/
73-
public JdbcOutboundChannelAdapterSpec preparedStatementSetter(MessagePreparedStatementSetter preparedStatementSetter) {
77+
public JdbcOutboundChannelAdapterSpec preparedStatementSetter(
78+
MessagePreparedStatementSetter preparedStatementSetter) {
79+
7480
this.target.setPreparedStatementSetter(preparedStatementSetter);
75-
return _this();
81+
return this;
7682
}
7783

7884
}

spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/dsl/JdbcOutboundGatewaySpec.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* A {@link MessageHandlerSpec} for a {@link JdbcOutboundGatewaySpec}.
2828
*
2929
* @author Jiandong Ma
30+
* @author Artem Bilan
3031
*
3132
* @since 7.0
3233
*/
@@ -43,7 +44,7 @@ protected JdbcOutboundGatewaySpec(JdbcOperations jdbcOperations, String updateQu
4344
*/
4445
public JdbcOutboundGatewaySpec maxRows(Integer maxRows) {
4546
this.target.setMaxRows(maxRows);
46-
return _this();
47+
return this;
4748
}
4849

4950
/**
@@ -53,27 +54,31 @@ public JdbcOutboundGatewaySpec maxRows(Integer maxRows) {
5354
*/
5455
public JdbcOutboundGatewaySpec keysGenerated(boolean keysGenerated) {
5556
this.target.setKeysGenerated(keysGenerated);
56-
return _this();
57+
return this;
5758
}
5859

5960
/**
6061
* @param sqlParameterSourceFactory the sqlParameterSourceFactory
6162
* @return the spec
6263
* @see JdbcOutboundGateway#setRequestSqlParameterSourceFactory(SqlParameterSourceFactory)
6364
*/
64-
public JdbcOutboundGatewaySpec requestSqlParameterSourceFactory(SqlParameterSourceFactory sqlParameterSourceFactory) {
65+
public JdbcOutboundGatewaySpec requestSqlParameterSourceFactory(
66+
SqlParameterSourceFactory sqlParameterSourceFactory) {
67+
6568
this.target.setRequestSqlParameterSourceFactory(sqlParameterSourceFactory);
66-
return _this();
69+
return this;
6770
}
6871

6972
/**
7073
* @param preparedStatementSetter the preparedStatementSetter
7174
* @return the spec
7275
* @see JdbcOutboundGateway#setRequestPreparedStatementSetter(MessagePreparedStatementSetter)
7376
*/
74-
public JdbcOutboundGatewaySpec requestPreparedStatementSetter(MessagePreparedStatementSetter preparedStatementSetter) {
77+
public JdbcOutboundGatewaySpec requestPreparedStatementSetter(
78+
MessagePreparedStatementSetter preparedStatementSetter) {
79+
7580
this.target.setRequestPreparedStatementSetter(preparedStatementSetter);
76-
return _this();
81+
return this;
7782
}
7883

7984
/**
@@ -83,7 +88,7 @@ public JdbcOutboundGatewaySpec requestPreparedStatementSetter(MessagePreparedSta
8388
*/
8489
public JdbcOutboundGatewaySpec replySqlParameterSourceFactory(SqlParameterSourceFactory sqlParameterSourceFactory) {
8590
this.target.setReplySqlParameterSourceFactory(sqlParameterSourceFactory);
86-
return _this();
91+
return this;
8792
}
8893

8994
/**
@@ -93,7 +98,7 @@ public JdbcOutboundGatewaySpec replySqlParameterSourceFactory(SqlParameterSource
9398
*/
9499
public JdbcOutboundGatewaySpec rowMapper(RowMapper<?> rowMapper) {
95100
this.target.setRowMapper(rowMapper);
96-
return _this();
101+
return this;
97102
}
98103

99104
}

spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/dsl/JdbcStoredProcInboundChannelAdapterSpec.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.Map;
2121
import java.util.function.Consumer;
2222

23+
import org.jspecify.annotations.Nullable;
24+
2325
import org.springframework.integration.dsl.ComponentsRegistration;
2426
import org.springframework.integration.dsl.MessageSourceSpec;
2527
import org.springframework.integration.jdbc.StoredProcExecutor;
@@ -30,6 +32,7 @@
3032
* A {@link MessageSourceSpec} for a {@link JdbcStoredProcInboundChannelAdapterSpec}.
3133
*
3234
* @author Jiandong Ma
35+
* @author Artem Bilan
3336
*
3437
* @since 7.0
3538
*/
@@ -39,11 +42,16 @@ public class JdbcStoredProcInboundChannelAdapterSpec
3942

4043
private final StoredProcExecutor storedProcExecutor;
4144

42-
private final StoredProcExecutorConfigurer storedProcExecutorConfigurer;
45+
private @Nullable StoredProcExecutorSpec storedProcExecutorSpec;
46+
47+
protected JdbcStoredProcInboundChannelAdapterSpec(StoredProcExecutorSpec storedProcExecutorSpec) {
48+
this(storedProcExecutorSpec.getObject());
49+
this.storedProcExecutorSpec = storedProcExecutorSpec;
50+
}
4351

4452
protected JdbcStoredProcInboundChannelAdapterSpec(StoredProcExecutor storedProcExecutor) {
4553
this.storedProcExecutor = storedProcExecutor;
46-
this.storedProcExecutorConfigurer = new StoredProcExecutorConfigurer(this.storedProcExecutor);
54+
this.storedProcExecutorSpec = null;
4755
this.target = new StoredProcPollingChannelAdapter(this.storedProcExecutor);
4856
}
4957

@@ -52,10 +60,14 @@ protected JdbcStoredProcInboundChannelAdapterSpec(StoredProcExecutor storedProcE
5260
* @param configurer the configurer.
5361
* @return the spec
5462
*/
55-
public JdbcStoredProcInboundChannelAdapterSpec configurerStoredProcExecutor(Consumer<StoredProcExecutorConfigurer> configurer) {
63+
public JdbcStoredProcInboundChannelAdapterSpec configurerStoredProcExecutor(
64+
Consumer<StoredProcExecutorSpec> configurer) {
65+
5666
Assert.notNull(configurer, "'configurer' must not be null");
57-
configurer.accept(this.storedProcExecutorConfigurer);
58-
return _this();
67+
Assert.notNull(this.storedProcExecutorSpec,
68+
"The externally provided 'StoredProcExecutor' cannot be mutated in this spec");
69+
configurer.accept(this.storedProcExecutorSpec);
70+
return this;
5971
}
6072

6173
/**
@@ -65,11 +77,12 @@ public JdbcStoredProcInboundChannelAdapterSpec configurerStoredProcExecutor(Cons
6577
*/
6678
public JdbcStoredProcInboundChannelAdapterSpec expectSingleResult(boolean expectSingleResult) {
6779
this.target.setExpectSingleResult(expectSingleResult);
68-
return _this();
80+
return this;
6981
}
7082

7183
@Override
7284
public Map<Object, String> getComponentsToRegister() {
7385
return Collections.singletonMap(this.storedProcExecutor, null);
7486
}
87+
7588
}

0 commit comments

Comments
 (0)