From 94eee6a32ae249da0d8fdaf7f6459c2909b26daa Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 7 Aug 2020 13:00:35 +0200 Subject: [PATCH] Provide access to AbstractRoutingDataSource's resolved target DataSources Closes gh-25544 --- .../lookup/AbstractRoutingDataSource.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.java index b89bb1abeb..88669594b1 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.springframework.jdbc.datasource.lookup; import java.sql.Connection; import java.sql.SQLException; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -164,6 +165,29 @@ public abstract class AbstractRoutingDataSource extends AbstractDataSource imple } } + /** + * Return the resolved target DataSources that this router manages. + * @return an unmodifiable map of resolved lookup keys and DataSources + * @throws IllegalStateException if the target DataSources are not resolved yet + * @since 5.2.9 + * @see #setTargetDataSources + */ + public Map getResolvedDataSources() { + Assert.state(this.resolvedDataSources != null, "DataSources not resolved yet - call afterPropertiesSet"); + return Collections.unmodifiableMap(this.resolvedDataSources); + } + + /** + * Return the resolved default target DataSource, if any. + * @return the default DataSource, or {@code null} if none or not resolved yet + * @since 5.2.9 + * @see #setDefaultTargetDataSource + */ + @Nullable + public DataSource getResolvedDefaultDataSource() { + return this.resolvedDefaultDataSource; + } + @Override public Connection getConnection() throws SQLException {