詳述JDK代理與CGLib代理區別

一、JDK代理與CGLib代理區別

1、JDK代理:
只能對實現了接口的類生成代理,而不能針對類。

2、CGLib代理:
針對類實現代理,主要是對指定的類生成一個子類,覆蓋其中的方法,並覆蓋其中方法(繼承),因爲是繼承,所以該類或方法最好不要聲明成final, 對於final類或方法,是無法繼承的。

3、何時使用JDK代理與CGLib代理?
①目標對象實現了接口,默認採用JDK代理實現AOP,此時可以強制使用CGLib代理實現AOP。
②目標對象沒有實現了接口,必須強制使用CGLib代理實現AOP。

4、如何強制使用CGLib代理實現AOP?
①添加CGLib庫。
②在Spring配置文件中加入<aop:aspectj-autoproxy proxy-target-class="true"/>

二、舉例

1、接口IComputerService.java:

package com.jd.computer.service;

public interface IComputerService {

	int div(int a, int b);
}

2、接口實現類ComputerService.java:

package com.jd.computer.service;

import org.springframework.stereotype.Service;

@Service
public class ComputerService implements IComputerService {

	public int div(int a, int b) {
		return a/b;
	}
}

3、MethodAOP.java:

package com.jd.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;

public class MethodAOP {

	public void before(JoinPoint jp) {
		Object [] args = jp.getArgs();
		Signature signature = jp.getSignature();
		String name = signature.getName();
		System.out.println("The "+name+" method begins.");
		System.out.println("The "+name+" method params ["+args[0]+","+args[1]+"].");
	}
}

4、String配置文件application.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<context:component-scan base-package="com.jd"></context:component-scan>
	
	<bean class="com.jd.aop.MethodAOP" id="ma"></bean>
	
	<aop:config>
		<aop:pointcut expression="execution(public int com.jd.computer.service.ComputerService.*(..))" id="pc"/>
		<aop:aspect ref="ma">
			<aop:before method="before" pointcut-ref="pc"/>
		</aop:aspect>
	</aop:config>
	
	<!-- proxy-target-class默認爲false,使用JDK代理,爲true時使用CGLib代理 -->
	<aop:aspectj-autoproxy proxy-target-class="false"/>
</beans>

5、測試類Test.java:
①使用JDK代理,<aop:aspectj-autoproxy proxy-target-class="false"/>

package com.jd.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jd.computer.service.IComputerService;

public class Test {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
		//JDK代理,getBean()中必須是接口IComputerService.class,不能是目標類ComputerService.class。
		IComputerService computerService = applicationContext.getBean(IComputerService.class);
		Class clazz = computerService.getClass();
		//JDK代理:代理類和目標類沒有繼承關係,clazz.getSuperclass().getName()得到父類的類名
		System.out.println(clazz.getSuperclass().getName());
		applicationContext.close();
	}
}

輸出如下,可以看出此時運用JDK代理,沒有繼承關係:

java.lang.reflect.Proxy

②使用CGLib代理,<aop:aspectj-autoproxy proxy-target-class="true"/>,

package com.jd.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jd.computer.service.ComputerService;
import com.jd.computer.service.IComputerService;

public class Test {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
		//CGLib代理,getBean()可以是接口IComputerService.class或者目標類ComputerService.class
		IComputerService computerService = applicationContext.getBean(ComputerService.class);
		Class clazz = computerService.getClass();
		//CGLib代理:代理類繼承自目標類
		System.out.println(clazz.getSuperclass().getName());
		applicationContext.close();
	}
}

輸出如下,可以看出運用CGLib代理,有繼承關係:

com.jd.computer.service.ComputerService
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章