{"id":2021,"date":"2017-10-13T17:06:44","date_gmt":"2017-10-13T12:18:16","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2021"},"modified":"2017-10-13T17:10:31","modified_gmt":"2017-10-13T09:10:31","slug":"%e9%9d%a0%e6%8e%92%e7%85%a7%e7%89%87%e5%92%8c%e6%8b%8d%e8%a7%86%e9%a2%91%e5%81%9a%e5%88%b0%e6%9c%88%e5%85%a510%e4%b8%87-20","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2021","title":{"rendered":"Spring\u52a8\u6001\u8def\u7531AbstractRoutingDataSource(\u6570\u636e\u6e90\u52a8\u6001\u5207\u6362)\u6559\u7a0b"},"content":{"rendered":"<p>\n\t\u6570\u636e\u6e90\u52a8\u6001\u5207\u6362\u4e5f\u4e0d\u662f\u4ec0\u4e48\u65b0\u6280\u672f\uff0c\u963f\u91cc\u5728\u65e9\u671f\u90fd\u6709\u591a\u9686\u5927\u795e\u5b9e\u73b0\u4e86\u3002\u4f46\u662f\u6211\u4eec\u4eca\u5929\u8981\u8bb2\u7684\u662f <a href=\"https:\/\/www.xttblog.com\/?tag=Spring\" name=\"Spring\" target=\"_blank\" title=\"Spring\">Spring<\/a> \u5bf9\u6570\u636e\u6e90\u8def\u7531\u7684\u5b9e\u73b0\u3002\n<\/p>\n<p>\n\t\u5927\u9879\u76ee\u7684\u591a\u4e2a\u6570\u636e\u5e93\u52a8\u6001\u5207\u6362\u5df2\u662f\u67b6\u6784\u5e08\u8003\u8651\u7684\u8d8b\u52bf\u3002\u6570\u636e\u6e90\u52a8\u6001\u5207\u6362\u5f80\u5f80\u80fd\u7ed9\u6211\u5e26\u6765\u5f88\u591a\u597d\u5904\uff0c\u6bd4\u5982\u6839\u636e\u591a\u8bed\u8a00\u5b9e\u73b0\u6570\u636e\u5e93\u52a8\u6001\u5207\u6362\uff0c\u8bfb\u5199\u5206\u79bb\u6570\u636e\u5e93\u52a8\u6001\u5207\u6362\uff0c\u707e\u5907\u5904\u7406\u7b49\u90fd\u53ef\u4ee5\u5e94\u7528\u6570\u636e\u6e90\u7684\u52a8\u6001\u5207\u6362\u3002\n<\/p>\n<p>\n\t\u9664\u4e86\u4e0a\u9762\u7684\u5e94\u7528\uff0c\u5173\u4e8e\u6c34\u5e73\u5206\u5e93\u548c\u5782\u76f4\u5206\u5e93\uff0c\u90fd\u662f\u591a\u6570\u636e\u6e90\u7684\u5207\u6362\u7684\u4f7f\u7528\u573a\u666f\u3002\u5173\u4e8e\u6570\u636e\u5e93\u67b6\u6784\u65b9\u9762\u7684\u77e5\u8bc6\uff0c\u63a8\u8350\u5927\u5bb6\u9605\u8bfb\u6211\u7684\u8fd9\u7bc7\u6587\u7ae0\uff1a<a href=\"https:\/\/www.xttblog.com\/?p=631\" name=\"\u5927\u578b\u7f51\u7ad9\u5e94\u7528\u4e2dMySQL\u7684\u67b6\u6784\u6f14\u53d8\u53f2\" target=\"_blank\" title=\"\u5927\u578b\u7f51\u7ad9\u5e94\u7528\u4e2dMySQL\u7684\u67b6\u6784\u6f14\u53d8\u53f2\">\u5927\u578b\u7f51\u7ad9\u5e94\u7528\u4e2dMySQL\u7684\u67b6\u6784\u6f14\u53d8\u53f2<\/a>\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u56de\u5f52\u4eca\u5929\u7684\u4e3b\u9898\uff0c<a href=\"https:\/\/www.xttblog.com\/?tag=Spring\" name=\"Spring\" target=\"_blank\" title=\"Spring\">Spring<\/a> \u7684&nbsp;<a href=\"https:\/\/www.xttblog.com\/?tag=AbstractRoutingDataSource\" name=\"AbstractRoutingDataSource\" target=\"_blank\" title=\"AbstractRoutingDataSource\">AbstractRoutingDataSource<\/a>&nbsp;\u52a8\u6001\u6570\u636e\u6e90\uff08\u6570\u636e\u6e90\u5207\u6362\uff09\u3002\n<\/p>\n<p>\n\t<a href=\"https:\/\/www.xttblog.com\/?tag=AbstractRoutingDataSource\" name=\"AbstractRoutingDataSource\" target=\"_blank\" title=\"AbstractRoutingDataSource\">AbstractRoutingDataSource<\/a> \u7684\u4e00\u4e2a\u4f5c\u7528\u5c31\u662f\u53ef\u4ee5\u6839\u636e\u7528\u6237\u53d1\u8d77\u7684\u4e0d\u540c\u8bf7\u6c42\u53bb\u8f6c\u6362\u4e0d\u540c\u7684\u6570\u636e\u6e90\uff0c\u6bd4\u5982\u6839\u636e\u7528\u6237\u7684\u4e0d\u540c\u5730\u533a\u8bed\u8a00\u9009\u62e9\u4e0d\u540c\u7684\u6570\u636e\u5e93\u3002\u8981\u5b9e\u73b0\u52a8\u6001\u6570\u636e\u6e90\u6211\u4eec\u5fc5\u987b\u8981\u5b9e\u73b0&nbsp;<a href=\"https:\/\/www.xttblog.com\/?tag=AbstractRoutingDataSource\" name=\"AbstractRoutingDataSource\" target=\"_blank\" title=\"AbstractRoutingDataSource\">AbstractRoutingDataSource<\/a>\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\npackage com.xttblog.dataSource;\r\nimport org.slf4j.Logger;\r\nimport org.slf4j.LoggerFactory;\r\nimport org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;\r\n\/\/\u52a8\u6001\u6570\u636e\u6e90\uff08\u6570\u636e\u6e90\u5207\u6362\uff09\r\npublic class DynamicDataSource extends AbstractRoutingDataSource {\r\n&nbsp;&nbsp; &nbsp;private final static Logger _log = LoggerFactory.getLogger(DynamicDataSource.class);\r\n&nbsp;&nbsp; &nbsp;private static final ThreadLocal&lt;String&gt; contextHolder = new ThreadLocal&lt;&gt;();\r\n&nbsp;&nbsp; &nbsp;@Override\r\n&nbsp;&nbsp; &nbsp;protected Object determineCurrentLookupKey() {\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String dataSource = getDataSource();\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;_log.info(&quot;\u5f53\u524d\u64cd\u4f5c\u4f7f\u7528\u7684\u6570\u636e\u6e90\uff1a{}&quot;, dataSource);\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return dataSource;\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/**\u591a\u8bed\u8a00\u7684\u4e00\u4e2a\u5b9e\u4f8b\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String language = LocaleContextHolder.getLocale().getLanguage(); &nbsp;\r\n&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&quot;Language obtained: &quot;+ language); &nbsp;\r\n&nbsp; &nbsp; &nbsp; &nbsp; return language;*\/\r\n&nbsp;&nbsp; &nbsp;}\r\n&nbsp;&nbsp; &nbsp;\/\/\u8bbe\u7f6e\u6570\u636e\u6e90\r\n&nbsp;&nbsp; &nbsp;public static void setDataSource(String dataSource) {\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;contextHolder.set(dataSource);\r\n&nbsp;&nbsp; &nbsp;}\r\n&nbsp;&nbsp; &nbsp;\/\/\u83b7\u53d6\u6570\u636e\u6e90\r\n&nbsp;&nbsp; &nbsp;public static String getDataSource() {\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String dataSource = contextHolder.get();\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ \u5982\u679c\u6ca1\u6709\u6307\u5b9a\u6570\u636e\u6e90\uff0c\u4f7f\u7528\u9ed8\u8ba4\u6570\u636e\u6e90\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (null == dataSource) {\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getDefault());\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return contextHolder.get();\r\n&nbsp;&nbsp; &nbsp;}\r\n&nbsp;&nbsp; &nbsp;\/\/\u6e05\u9664\u6570\u636e\u6e90\r\n&nbsp;&nbsp; &nbsp;public static void clearDataSource() {\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;contextHolder.remove();\r\n&nbsp;&nbsp; &nbsp;}\r\n}<\/pre>\n<p>\n\t\u591a\u4e2a\u6570\u636e\u6e90\u662f\u591a\u4e2adataSource\uff0c\u5207\u8bb0\u4e0d\u80fd\u662f\u591a\u4e2asqlSessionFactory\u3002\u4e0b\u9762\u662f DynamicDataSource \u591a\u6570\u636e\u6e90\u7684\u914d\u7f6e\u5185\u5bb9\uff1a\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n&nbsp; &nbsp; &nbsp; &nbsp;xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n&nbsp; &nbsp; &nbsp; &nbsp;xmlns:aop=&quot;http:\/\/www.springframework.org\/schema\/aop&quot;\r\n&nbsp; &nbsp; &nbsp; &nbsp;xmlns:tx=&quot;http:\/\/www.springframework.org\/schema\/tx&quot;\r\n&nbsp; &nbsp; &nbsp; &nbsp;xmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot;\r\n&nbsp; &nbsp; &nbsp; &nbsp;xsi:schemaLocation=&quot;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; http:\/\/www.springframework.org\/schema\/beans\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; http:\/\/www.springframework.org\/schema\/tx\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; http:\/\/www.springframework.org\/schema\/tx\/spring-tx.xsd\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; http:\/\/www.springframework.org\/schema\/context\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; http:\/\/www.springframework.org\/schema\/context\/spring-context.xsd\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; http:\/\/www.springframework.org\/schema\/aop\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; http:\/\/www.springframework.org\/schema\/aop\/spring-aop.xsd&quot;&gt;\r\n&nbsp; &nbsp; &lt;!-- \u5f15\u5165jdbc\u914d\u7f6e\u6587\u4ef6 --&gt;\r\n&nbsp; &nbsp; &lt;!--&lt;context:property-placeholder location=&quot;classpath:jdbc.properties&quot; \/&gt;--&gt;\r\n&nbsp; &nbsp; &lt;!-- \u914d\u7f6e\u8fdb\u884c\u89e3\u5bc6 &nbsp;--&gt;\r\n&nbsp; &nbsp; &lt;bean id=&quot;propertyConfigurer&quot; class=&quot;com.xttblog.plugin.EncryptPropertyPlaceholderConfigurer&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;locations&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;list&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;value&gt;classpath:jdbc.properties&lt;\/value&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;value&gt;classpath:redis.properties&lt;\/value&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;\/list&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;\/property&gt;\r\n&nbsp; &nbsp; &lt;\/bean&gt;\r\n&nbsp; &nbsp; &lt;!-- \u4e3b\u5e93\u6570\u636e\u6e90 --&gt;\r\n&nbsp; &nbsp; &lt;bean id=&quot;masterDataSource&quot; class=&quot;com.alibaba.druid.pool.DruidDataSource&quot; init-method=&quot;init&quot;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; destroy-method=&quot;close&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- \u57fa\u672c\u5c5e\u6027 url\u3001user\u3001password --&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;driverClassName&quot; value=&quot;${master.jdbc.driver}&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;url&quot; value=&quot;${master.jdbc.url}&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;username&quot; value=&quot;${master.jdbc.username}&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;password&quot; value=&quot;${master.jdbc.password}&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- \u914d\u7f6e\u521d\u59cb\u5316\u5927\u5c0f\u3001\u6700\u5c0f\u3001\u6700\u5927 --&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;initialSize&quot; value=&quot;1&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;minIdle&quot; value=&quot;1&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;maxActive&quot; value=&quot;20&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- \u914d\u7f6e\u83b7\u53d6\u8fde\u63a5\u7b49\u5f85\u8d85\u65f6\u7684\u65f6\u95f4 --&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;maxWait&quot; value=&quot;60000&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- \u914d\u7f6e\u95f4\u9694\u591a\u4e45\u624d\u8fdb\u884c\u4e00\u6b21\u68c0\u6d4b\uff0c\u68c0\u6d4b\u9700\u8981\u5173\u95ed\u7684\u7a7a\u95f2\u8fde\u63a5\uff0c\u5355\u4f4d\u662f\u6beb\u79d2 --&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;timeBetweenEvictionRunsMillis&quot; value=&quot;60000&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- \u914d\u7f6e\u4e00\u4e2a\u8fde\u63a5\u5728\u6c60\u4e2d\u6700\u5c0f\u751f\u5b58\u7684\u65f6\u95f4\uff0c\u5355\u4f4d\u662f\u6beb\u79d2 --&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;minEvictableIdleTimeMillis&quot; value=&quot;300000&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- \u6821\u9a8c\u8bed\u53e5 --&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;validationQuery&quot; value=&quot;SELECT 1&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;testWhileIdle&quot; value=&quot;true&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;testOnBorrow&quot; value=&quot;false&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;testOnReturn&quot; value=&quot;false&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- \u914d\u7f6e\u76d1\u63a7\u7edf\u8ba1\u62e6\u622a\u7684filters --&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;filters&quot; value=&quot;stat&quot;\/&gt;\r\n&nbsp; &nbsp; &lt;\/bean&gt;\r\n&nbsp; &nbsp; &lt;!-- \u4ece\u5e93\u6570\u636e\u6e90 --&gt;\r\n&nbsp; &nbsp; &lt;bean id=&quot;slaveDataSource&quot; class=&quot;com.alibaba.druid.pool.DruidDataSource&quot; init-method=&quot;init&quot; destroy-method=&quot;close&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- \u57fa\u672c\u5c5e\u6027 url\u3001user\u3001password --&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;driverClassName&quot; value=&quot;${slave.jdbc.driver}&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;url&quot; value=&quot;${slave.jdbc.url}&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;username&quot; value=&quot;${slave.jdbc.username}&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;password&quot; value=&quot;${slave.jdbc.password}&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- \u914d\u7f6e\u521d\u59cb\u5316\u5927\u5c0f\u3001\u6700\u5c0f\u3001\u6700\u5927 --&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;initialSize&quot; value=&quot;1&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;minIdle&quot; value=&quot;1&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;maxActive&quot; value=&quot;20&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- \u914d\u7f6e\u83b7\u53d6\u8fde\u63a5\u7b49\u5f85\u8d85\u65f6\u7684\u65f6\u95f4 --&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;maxWait&quot; value=&quot;60000&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- \u914d\u7f6e\u95f4\u9694\u591a\u4e45\u624d\u8fdb\u884c\u4e00\u6b21\u68c0\u6d4b\uff0c\u68c0\u6d4b\u9700\u8981\u5173\u95ed\u7684\u7a7a\u95f2\u8fde\u63a5\uff0c\u5355\u4f4d\u662f\u6beb\u79d2 --&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;timeBetweenEvictionRunsMillis&quot; value=&quot;60000&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- \u914d\u7f6e\u4e00\u4e2a\u8fde\u63a5\u5728\u6c60\u4e2d\u6700\u5c0f\u751f\u5b58\u7684\u65f6\u95f4\uff0c\u5355\u4f4d\u662f\u6beb\u79d2 --&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;minEvictableIdleTimeMillis&quot; value=&quot;300000&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- \u6821\u9a8c\u8bed\u53e5 --&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;validationQuery&quot; value=&quot;SELECT 1&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;testWhileIdle&quot; value=&quot;true&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;testOnBorrow&quot; value=&quot;false&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;testOnReturn&quot; value=&quot;false&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- \u914d\u7f6e\u76d1\u63a7\u7edf\u8ba1\u62e6\u622a\u7684filters --&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;filters&quot; value=&quot;stat&quot;\/&gt;\r\n&nbsp; &nbsp; &lt;\/bean&gt;\r\n&nbsp; &nbsp; &lt;!-- \u52a8\u6001\u6570\u636e\u6e90 --&gt;\r\n&nbsp; &nbsp; &lt;bean id=&quot;dataSource&quot; class=&quot;com.xttblog.dataSource.DynamicDataSource&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;targetDataSources&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;map key-type=&quot;java.lang.String&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- \u53ef\u914d\u7f6e\u591a\u4e2a\u6570\u636e\u6e90 --&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;entry value-ref=&quot;masterDataSource&quot; key=&quot;masterDataSource&quot;&gt;&lt;\/entry&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;entry value-ref=&quot;slaveDataSource&quot; key=&quot;slaveDataSource&quot;&gt;&lt;\/entry&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;\/map&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;\/property&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;defaultTargetDataSource&quot; ref=&quot;masterDataSource&quot;&gt;&lt;\/property&gt;\r\n&nbsp; &nbsp; &lt;\/bean&gt;\r\n&nbsp; &nbsp; &lt;!-- \u4e3aMybatis\u521b\u5efaSqlSessionFactory\uff0c\u540c\u65f6\u6307\u5b9a\u6570\u636e\u6e90 --&gt;\r\n&nbsp; &nbsp; &lt;bean id=&quot;sqlSessionFactory&quot; class=&quot;org.mybatis.spring.SqlSessionFactoryBean&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;configLocation&quot; value=&quot;classpath:mybatis-config.xml&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;mapperLocations&quot; value=&quot;classpath*:com\/zheng\/pay\/dao\/mapper\/*Mapper.xml&quot;\/&gt;\r\n&nbsp; &nbsp; &lt;\/bean&gt;\r\n&nbsp; &nbsp; &lt;!-- Mapper\u63a5\u53e3\u6240\u5728\u5305\u540d\uff0cSpring\u4f1a\u81ea\u52a8\u67e5\u627e\u5176\u4e0b\u7684Mapper --&gt;\r\n&nbsp; &nbsp; &lt;bean id=&quot;mapperScannerConfigurer&quot; class=&quot;org.mybatis.spring.mapper.MapperScannerConfigurer&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;basePackage&quot; value=&quot;**.mapper&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;sqlSessionFactoryBeanName&quot; value=&quot;sqlSessionFactory&quot;\/&gt;\r\n&nbsp; &nbsp; &lt;\/bean&gt;\r\n&nbsp; &nbsp; &lt;!-- \u4e8b\u52a1\u7ba1\u7406\u5668 --&gt;\r\n&nbsp; &nbsp; &lt;bean id=&quot;transactionManager&quot; class=&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;\/&gt;\r\n&nbsp; &nbsp; &lt;\/bean&gt;\r\n&nbsp; &nbsp; &lt;tx:advice id=&quot;txAdvice&quot; transaction-manager=&quot;transactionManager&quot;&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;tx:attributes&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tx:method name=&quot;insert*&quot; propagation=&quot;NESTED&quot; rollback-for=&quot;Exception&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tx:method name=&quot;add*&quot; propagation=&quot;NESTED&quot; rollback-for=&quot;Exception&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tx:method name=&quot;update*&quot; propagation=&quot;NESTED&quot; rollback-for=&quot;Exception&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tx:method name=&quot;modify*&quot; propagation=&quot;NESTED&quot; rollback-for=&quot;Exception&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tx:method name=&quot;edit*&quot; propagation=&quot;NESTED&quot; rollback-for=&quot;Exception&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tx:method name=&quot;del*&quot; propagation=&quot;NESTED&quot; rollback-for=&quot;Exception&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tx:method name=&quot;save*&quot; propagation=&quot;NESTED&quot; rollback-for=&quot;Exception&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tx:method name=&quot;send*&quot; propagation=&quot;NESTED&quot; rollback-for=&quot;Exception&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tx:method name=&quot;get*&quot; read-only=&quot;true&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tx:method name=&quot;find*&quot; read-only=&quot;true&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tx:method name=&quot;query*&quot; read-only=&quot;true&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tx:method name=&quot;search*&quot; read-only=&quot;true&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tx:method name=&quot;select*&quot; read-only=&quot;true&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tx:method name=&quot;count*&quot; read-only=&quot;true&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;\/tx:attributes&gt;\r\n&nbsp; &nbsp; &lt;\/tx:advice&gt;\r\n&nbsp;\r\n&nbsp; &nbsp; &lt;aop:config&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;aop:pointcut id=&quot;service&quot; expression=&quot;execution(* com.xttblog.datasource..*.service.*.*(..))&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;!-- \u5173\u952e\u914d\u7f6e\uff0c\u5207\u6362\u6570\u636e\u6e90\u4e00\u5b9a\u8981\u6bd4\u6301\u4e45\u5c42\u4ee3\u7801\u66f4\u5148\u6267\u884c\uff08\u4e8b\u52a1\u4e5f\u7b97\u6301\u4e45\u5c42\u4ee3\u7801\uff09 --&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;aop:advisor advice-ref=&quot;txAdvice&quot; pointcut-ref=&quot;service&quot; order=&quot;2&quot;\/&gt;\r\n&nbsp; &nbsp; &nbsp; &nbsp; &lt;aop:advisor advice-ref=&quot;dataSourceExchange&quot; pointcut-ref=&quot;service&quot; order=&quot;1&quot;\/&gt;\r\n&nbsp; &nbsp; &lt;\/aop:config&gt;\r\n&lt;\/beans&gt;<\/pre>\n<p>\n\t\u4ee5\u4e0a\u6838\u5fc3\u4ee3\u7801\u5c31\u53ef\u4ee5\u8ba9\u6211\u4eec\u57fa\u4e8eaop\u5b9e\u73b0\u591a\u8bed\u8a00\uff0c\u6216\u8005\u8bfb\u5199\u5206\u79bb\u4e4b\u7c7b\u7684\u591a\u6570\u636e\u6e90\u5207\u6362\u4e86\u3002\n<\/p>\n<p>\n\t\u8fd9\u7bc7\u6587\u7ae0\u53ea\u662f\u4ecb\u7ecd\u4e86&nbsp;<a href=\"https:\/\/www.xttblog.com\/?tag=AbstractRoutingDataSource\" name=\"AbstractRoutingDataSource\" target=\"_blank\" title=\"AbstractRoutingDataSource\">AbstractRoutingDataSource<\/a> \u7684\u7b80\u964b\u5b9e\u73b0\uff0c\u5173\u4e8e\u8bfb\u5199\u5206\u79bb\u548c <a href=\"https:\/\/www.xttblog.com\/?tag=AbstractRoutingDataSource\" name=\"AbstractRoutingDataSource\" target=\"_blank\" title=\"AbstractRoutingDataSource\">AbstractRoutingDataSource<\/a>&nbsp;\u7684\u5b9e\u73b0\u539f\u7406\uff0c\u6211\u4eec\u540e\u8fb9\u5728\u5b9e\u73b0\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=2021\">Spring\u52a8\u6001\u8def\u7531AbstractRoutingDataSource(\u6570\u636e\u6e90\u52a8\u6001\u5207\u6362)\u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6570\u636e\u6e90\u52a8\u6001\u5207\u6362\u4e5f\u4e0d\u662f\u4ec0\u4e48\u65b0\u6280\u672f\uff0c\u963f\u91cc\u5728\u65e9\u671f\u90fd\u6709\u591a\u9686\u5927\u795e\u5b9e\u73b0\u4e86\u3002\u4f46\u662f\u6211\u4eec\u4eca\u5929\u8981\u8bb2\u7684\u662f Spring \u5bf9\u6570\u636e\u6e90\u8def\u7531 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1537,69,551],"class_list":["post-2021","post","type-post","status-publish","format-standard","hentry","category-java","tag-abstractroutingdatasource","tag-java","tag-spring"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2021"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2021\/revisions"}],"predecessor-version":[{"id":2160,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2021\/revisions\/2160"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}