详述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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值