JDK代理与CGLib代理区别

1、JDK动态代理基于接口实现,所以实现JDK动态代理,必须先定义接口;CGLib动态代理基于被代理类实现;

2、JDK动态代理机制是委托机制,委托hanlder调用原始实现类方法;CGLib则使用继承机制,被代理类和代理类是继承关系,所以代理类对象可以赋值给被代理类类型的变量;如果被代理类有接口,那么代理类对象也可以赋值给该接口类型的变量。

xml中配置时默认是JDK代理,要开启CGLib代理的话,在xml中需要配置<aop:aspectj-autoproxy proxy-target-class="true"/>

测试Test

package yxy.club.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import yxy.club.computer.service.IComputerService;

public class Test {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
		IComputerService computerService= applicationContext.getBean(IComputerService.class);//如果为JDK代理该处不能写为ComputerService.class,若为CGLib代理可以
		Class clazz=computerService.getClass();
		System.out.println(clazz.getSuperclass().getName());
		applicationContext.close();
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值