动态代理之cglib
来源:互联网 发布:小程序cms系统 编辑:程序博客网 时间:2024/06/05 17:12
代理技术有俩种实现方式::
* JDK的动态代理 :针对实现了接口的类产生代理.
* Cglib的动态代理 :针对没有实现接口的类产生代理.产生了这个类的一个子类对象.不要使用final修改这个类.
这篇文章说一下Cglib方式的动态代理
直接上例子:
一个类
public class StudentDao {public void add(){System.out.println("我是添加方法。。。。。。。。");}public void delete(){System.out.println("我是删除方法。。。。。。。。");}public void modify(){System.out.println("我是修改方法。。。。。。。。");}public void find(){System.out.println("我是查询方法。。。。。。。。");}}一个Cglib代理类
public class StudentCglibProxy implements MethodInterceptor {private StudentDao studentDao;public StudentCglibProxy(StudentDao studentDao){this.studentDao = studentDao;}/** * 创建代理对象 */public StudentDao createProxy(){// 创建Cglib的核心类:Enhancer enhancer = new Enhancer();// 设置父类:enhancer.setSuperclass(StudentDao.class);// 设置回调:enhancer.setCallback(this);// 生成代理:StudentDao studentProxy = (StudentDao) enhancer.create();return studentProxy;}@Overridepublic Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {//判断方法if("delete".equals(method.getName())){System.out.println("使用Cglib的方式增强了删除方法");return methodProxy.invokeSuper(proxy, args);}return methodProxy.invokeSuper(proxy, args);}}一个测试类
@Testpublic void demo1(){StudentDao studentDao = new StudentDao();StudentDao proxy = new StudentCglibProxy(studentDao).createProxy();proxy.add();proxy.delete();proxy.modify();proxy.find();}出结果:有图有真相
阅读全文
0 0
- 动态代理之cglib
- 动态代理之CGLIB
- 动态代理之CGLib
- 动态代理之cglib
- 动态代理之cglib代理
- Java动态代理之CGLIB
- 动态代理之cglib实现
- Java动态代理之CGLIB
- Java动态代理之cglib
- 代理模式之cglib动态代理
- 代理模式之cglib动态代理
- 代理模式之cglib动态代理
- java之架构基础-动态代理&cglib
- (五)sping之CGLIB动态代理
- java之架构基础-动态代理&cglib
- java之架构基础-动态代理&cglib
- 动态代理之JDK代理与CGlib代理
- Spirng之静态代理、动态代理、以及cglib代理
- 网络状态的判断
- Kotlin学习记录(五)—— 条件表达式的使用
- Cygwin安装教程
- Could not find bundle: org.eclipse.equinox.console
- VSCode使用
- 动态代理之cglib
- Dyna
- 设计模式(七)建造者模式
- intellij idea 找不到静态资源的解决方法
- 文件上传在Struts的Action检测文件类型
- 人工智能基础复习4——不确定知识与推理
- 微信小程序 如何在页面直接传递信息 / 通信方法 / 附带项目案例
- 国际贸易基础(三)商检
- dts和dtsi分析