From 4044f4c30ff9b04b3860a1661695a750c650912e Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 3 Sep 2020 23:56:36 +0200 Subject: [PATCH] Polishing --- .../DelegatingConnectionFactory.java | 30 ++++++++--------- ...ransactionAwareConnectionFactoryProxy.java | 33 +++++++------------ .../init/ConnectionFactoryInitializer.java | 19 +++++------ .../connection/init/DatabasePopulator.java | 4 +-- .../AbstractRoutingConnectionFactory.java | 6 ---- .../BeanFactoryConnectionFactoryLookup.java | 1 + .../lookup/ConnectionFactoryLookup.java | 3 +- .../lookup/MapConnectionFactoryLookup.java | 6 ++-- .../lookup/SingleConnectionFactoryLookup.java | 1 + 9 files changed, 43 insertions(+), 60 deletions(-) diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/DelegatingConnectionFactory.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/DelegatingConnectionFactory.java index 69615f5b4d..e3ba0c523b 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/DelegatingConnectionFactory.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/DelegatingConnectionFactory.java @@ -34,43 +34,43 @@ import org.springframework.util.Assert; * * @author Mark Paluch * @since 5.3 - * @see #create + * @see #create() */ public class DelegatingConnectionFactory implements ConnectionFactory, Wrapped { private final ConnectionFactory targetConnectionFactory; + /** + * Create a new DelegatingConnectionFactory. + * @param targetConnectionFactory the target ConnectionFactory + */ public DelegatingConnectionFactory(ConnectionFactory targetConnectionFactory) { Assert.notNull(targetConnectionFactory, "ConnectionFactory must not be null"); this.targetConnectionFactory = targetConnectionFactory; } - @Override - public Mono create() { - return Mono.from(this.targetConnectionFactory.create()); - } - + /** + * Return the target ConnectionFactory that this ConnectionFactory delegates to. + */ public ConnectionFactory getTargetConnectionFactory() { return this.targetConnectionFactory; } + @Override + public Mono create() { + return Mono.from(this.targetConnectionFactory.create()); + } + @Override public ConnectionFactoryMetadata getMetadata() { - return obtainTargetConnectionFactory().getMetadata(); + return this.targetConnectionFactory.getMetadata(); } @Override public ConnectionFactory unwrap() { - return obtainTargetConnectionFactory(); - } - - /** - * Obtain the target {@link ConnectionFactory} for actual use (never {@code null}). - */ - protected ConnectionFactory obtainTargetConnectionFactory() { - return getTargetConnectionFactory(); + return this.targetConnectionFactory; } } diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/TransactionAwareConnectionFactoryProxy.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/TransactionAwareConnectionFactoryProxy.java index 406b2662cf..edc61bfb84 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/TransactionAwareConnectionFactoryProxy.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/TransactionAwareConnectionFactoryProxy.java @@ -58,9 +58,8 @@ import org.springframework.util.ReflectionUtils; * {@link ConnectionFactory}, avoiding the need to define such a proxy in the first place. * *

