Spring框架中的JDK与CGlib动态代理

CGlib:可以是接口也可以是类

这里讨论的是test类中(是用接口还是目标类)

IMathService mathService = applicationContext.getBean(MathService.class);test类中源码:

<code>package com.jd.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.jd.math.IMathService;import com.jd.math.MathService; public class Test { public static void main(String[] args) {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");//true的时候是根据目标类来创建,false是根据接口来创建的,只能通过接口。IMathService mathService = applicationContext.getBean(MathService.class);System.out.println(mathService.getClass());applicationContext.close();}}/<code>

xml源码:

<code><beans>   <component-scan>    <aspectj-autoproxy>/<beans>复制代码/<code>

引用jar包:

Spring框架中的JDK与CGlib动态代理

这里在xml中;proxy-target-class="true" true的时候是根据目标类来创建,false是根据接口来创建的,只能通过接口

运行结果:

Spring框架中的JDK与CGlib动态代理

文件结构:

Spring框架中的JDK与CGlib动态代理

CGlib动态代理所产生的代理类是目标子类JDK动态代理产生的代理类与目标的继承关系,其代理类是目标类所实现的接口的实现类

CGlib测试:xml中改为true

test类源码:

<code>package com.jd.test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.jd.math.IMathService;import com.jd.math.MathService; public class Test { public static void main(String[] args) {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");//true的时候是根据目标类来创建,false是根据接口来创建的,只能通过接口。jdk只能用接口//CGlib3.2开始自身支持CGlibIMathService mathService = applicationContext.getBean(MathService.class);//CGlib动态代理所产生的代理类是目标子类//JDK动态代理产生的代理类与目标的继承关系,其代理类是目标类所实现的接口的实现类System.out.println(mathService.getClass().getSuperclass());Class clazz = mathService.getClass();Class [] array = clazz.getInterfaces();for(Class c:array) {System.out.println(c.getName());}applicationContext.close();}}/<code> 

运行结果:class com.jd.math.MathServiceorg.springframework.aop.SpringProxy

org.springframework.aop.framework.Advisedorg.springframework.cglib.proxy.Factory

JDK测试:xml中改为false

test类中

Spring框架中的JDK与CGlib动态代理

这里为IMathService。

运行结果:

class java.lang.reflect.Proxycom.jd.math.IMathService

org.springframework.aop.SpringProxy

org.springframework.aop.framework.Advised

org.springframework.core.DecoratingProxy

事务中这两种讨论:CGlib

test源码:

<code>package com.jd.test; import java.util.HashMap;import java.util.Map; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.jd.car.CarService;import com.jd.car.ICarService;import com.jd.coupon.service.CouponService;import com.jd.coupon.service.ICouponService; public class Test { /*public static void main(String[] args) {ClassPathXmlApplicationContext application = new ClassPathXmlApplicationContext("application.xml");//立即购买ICouponService couponService = application.getBean(ICouponService.class);System.out.println(couponService.getClass().getName());//String userId = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";//String bookId = "8" ;//String bookId = "445" ;//库存有10本 他一次买了5本//int count=7;if(couponService.insert(userId, bookId, count)) {System.out.println("OK");}//购物车购买ICarService carService = application.getBean(ICarService.class);String userId = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";Map<string> commodities = new HashMap<string>();//买两本书commodities.put("a2f39533-659f-42ca-af91-c688a83f6e49",1);commodities.put("4c37672a-653c-4cc8-9ab5-ee0c614c7425",1);carService.batch(userId, commodities);application.close();}*/public static void main(String[] args) {//一个类中的方法被@Transctional注解修饰,则Spring自动为该类创建代理类及代理对象   ClassPathXmlApplicationContext application = new ClassPathXmlApplicationContext("application.xml");ICarService carService = application.getBean(CarService.class);System.out.println(carService.getClass().getName());application.close();}/<string>/<string>/<code>

test类中:ICarService carService = application.getBean(CarService.class);

这里为CarService.class

配置xml:

<code><beans> <component-scan> <bean>    <property>    <property>/<bean>  <bean> <bean>  <property>/<bean> <annotation-driven><aspectj-autoproxy> /<beans>/<code>

这里:<aspectj-autoproxy>

为true时

true的时候是根据目标类来创建,false是根据接口来创建的,只能通过接口

运行结果:

com.jd.car.CarService

EnhancerBySpringCGLIB

40b982ec

JDK代理:

test类中改为:

ICarService carService = application.getBean(ICarService.class);xml中改为:

<aspectj-autoproxy>

运行结果:com.sun.proxy.$Proxy11得证!

<code>关注私信回复:555领取Java高级架构资料、Spring源码分析、Dubbo、Redis、Netty、zookeeper、Spring cloud、分布式等/<code>


分享到:


相關文章: