首页 > 其他 > 详细

动态代理Cglib

时间:2020-02-11 23:54:56      阅读:99      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/Tsugar/p/12297451.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!