<span style="font-family:SimHei;font-size:18px;">如果目标类没有实现任何接口,那么默认情况下,ProxyFactory会对目标类进行基于类的代理,即使用CGLIB,
假设我们现有一个目标类定义如下:
public class Executable {
public void execute(){
System.out.println("Executable without any Interfaces");
}
}
如果使用Executable作为目标对象,那么ProxyFactory就会对其进行基于类的代理,下面的代码演示了使用ProxyFactory对Executable进行织入的过程.
ProxyFactory proxy1=new ProxyFactory(new Executable());
NameMatchMethodPointcutAdvisor advisor1=new NameMatchMethodPointcutAdvisor();
advisor1.setMappedName("execute");
advisor1.setAdvice(new PerformanceMethodInterceptor());
proxy1.addAdvisor(advisor1);
Executable proxyObject1=(Executable) proxy1.getProxy();
proxyObject1.execute();
System.out.println(proxyObject1.getClass());
PerformanceMethodInterceptor定义如下:
public class PerformanceMethodInterceptor implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// TODO Auto-generated method stub
System.out.println("you must overcome the obstacle hiding in yor heart");
return invocation.proceed();
}
}
这样就实现了基于类的代理,最重要的还是动手实践。。。继续加油!</span>
基于类的代理
最新推荐文章于 2022-08-02 14:24:41 发布