一、MethodInterceptor简介
public interface MethodInterceptor extends Callback {
Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable;
}
MethodInterceptor是CGLIB动态代理中的一个回调接口,用于拦截方法调用,并在方法调用前后执行一些额外的逻辑。它定义了一个intercept()
方法,在代理对象调用被代理方法前后都会被调用。
二、MethodInterceptor源码分析
MethodInterceptor接口源码如下:
public interface MethodInterceptor extends Callback {
Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable;
}
这个接口继承了Callback接口,同时声明了一个intercept()
方法,该方法用于代理对象的方法拦截。参数含义如下:
obj
:代理对象method
:被代理方法args
:被代理方法的参数proxy
:MethodProxy对象,可以用来执行原始方法 接下来看一下MethodInterceptor接口的实现类:
public class UserServiceInterceptor implements MethodInterceptor {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("进入代理方法");
Object result = proxy.invokeSuper(obj, args);
System.out.println("离开代理方法");
return result;
}
}
在UserServiceInterceptor
实现类中,我们重写了intercept()
方法,并在方法调用前后分别打印了日志。此时通过CGLIB动态代理创建一个UserService对象,然后使用MethodInterceptor拦截该对象,就可以在方法调用前后,添加一些额外的逻辑。
三、MethodInterceptor的应用场景
1、MethodInterceptor找不到
如果使用Spring AOP切面,我们往往需要在目标Bean上添加注解或进行配置,然后再在配置文件中指定切面来对目标对象的方法进行增强。但是在某些情况下,我们无法修改目标Bean的代码,这时候就需要使用MethodInterceptor来对目标对象的方法进行增强。
2、MethodInterceptor拦截不到
有时候我们可能会遇到一些问题,例如使用代理对象调用String类的equals()
方法,但是通过MethodInterceptor并不能拦截到equals()
方法的调用。这是因为equals()
方法是final方法,CGLIB无法对其进行代理。
3、MethodInterceptor接口的作用
MethodInterceptor接口的作用是拦截方法调用,并在方法调用前后执行一些额外的逻辑。我们可以在intercept()
方法中添加一些我们需要的额外逻辑,例如日志、缓存、权限等。
四、MethodInterceptor示例代码
下面是一个使用MethodInterceptor实现事务拦截的示例代码:
public class TransactionInterceptor implements MethodInterceptor {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
Connection connection = getConnection();
try {
connection.setAutoCommit(false);
Object result = proxy.invokeSuper(obj, args);
connection.commit();
return result;
} catch (Exception e) {
e.printStackTrace();
connection.rollback();
throw e;
} finally {
closeConnection();
}
}
private void closeConnection() {
//...
}
private Connection getConnection() {
//...
}
}
在TransactionInterceptor
类中,我们定义了一个intercept()
方法,用于实现事务拦截的逻辑。首先获取数据库连接,然后关闭自动提交,执行被代理对象的方法,如果方法调用成功则提交事务,否则回滚事务。最后释放数据库连接。
接下来我们使用CGLIB动态代理来创建UserService对象,并使用TransactionInterceptor拦截该对象,实现事务的拦截和管理。
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(UserService.class);
TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
enhancer.setCallback(transactionInterceptor);
UserService userService = (UserService) enhancer.create();
userService.addUser(user);
以上代码中,我们使用Enhancer对UserService类进行代理,使用TransactionInterceptor对代理对象进行拦截。