org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource

DynamicDataSource-CSDN博客

/** 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.* You may obtain a copy of the License at**      https://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.springframework.jdbc.datasource.lookup;import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Map;import javax.sql.DataSource;import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.datasource.AbstractDataSource;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;/*** Abstract {@link javax.sql.DataSource} implementation that routes {@link #getConnection()}* calls to one of various target DataSources based on a lookup key. The latter is usually* (but not necessarily) determined through some thread-bound transaction context.** @author Juergen Hoeller* @since 2.0.1* @see #setTargetDataSources* @see #setDefaultTargetDataSource* @see #determineCurrentLookupKey()*/
public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean {@Nullableprivate Map<Object, Object> targetDataSources;@Nullableprivate Object defaultTargetDataSource;private boolean lenientFallback = true;private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup();@Nullableprivate Map<Object, DataSource> resolvedDataSources;@Nullableprivate DataSource resolvedDefaultDataSource;/*** Specify the map of target DataSources, with the lookup key as key.* The mapped value can either be a corresponding {@link javax.sql.DataSource}* instance or a data source name String (to be resolved via a* {@link #setDataSourceLookup DataSourceLookup}).* <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()}.*/public void setTargetDataSources(Map<Object, Object> targetDataSources) {this.targetDataSources = targetDataSources;}/*** Specify the default target DataSource, if any.* <p>The mapped value can either be a corresponding {@link javax.sql.DataSource}* instance or a data source name String (to be resolved via a* {@link #setDataSourceLookup DataSourceLookup}).* <p>This DataSource will be used as target if none of the keyed* {@link #setTargetDataSources targetDataSources} match the* {@link #determineCurrentLookupKey()} current lookup key.*/public void setDefaultTargetDataSource(Object defaultTargetDataSource) {this.defaultTargetDataSource = defaultTargetDataSource;}/*** Specify whether to apply a lenient fallback to the default DataSource* if no specific DataSource could be found for the current lookup key.* <p>Default is "true", accepting lookup keys without a corresponding entry* in the target DataSource map - simply falling back to the default DataSource* in that case.* <p>Switch this flag to "false" if you would prefer the fallback to only apply* if the lookup key was {@code null}. Lookup keys without a DataSource* entry will then lead to an IllegalStateException.* @see #setTargetDataSources* @see #setDefaultTargetDataSource* @see #determineCurrentLookupKey()*/public void setLenientFallback(boolean lenientFallback) {this.lenientFallback = lenientFallback;}/*** Set the DataSourceLookup implementation to use for resolving data source* name Strings in the {@link #setTargetDataSources targetDataSources} map.* <p>Default is a {@link JndiDataSourceLookup}, allowing the JNDI names* of application server DataSources to be specified directly.*/public void setDataSourceLookup(@Nullable DataSourceLookup dataSourceLookup) {this.dataSourceLookup = (dataSourceLookup != null ? dataSourceLookup : new JndiDataSourceLookup());}@Overridepublic void afterPropertiesSet() {if (this.targetDataSources == null) {throw new IllegalArgumentException("Property 'targetDataSources' is required");}this.resolvedDataSources = CollectionUtils.newHashMap(this.targetDataSources.size());this.targetDataSources.forEach((key, value) -> {Object lookupKey = resolveSpecifiedLookupKey(key);DataSource dataSource = resolveSpecifiedDataSource(value);this.resolvedDataSources.put(lookupKey, dataSource);});if (this.defaultTargetDataSource != null) {this.resolvedDefaultDataSource = resolveSpecifiedDataSource(this.defaultTargetDataSource);}}/*** Resolve the given lookup key object, as specified in the* {@link #setTargetDataSources targetDataSources} map, into* the actual lookup key to be used for matching with the* {@link #determineCurrentLookupKey() current lookup key}.* <p>The default implementation simply returns the given key as-is.* @param lookupKey the lookup key object as specified by the user* @return the lookup key as needed for matching*/protected Object resolveSpecifiedLookupKey(Object lookupKey) {return lookupKey;}/*** Resolve the specified data source object into a DataSource instance.* <p>The default implementation handles DataSource instances and data source* names (to be resolved via a {@link #setDataSourceLookup DataSourceLookup}).* @param dataSource the data source value object as specified in the* {@link #setTargetDataSources targetDataSources} map* @return the resolved DataSource (never {@code null})* @throws IllegalArgumentException in case of an unsupported value type*/protected DataSource resolveSpecifiedDataSource(Object dataSource) throws IllegalArgumentException {if (dataSource instanceof DataSource) {return (DataSource) dataSource;}else if (dataSource instanceof String) {return this.dataSourceLookup.getDataSource((String) dataSource);}else {throw new IllegalArgumentException("Illegal data source value - only [javax.sql.DataSource] and String supported: " + dataSource);}}/*** 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<Object, DataSource> 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*/@Nullablepublic DataSource getResolvedDefaultDataSource() {return this.resolvedDefaultDataSource;}@Overridepublic Connection getConnection() throws SQLException {return determineTargetDataSource().getConnection();}@Overridepublic Connection getConnection(String username, String password) throws SQLException {return determineTargetDataSource().getConnection(username, password);}@Override@SuppressWarnings("unchecked")public <T> T unwrap(Class<T> iface) throws SQLException {if (iface.isInstance(this)) {return (T) this;}return determineTargetDataSource().unwrap(iface);}@Overridepublic boolean isWrapperFor(Class<?> iface) throws SQLException {return (iface.isInstance(this) || determineTargetDataSource().isWrapperFor(iface));}/*** Retrieve the current target DataSource. Determines the* {@link #determineCurrentLookupKey() current lookup key}, performs* a lookup in the {@link #setTargetDataSources targetDataSources} map,* falls back to the specified* {@link #setDefaultTargetDataSource default target DataSource} if necessary.* @see #determineCurrentLookupKey()*/protected DataSource determineTargetDataSource() {Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");Object lookupKey = determineCurrentLookupKey();DataSource dataSource = this.resolvedDataSources.get(lookupKey);if (dataSource == null && (this.lenientFallback || lookupKey == null)) {dataSource = this.resolvedDefaultDataSource;}if (dataSource == null) {throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");}return dataSource;}/*** Determine the current lookup key. This will typically be* implemented to check a thread-bound transaction context.* <p>Allows for arbitrary keys. The returned key needs* to match the stored lookup key type, as resolved by the* {@link #resolveSpecifiedLookupKey} method.*/@Nullableprotected abstract Object determineCurrentLookupKey();}