NOTE: This {@link ConnectionFactory} proxy needs to return wrapped - * {@link Connection}s (which implement the {@link ConnectionProxy} interface) in order - * to handle close calls properly. Use {@link Wrapped#unwrap()} to retrieve - * the native R2DBC Connection. + * {@link Connection}s in order to handle close calls properly. + * Use {@link Wrapped#unwrap()} to retrieve the native R2DBC Connection. * * @author Mark Paluch * @author Christoph Strobl @@ -74,9 +73,7 @@ public class TransactionAwareConnectionFactoryProxy extends DelegatingConnection /** * Create a new {@link TransactionAwareConnectionFactoryProxy}. - * - * @param targetConnectionFactory the target {@link ConnectionFactory}. - * @throws IllegalArgumentException if given {@link ConnectionFactory} is {@code null}. + * @param targetConnectionFactory the target {@link ConnectionFactory} */ public TransactionAwareConnectionFactoryProxy(ConnectionFactory targetConnectionFactory) { super(targetConnectionFactory); @@ -84,25 +81,20 @@ public class TransactionAwareConnectionFactoryProxy extends DelegatingConnection /** - * Delegates to {@link ConnectionFactoryUtils} for automatically participating in Spring-managed transactions. - *

- * The returned {@link ConnectionFactory} handle implements the {@link ConnectionProxy} interface, allowing to - * retrieve the underlying target {@link Connection}. - * + * Delegates to {@link ConnectionFactoryUtils} for automatically participating + * in Spring-managed transactions. * @return a transactional {@link Connection} if any, a new one else. * @see ConnectionFactoryUtils#doGetConnection - * @see ConnectionProxy#getTargetConnection */ @Override public Mono create() { - return getTransactionAwareConnectionProxy(obtainTargetConnectionFactory()); + return getTransactionAwareConnectionProxy(getTargetConnectionFactory()); } /** - * Wraps the given {@link Connection} with a proxy that delegates every method call to it but delegates - * {@code close()} calls to {@link ConnectionFactoryUtils}. - * - * @param targetConnectionFactory the {@link ConnectionFactory} that the {@link Connection} came from. + * Wraps the given {@link Connection} with a proxy that delegates every method call + * to it but delegates {@code close()} calls to {@link ConnectionFactoryUtils}. + * @param targetConnectionFactory the {@link ConnectionFactory} that the {@link Connection} came from * @return the wrapped {@link Connection}. * @see Connection#close() * @see ConnectionFactoryUtils#doReleaseConnection @@ -113,16 +105,15 @@ public class TransactionAwareConnectionFactoryProxy extends DelegatingConnection } private static Connection proxyConnection(Connection connection, ConnectionFactory targetConnectionFactory) { - return (Connection) Proxy.newProxyInstance(TransactionAwareConnectionFactoryProxy.class.getClassLoader(), - new Class[] { Connection.class, Wrapped.class }, + new Class[] {Connection.class, Wrapped.class}, new TransactionAwareInvocationHandler(connection, targetConnectionFactory)); } /** - * Invocation handler that delegates close calls on R2DBC Connections to {@link ConnectionFactoryUtils} for being - * aware of context-bound transactions. + * Invocation handler that delegates close calls on R2DBC Connections to + * {@link ConnectionFactoryUtils} for being aware of context-bound transactions. */ private static class TransactionAwareInvocationHandler implements InvocationHandler { diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ConnectionFactoryInitializer.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ConnectionFactoryInitializer.java index 8fa8fc04d2..aef7f60bcb 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ConnectionFactoryInitializer.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/ConnectionFactoryInitializer.java @@ -47,11 +47,9 @@ public class ConnectionFactoryInitializer implements InitializingBean, Disposabl /** - * The {@link ConnectionFactory} for the database to populate when this component is initialized and to clean up when - * this component is shut down. - *

- * This property is mandatory with no default provided. - * + * The {@link ConnectionFactory} for the database to populate when this + * component is initialized and to clean up when this component is shut down. + *

This property is mandatory with no default provided. * @param connectionFactory the R2DBC {@link ConnectionFactory}. */ public void setConnectionFactory(ConnectionFactory connectionFactory) { @@ -60,7 +58,6 @@ public class ConnectionFactoryInitializer implements InitializingBean, Disposabl /** * Set the {@link DatabasePopulator} to execute during the bean initialization phase. - * * @param databasePopulator the {@link DatabasePopulator} to use during initialization * @see #setDatabaseCleaner */ @@ -80,16 +77,16 @@ public class ConnectionFactoryInitializer implements InitializingBean, Disposabl } /** - * Flag to explicitly enable or disable the {@link #setDatabasePopulator database populator} and - * {@link #setDatabaseCleaner database cleaner}. - * - * @param enabled {@code true} if the database populator and database cleaner should be called on startup and - * shutdown, respectively + * Flag to explicitly enable or disable the {@link #setDatabasePopulator database populator} + * and {@link #setDatabaseCleaner database cleaner}. + * @param enabled {@code true} if the database populator and database cleaner + * should be called on startup and shutdown, respectively */ public void setEnabled(boolean enabled) { this.enabled = enabled; } + /** * Use the {@link #setDatabasePopulator database populator} to set up the database. */ diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/DatabasePopulator.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/DatabasePopulator.java index 521aa36d8e..b2c63133a0 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/DatabasePopulator.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/init/DatabasePopulator.java @@ -38,7 +38,6 @@ public interface DatabasePopulator { /** * Populate, initialize, or clean up the database using the * provided R2DBC {@link Connection}. - * * @param connection the R2DBC connection to use to populate the db; * already configured and ready to use, must not be {@code null} * @return {@link Mono} that initiates script execution and is @@ -53,8 +52,7 @@ public interface DatabasePopulator { * @return {@link Mono} that initiates {@link DatabasePopulator#populate(Connection)} * and is notified upon completion */ - default Mono populate(ConnectionFactory connectionFactory) - throws DataAccessException { + default Mono populate(ConnectionFactory connectionFactory) throws DataAccessException { Assert.notNull(connectionFactory, "ConnectionFactory must not be null"); return Mono.usingWhen(ConnectionFactoryUtils.getConnection(connectionFactory), // this::populate, // diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/AbstractRoutingConnectionFactory.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/AbstractRoutingConnectionFactory.java index ace6c52976..821b694523 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/AbstractRoutingConnectionFactory.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/AbstractRoutingConnectionFactory.java @@ -76,7 +76,6 @@ public abstract class AbstractRoutingConnectionFactory implements ConnectionFact * with the lookup key as key. The mapped value can either be a corresponding * {@link ConnectionFactory} instance or a connection factory name String (to be * resolved via a {@link #setConnectionFactoryLookup ConnectionFactoryLookup}). - * *

The key can be of arbitrary type; this class implements the generic lookup * process only. The concrete key representation will be handled by * {@link #resolveSpecifiedLookupKey(Object)} and {@link #determineCurrentLookupKey()}. @@ -87,11 +86,9 @@ public abstract class AbstractRoutingConnectionFactory implements ConnectionFact /** * Specify the default target {@link ConnectionFactory}, if any. - * *

The mapped value can either be a corresponding {@link ConnectionFactory} * instance or a connection factory name {@link String} (to be resolved via a * {@link #setConnectionFactoryLookup ConnectionFactoryLookup}). - * *

This {@link ConnectionFactory} will be used as target if none of the keyed * {@link #setTargetConnectionFactories targetConnectionFactories} match the * {@link #determineCurrentLookupKey() current lookup key}. @@ -103,11 +100,9 @@ public abstract class AbstractRoutingConnectionFactory implements ConnectionFact /** * Specify whether to apply a lenient fallback to the default {@link ConnectionFactory} * if no specific {@link ConnectionFactory} could be found for the current lookup key. - * *

Default is {@code true}, accepting lookup keys without a corresponding entry * in the target {@link ConnectionFactory} map - simply falling back to the default * {@link ConnectionFactory} in that case. - * *

Switch this flag to {@code false} if you would prefer the fallback to only * apply when no lookup key was emitted. Lookup keys without a {@link ConnectionFactory} * entry will then lead to an {@link IllegalStateException}. @@ -234,7 +229,6 @@ public abstract class AbstractRoutingConnectionFactory implements ConnectionFact * Determine the current lookup key. This will typically be implemented to check a * subscriber context. Allows for arbitrary keys. The returned key needs to match the * stored lookup key type, as resolved by the {@link #resolveSpecifiedLookupKey} method. - * * @return {@link Mono} emitting the lookup key. May complete without emitting a value * if no lookup key available */ diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/BeanFactoryConnectionFactoryLookup.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/BeanFactoryConnectionFactoryLookup.java index 1e19d3eb0b..5359fec112 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/BeanFactoryConnectionFactoryLookup.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/BeanFactoryConnectionFactoryLookup.java @@ -72,6 +72,7 @@ public class BeanFactoryConnectionFactoryLookup implements ConnectionFactoryLook @Override public ConnectionFactory getConnectionFactory(String connectionFactoryName) throws ConnectionFactoryLookupFailureException { + Assert.state(this.beanFactory != null, "BeanFactory is required"); try { return this.beanFactory.getBean(connectionFactoryName, ConnectionFactory.class); diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/ConnectionFactoryLookup.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/ConnectionFactoryLookup.java index 70d5c99f12..5352d5207e 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/ConnectionFactoryLookup.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/ConnectionFactoryLookup.java @@ -33,6 +33,7 @@ public interface ConnectionFactoryLookup { * @return the {@link ConnectionFactory} (never {@code null}) * @throws ConnectionFactoryLookupFailureException if the lookup failed */ - ConnectionFactory getConnectionFactory(String connectionFactoryName) throws ConnectionFactoryLookupFailureException; + ConnectionFactory getConnectionFactory(String connectionFactoryName) + throws ConnectionFactoryLookupFailureException; } diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/MapConnectionFactoryLookup.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/MapConnectionFactoryLookup.java index 0e6f0f12f5..1598d1da4c 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/MapConnectionFactoryLookup.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/MapConnectionFactoryLookup.java @@ -86,9 +86,8 @@ public class MapConnectionFactoryLookup implements ConnectionFactoryLookup { } /** - * Add the supplied {@link ConnectionFactory} to the map of {@link ConnectionFactory ConnectionFactorys} maintained by - * this object. - * + * Add the supplied {@link ConnectionFactory} to the map of + * {@link ConnectionFactory ConnectionFactory} instances maintained by this object. * @param connectionFactoryName the name under which the supplied {@link ConnectionFactory} is to be added * @param connectionFactory the {@link ConnectionFactory} to be so added */ @@ -101,6 +100,7 @@ public class MapConnectionFactoryLookup implements ConnectionFactoryLookup { @Override public ConnectionFactory getConnectionFactory(String connectionFactoryName) throws ConnectionFactoryLookupFailureException { + Assert.notNull(connectionFactoryName, "ConnectionFactory name must not be null"); return this.connectionFactories.computeIfAbsent(connectionFactoryName, key -> { throw new ConnectionFactoryLookupFailureException( diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/SingleConnectionFactoryLookup.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/SingleConnectionFactoryLookup.java index afda412ce8..58919891d7 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/SingleConnectionFactoryLookup.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/lookup/SingleConnectionFactoryLookup.java @@ -46,6 +46,7 @@ public class SingleConnectionFactoryLookup implements ConnectionFactoryLookup { @Override public ConnectionFactory getConnectionFactory(String connectionFactoryName) throws ConnectionFactoryLookupFailureException { + return this.connectionFactory; }