Selaa lähdekoodia

合并2.x dataSource被代理处理

Caratacus 6 vuotta sitten
vanhempi
commit
8661e52aa0

+ 92 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/AopUtils.java

@@ -0,0 +1,92 @@
+/**
+ * Copyright (c) 2011-2014, hubin (jobob@qq.com).
+ * <p>
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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 com.baomidou.mybatisplus.core.toolkit;
+
+import java.lang.reflect.Field;
+
+import org.apache.ibatis.logging.Log;
+import org.apache.ibatis.logging.LogFactory;
+
+/**
+ * AopUtils Aop工具类
+ *
+ * @author Caratacus
+ * @date 2018/08/02
+ */
+public class AopUtils {
+
+    private static final Log logger = LogFactory.getLog(AopUtils.class);
+
+    /**
+     * 获取源目标对象
+     *
+     * @param proxy
+     * @param <T>
+     * @return
+     */
+    public static <T> T getTargetObject(T proxy) {
+        if (!ClassUtils.isProxy(proxy.getClass())) {
+            return proxy;
+        }
+        try {
+            if (org.springframework.aop.support.AopUtils.isJdkDynamicProxy(proxy)) {
+                return getJdkDynamicProxyTargetObject(proxy);
+            } else if (org.springframework.aop.support.AopUtils.isCglibProxy(proxy)) {
+                return getCglibProxyTargetObject(proxy);
+            } else {
+                logger.warn("Warn: The proxy object processing method is not supported.");
+                return proxy;
+            }
+        } catch (Exception e) {
+            throw ExceptionUtils.mpe("Error: Get proxy targetObject exception !  Cause:" + e);
+        }
+    }
+
+    /**
+     * 获取Cglib源目标对象
+     *
+     * @param proxy
+     * @param <T>
+     * @return
+     */
+    private static <T> T getCglibProxyTargetObject(T proxy) throws Exception {
+        Field cglibField = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0");
+        cglibField.setAccessible(true);
+        Object dynamicAdvisedInterceptor = cglibField.get(proxy);
+        Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised");
+        advised.setAccessible(true);
+        Object target = ((org.springframework.aop.framework.AdvisedSupport) advised.get(dynamicAdvisedInterceptor)).getTargetSource().getTarget();
+        return (T) target;
+    }
+
+    /**
+     * 获取JdkDynamic源目标对象
+     *
+     * @param proxy
+     * @param <T>
+     * @return
+     */
+    private static <T> T getJdkDynamicProxyTargetObject(T proxy) throws Exception {
+        Field jdkDynamicField = proxy.getClass().getSuperclass().getDeclaredField("jdkDynamicField");
+        jdkDynamicField.setAccessible(true);
+        org.springframework.aop.framework.AopProxy aopProxy = (org.springframework.aop.framework.AopProxy) jdkDynamicField.get(proxy);
+        Field advised = aopProxy.getClass().getDeclaredField("advised");
+        advised.setAccessible(true);
+        Object target = ((org.springframework.aop.framework.AdvisedSupport) advised.get(aopProxy)).getTargetSource().getTarget();
+        return (T) target;
+    }
+
+}

+ 2 - 1
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/spring/MybatisSqlSessionFactoryBean.java

@@ -67,6 +67,7 @@ import com.baomidou.mybatisplus.core.MybatisConfiguration;
 import com.baomidou.mybatisplus.core.MybatisXMLConfigBuilder;
 import com.baomidou.mybatisplus.core.MybatisXMLConfigBuilder;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.core.enums.IEnum;
 import com.baomidou.mybatisplus.core.enums.IEnum;
+import com.baomidou.mybatisplus.core.toolkit.AopUtils;
 import com.baomidou.mybatisplus.core.toolkit.Assert;
 import com.baomidou.mybatisplus.core.toolkit.Assert;
 import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
 import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
 import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
 import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
@@ -570,7 +571,7 @@ public class MybatisSqlSessionFactoryBean implements FactoryBean<SqlSessionFacto
         }
         }
         // TODO 设置元数据相关 如果用户没有配置 dbType 则自动获取
         // TODO 设置元数据相关 如果用户没有配置 dbType 则自动获取
         if (globalConfig.getDbConfig().getDbType() == DbType.OTHER) {
         if (globalConfig.getDbConfig().getDbType() == DbType.OTHER) {
-            try (Connection connection = dataSource.getConnection()) {
+            try (Connection connection = AopUtils.getTargetObject(this.dataSource).getConnection()) {
                 globalConfig.getDbConfig().setDbType(JdbcUtils.getDbType(connection.getMetaData().getURL()));
                 globalConfig.getDbConfig().setDbType(JdbcUtils.getDbType(connection.getMetaData().getURL()));
             } catch (Exception e) {
             } catch (Exception e) {
                 throw ExceptionUtils.mpe("Error: GlobalConfigUtils setMetaData Fail !  Cause:" + e);
                 throw ExceptionUtils.mpe("Error: GlobalConfigUtils setMetaData Fail !  Cause:" + e);