==分析,启动==

==分析,网页请求==

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/192210.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

鸿蒙工具DevEco Studio调试Build task failed. Open the Run window to view details.

DevEco Studio 预览代码时候出现的问题 1.进入设置 2.打开设置&#xff0c;构建&#xff0c;执行&#xff0c;部署下面的Hvigor&#xff0c; 把构建守护进程关掉就行。 然后重启启动一下就好了

Redis SDS 源码

底层数据结构的好处&#xff1a; 杜绝缓冲区溢出。减少修改字符串长度时所需的内存重分配次数。二进制安全。兼容部分C字符串函数。 常用命令&#xff1a; set key value、get key 等 应用场景&#xff1a;共享 session、分布式锁&#xff0c;计数器、限流。 1、给char*定义…

【重点】【哈希】128.最长连续序列

题目 思路&#xff1a;https://leetcode.cn/problems/longest-consecutive-sequence/solutions/2362995/javapython3cha-xi-biao-ding-wei-mei-ge-l-xk4c/?envTypestudy-plan-v2&envIdtop-100-liked class Solution {public int longestConsecutive(int[] nums) {Set<…

<JavaEE> volatile关键字 -- 保证内存可见性、禁止指令重排序

目录 一、内存可见性 1.1 Java内存模型(JMM) 1.2 内存可见性演示 二、指令重排序 三、关键字 volatile 一、内存可见性 1.1 Java内存模型(JMM) 1&#xff09;什么是Java内存模型&#xff08;JMM&#xff09;&#xff1f;Java内存模型即Java Memory Model&#xff0c;简…

如何使用注解实现接口的幂等性校验

如何使用注解实现接口的幂等性校验 背景什么是幂等性为什么要实现幂等性校验如何实现接口的幂等性校验1. 数据库唯一主键2. 数据库乐观锁3. 防重 Token 令牌4. redis 如何将这几种方式都组装到一起结语 背景 最近在小组同学卷的受不了的情况下&#xff0c;我决定换一个方向卷去…

基于景区智慧灯杆、智能指路牌基础设施的景区建设应用

智慧景区是指运用现代信息技术手段&#xff0c;将景区内的资源、服务、管理等进行数字化、网络化和智能化整合&#xff0c;打造出高效便捷、安全舒适、互动体验和可持续发展的景区。智慧景区可以从以下几个方面进行体现&#xff1a; 智慧导览&#xff1a;通过使用智能化的导览…

二分查找:LeetCode2035:将数组分成两个数组并最小化数组和的差

本文涉及的基础知识点 二分查找算法合集 作者推荐 动态规划LeetCode2552&#xff1a;优化了6版的1324模式 题目 给你一个长度为 2 * n 的整数数组。你需要将 nums 分成 两个 长度为 n 的数组&#xff0c;分别求出两个数组的和&#xff0c;并 最小化 两个数组和之 差的绝对…

Screenshot To Code

