//JDK动态代理基于接口实现,所以实现JDK动态代理,必须先定义接口JDK代理与目标类没有关系
配置如下:
application.xml文件中
<aop:aspectj-autoproxy proxy-target-class="false"/>
Test:
package com.jd.test;
import org.springframework.cglib.core.DebuggingClassWriter;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jd.calculator.IUserInfoService;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
ICalculatorService calculatorService =applicationContext.getBean(IUserInfoService.class);//不能写UserInfo.class
Class clazz = calculatorService.getClass();
System.out.println(clazz.getName());
applicationContext.close();
}
}
//CGLIB代理类继承目标类
application.xml文件中
<aop:aspectj-autoproxy proxy-target-class="true"/>
Test:
package com.jd.test;
import org.springframework.cglib.core.DebuggingClassWriter;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jd.calculator.IUserInfoService;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
ICalculatorService calculatorService =applicationContext.getBean(UserInfoService.class);//因为代理类继承了目标类即可以调用实现接口
Class clazz = calculatorService.getClass();
System.out.println(clazz.getName());
applicationContext.close();
}
}