标签导航:

如何用cglib无侵入式拦截增强java.sql.statement类?

如何使用 cglib 拦截 java.sql.statement 类

在不修改源代码的情况下拦截增强 java.sql.statement 类,可以使用 cglib。但是,使用 cglib 需要手动使用 enhancer#create() 方法创建一个代理类,手动调用才能触发 callback 的钩子函数,似乎不太方便。

其实,我们可以通过代理 connection 对象来间接拦截 statement 类。修改数据源的 getconnection 方法,使其返回代理对象即可。这样,业务代码无需修改,也能实现拦截增强。

代码示例如下:

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.Statement;

public class StatementInterceptor implements MethodInterceptor {

    private final DataSource dataSource;

    public StatementInterceptor(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        if ("createStatement".equals(method.getName())) {
            Statement statement = (Statement) proxy.invokeSuper(obj, args);
            return new StatementProxy(statement);
        }
        return proxy.invokeSuper(obj, args);
    }

    public static void main(String[] args) {
        DataSource dataSource = new MyDataSource();
        DataSource proxyDataSource = (DataSource) Enhancer.create(dataSource.getClass(), new StatementInterceptor(dataSource));
        Connection connection = proxyDataSource.getConnection();
        Statement statement = connection.createStatement();
        // ...
    }

    private static class MyDataSource implements DataSource {

        @Override
        public Connection getConnection() {
            return null;
        }

        @Override
        public Connection getConnection(String username, String password) {
            return null;
        }

        @Override
        public <T> T unwrap(Class<T> iface) {
            return null;
        }

        @Override
        public boolean isWrapperFor(Class<?> iface) {
            return false;
        }
    }

    private static class StatementProxy implements Statement {

        private final Statement statement;

        public StatementProxy(Statement statement) {
            this.statement = statement;
        }

        @Override
        public ResultSet executeQuery(String sql) {
            // ...
            return statement.executeQuery(sql);
        }

        @Override
        public int executeUpdate(String sql) {
            // ...
            return statement.executeUpdate(sql);
        }

        // ...
    }
}