动态代理Cglib
Posted tsugar
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态代理Cglib相关的知识,希望对你有一定的参考价值。
jar包
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.1</version>
</dependency>
代码:
切面:
package com.blb.proxy;
public class AOPS{
public void sayhello()
{
System.out.println("我是AOPS类中的sayhello方法");
}
public void before()
{
System.out.println("我是前面");
}
public void after()
{
System.out.println("我是后面");
}
}
关注点:
package com.blb.proxy;
public class Hello {
public void sayhello()
{
System.out.println("我是Hello类中的sayhello方法");
}
}
切入规则:
package com.blb.proxy;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class Rule implements MethodInterceptor {
private AOPS aops = new AOPS();
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
aops.before();
Object result = methodProxy.invokeSuper(o,objects);
aops.after();
return result;
}
}
开始切入:
package com.blb.proxy;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class Rule implements MethodInterceptor {
private AOPS aops = new AOPS();
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
aops.before();
Object result = methodProxy.invokeSuper(o,objects);
aops.after();
return result;
}
}
以上是关于动态代理Cglib的主要内容,如果未能解决你的问题,请参考以下文章