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>


分享到:


相關文章: