Polishing

This commit is contained in:
Juergen Hoeller
2020-09-03 23:56:36 +02:00
parent a2bb59f1b8
commit 4044f4c30f
9 changed files with 43 additions and 60 deletions

View File

@@ -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<ConnectionFactory> {
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<? extends Connection> 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<? extends Connection> 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;
}
}

View File

@@ -58,9 +58,8 @@ import org.springframework.util.ReflectionUtils;
* {@link ConnectionFactory}, avoiding the need to define such a proxy in the first place.
*
* <p><b>NOTE:</b> 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.
* <p>
* 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<Connection> 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 {

View File

@@ -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.
* <p/>
* 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.
* <p>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.
*/

View File

@@ -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<Void> populate(ConnectionFactory connectionFactory)
throws DataAccessException {
default Mono<Void> populate(ConnectionFactory connectionFactory) throws DataAccessException {
Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
return Mono.usingWhen(ConnectionFactoryUtils.getConnection(connectionFactory), //
this::populate, //

View File

@@ -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}).
*
* <p>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.
*
* <p>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}).
*
* <p>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.
*
* <p>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.
*
* <p>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
*/

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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(

View File

@@ -46,6 +46,7 @@ public class SingleConnectionFactoryLookup implements ConnectionFactoryLookup {
@Override
public ConnectionFactory getConnectionFactory(String connectionFactoryName)
throws ConnectionFactoryLookupFailureException {
return this.connectionFactory;
}