动态代理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;
}
}

 

posted @ 2020-02-11 23:38  Tsugar  阅读(142)  评论(0编辑  收藏  举报