上一篇博客说到mybatis对日志的实现有一个优先顺序,本篇以jdkLog为例探讨mybatis运用到的动态代理模式。 首先要知道它为什么要使用动态代理,可以观察到当执行mybatis的代码时,他总能打印出一些执行语句的记录,这就像spring里的aop(通过动态代理实现),是通过动态代理实现的,如下类都是mybatis对动态代理日志类的实现:
可以通过下图看出底下的四个logger都继承了基类logger,并且都实现了invocationhandler:
这样整个动态代理框架就出来了。 例如ConnectionLogger:
代码语言:javascript复制/*
* Copyright ${license.git.copyrightYears} 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
*
* http://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.apache.ibatis.logging.jdbc;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.reflection.ExceptionUtil;
/**
* Connection proxy to add logging.
*
* @author Clinton Begin
* @author Eduardo Macarron
*
*/
//负责打印连接信息和sql语句并创建一个preparestatmentLogger
public final class ConnectionLogger extends BaseJdbcLogger implements InvocationHandler {
//真正的连接对象
private final Connection connection;
private ConnectionLogger(Connection conn, Log statementLog, int queryStack) {
super(statementLog, queryStack);
this.connection = conn;
}
@Override
public Object invoke(Object proxy, Method method, Object[] params)
throws Throwable {
try {
//如果是object自己的方法则忽略
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, params);
}
//增强跟数据库打交道的方法
if ("prepareStatement".equals(method.getName()) || "prepareCall".equals(method.getName())) {
if (isDebugEnabled()) {
debug(" Preparing: " removeExtraWhitespace((String) params[0]), true);
}
PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params);
//创建一个PreparedStatementLogger,具有相关的preparedstatement的打印
stmt = PreparedStatementLogger.newInstance(stmt, statementLog, queryStack);
return stmt;
} else if ("createStatement".equals(method.getName())) {
Statement stmt = (Statement) method.invoke(connection, params);
stmt = StatementLogger.newInstance(stmt, statementLog, queryStack);
return stmt;
} else {
return method.invoke(connection, params);
}
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
}
/**
* Creates a logging version of a connection.
*
* @param conn
* the original connection
* @param statementLog
* the statement log
* @param queryStack
* the query stack
* @return the connection with logging
*/
public static Connection newInstance(Connection conn, Log statementLog, int queryStack) {
InvocationHandler handler = new ConnectionLogger(conn, statementLog, queryStack);
ClassLoader cl = Connection.class.getClassLoader();
return (Connection) Proxy.newProxyInstance(cl, new Class[]{Connection.class}, handler);
}
/**
* return the wrapped connection.
*
* @return the connection
*/
public Connection getConnection() {
return connection;
}
}
很明显的可以看见增强逻辑。