序言 对于GPT-4我只是一个门外汉&#xff0c;至于我为什么要了解screenshot to code&#xff0c;只是因为我想知道&#xff0c;在我不懂前端设计的情况下&#xff0c;能不能通过一些工具辅助自己做一些简单的前端界面设计。如果你想通过此文深刻了解GPT-4或者该开源项目&#…

【python】保存excel

正确安装了pandas和openpyxl库。 可以通过在命令行中输入以下命令来检查&#xff1a; pip show pandas pip show openpyxl 可以使用pip安装 pip install pandas pip install openpyxl#更新 pip install --upgrade pandas pip install --upgrade openpyxl 保存excel …

pygame实现贪吃蛇小游戏

import pygame import random# 游戏初始化 pygame.init()# 游戏窗口设置 win_width, win_height 800, 600 window pygame.display.set_mode((win_width, win_height)) pygame.display.set_caption("Snake Game")# 颜色设置 WHITE (255, 255, 255) BLACK (0, 0, 0…

如何确定短线的买入卖出时机?

短线投资制胜的一个关键能力&#xff0c;就是精准地找到买入卖出时机。那么&#xff0c;怎么样才能获得这种关键能力呢&#xff1f; 在这节课里&#xff0c;我们将给大家梳理一下常见的短线买入卖出时机&#xff0c;并通过案例讲解帮助大家理解。话不多说&#xff0c;赶紧进入主…

rdf-file:SM2加解密

一&#xff1a;SM2简介 SM2是中国密码学算法标准中的一种非对称加密算法&#xff08;包括公钥和私钥&#xff09;。SM2主要用于数字签名、密钥交换和加密解密等密码学。 生成秘钥&#xff1a;用于生成一对公钥和私钥。公钥&#xff1a;用于加密数据和验证数字签名。私钥&…

javaSE学习-1-数据类型与运算符

目录 字面常量 数据类型 int Long short Byte float double char boolean 类型转换 强转 自动类型转换(隐式) 字符串类型 字符串和整形数字之间进行转换 字面常量 比如 System.Out.println("Hello World") &#xff1b; 语句&#xff0c;不论程序何时…

代码随想录第二十二天(一刷C语言)|组合总数电话号码的字母组合

创作目的&#xff1a;为了方便自己后续复习重点&#xff0c;以及养成写博客的习惯。 一、组合总数 思路&#xff1a;参考carl文档和视频 1、需要一维数组path来存放符合条件的结果&#xff0c;二维数组result来存放结果集。 2、targetSum 目标和&#xff0c;也就是题目中的…

【Python】Python给工作减负-读Excel文件生成xml文件

目录 ​前言 正文 1.Python基础学习 2.Python读取Excel表格 2.1安装xlrd模块 2.2使用介绍 2.2.1常用单元格中的数据类型 2.2.2 导入模块 2.2.3打开Excel文件读取数据 2.2.4常用函数 2.2.5代码测试 2.2.6 Python操作Excel官方网址 3.Python创建xml文件 3.1 xml语法…

自定义类型:结构体(自引用、内存对齐、位段(位域))

目录 一. 结构体类型的声明和定义 1.1结构体相关概念 1.11结构的声明 1.12成员列表 1.2定义结构体类型变量的方法 1.21先声明结构体类型再定义变量名 ​​​​1.22在声明类型的同时定义变量 1.23直接定义结构类型变量 二、结构体变量的创建、初始化​和访问 2.1结构体…

[二分查找]LeetCode2009 :使数组连续的最少操作数

本文涉及的基础知识点 二分查找算法合集 作者推荐 动态规划LeetCode2552&#xff1a;优化了6版的1324模式 题目 给你一个整数数组 nums 。每一次操作中&#xff0c;你可以将 nums 中 任意 一个元素替换成 任意 整数。 如果 nums 满足以下条件&#xff0c;那么它是 连续的 …

Java Web——动态Web开发核心-Servlet

1. 官方文档 官方文档地址&#xff1a;Overview (Servlet 4.0 API Documentation - Apache Tomcat 9.0.83) servlet 与 Tomcat 的关系&#xff1a;Tomcat 支持 Servlet Tomcat 是一个开源的 Java 服务器&#xff0c;它主要用来提供 Web 服务&#xff0c;包括 HTTP 请求和响应…

EasyExcel写入多个sheet

直接上代码&#xff1a; public static void main(String[] args) {// 设置excel工作簿ExcelWriter excelWriter EasyExcel.write("F:\\excel\\a.xls").build();List<User> userList new ArrayList<>();userList.add(new User("lisi", "…

初始数据结构(加深对旋转的理解)

力扣&#xff08;LeetCode&#xff09;官网 - 全球极客挚爱的技术成长平台备战技术面试&#xff1f;力扣提供海量技术面试资源&#xff0c;帮助你高效提升编程技能&#xff0c;轻松拿下世界 IT 名企 Dream Offer。https://leetcode.cn/problems/rotate-array/submissions/ 与字…