
如何使用 cglib 拦截 java.sql.statement 类
在不修改源代码的情况下拦截增强 java.sql.statement 类,可以使用 cglib。但是,使用 cglib 需要手动使用 enhancer#create() 方法创建一个代理类,手动调用才能触发 callback 的钩子函数,似乎不太方便。
其实,我们可以通过代理 connection 对象来间接拦截 statement 类。修改数据源的 getconnection 方法,使其返回代理对象即可。这样,业务代码无需修改,也能实现拦截增强。
无阶未来模型擂台/AI 应用平台
无阶未来模型擂台/AI 应用平台,一站式模型+应用平台
35 查看详情
代码示例如下:
立即学习“Java免费学习笔记(深入)”;
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 unwrap(Class 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); } // ... }}
以上就是如何用CGLIB无侵入式拦截增强java.sql.Statement类?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/385001.html
微信扫一扫
支付宝扫一扫