分为静态代理和动态代理,动态代理分为jdk和proxy两种方法,proxy又分为无接口代理和有接口代理。下面将一一讲解他们的实现。
1.静态代理模式:
概念:代理类在程序运行前已经定义好。
实现:
(1)定义业务接口
//主业务接口
public interface ISomeService {
//目标方法
String doFirst();
//目标方法
void doSecond();
}
(2)定义目标类SomeServiceImpl,该类实现业务接口。
//目标类
public class SomeServiceImpl implements ISomeService {
public String doFirst() {
System.out.println("执行doFirst()方法");
return "abcde";
}
public void doSecond() {
// TODO Auto-generated method stub
System.out.println("执行doSecond()方法");
}
}
(3)定义代理类SomeServiceProxy,该类要实现ISomeService接口,在构造方法里要定义或者通过有参构造器接受目标对象,目标对象引入代理类,然后加强。
//代理类
public class SomeServiceProxy implements ISomeService {
private SomeServiceImpl target;
public SomeServiceProxy() {
super();
//创建目标对象
target = new SomeServiceImpl();
}
public String doFirst() {
//代理类调用目标方法
String result = target.doFirst();
//增加就发生在这里
return result.toUpperCase(); //转为大写字母
}
public void doSecond() {
target.doSecond();
}
}
(4)定义测试类MyTest,创建代理对象,执行结果。
public class MyTest {
public static void main(String[] args) {
ISomeService service=new SomeServiceProxy();
String result=service.doFirst();
System.out.println("result="+result);
service.doSecond();
}
}
2.JDK动态代理
概念:动态代理指,程序在整个运行过程中根本就不存在目标类的代理,目标对象的代理对象只是由代理工具在程序运行时由JVM根据反射机制动态生成的。
实现:
(1)定义业务接口,与上同
(2)定义目标类,与上同
(3)定义测试类
public class MyTest {
public static void main(String[] args) {
final ISomeService target=new SomeServiceImpl();
//使用jdk的proxy动态代理,要求目标类必须实现接口
//因为其底层的执行原理,与静态的相同
ISomeService service=(ISomeService) Proxy.newProxyInstance(
target.getClass().getClassLoader(), //目标类的类加载器
target.getClass().getInterfaces(), //目标类所实现的所有接口
new InvocationHandler() { //匿名内部类
//proxy:代理对象
//method:目标方法
//tags:目标方法的参数列表
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
//调用目标方法
Object result = method.invoke(target, args);
if (result!=null) {
result = ((String) result).toUpperCase();
}
return result;
}
});
String result=service.doFirst();
System.out.println("result="+result);
service.doSecond();
}
}
3.CGLIB动态代理(无接口)
使用JDK的proxy实现代理,要求目标类与代理类实现相同的接口。若目标类不存在接口,则无法用该方法实现。
但对于无接口的类,要为其创建动态代理,就要用CGLIB来实现。cglib代理的生成原理是生成目标类的子类,而子类是增强过的,这个子类对象就是代理对象。所以,使用cglib生成动态代理,要求目标类必须能够被继承,即不能是final的类。
实现:
(1)定义目标类
//目标类
public class SomeService {
public String doFirst() {
System.out.println("执行doFirst()方法");
return "abcde";
}
public void doSecond() {
// TODO Auto-generated method stub
System.out.println("执行doSecond()方法");
}
}
(2)定义代理工厂类MyCglibFactory
//MyCglibFactory实现了MethodInterceptor接口,MethodInterceptor继承了Callback,也就是MyCglibFactory实现了Callback
//此处ctrl+MethodInterceptor查看是否连接源码
public class MyCglibFactory implements MethodInterceptor{
private SomeService target;
public MyCglibFactory() {
super();
target = new SomeService();
}
public SomeService myCglibCreator(){
//创建增强器对象
Enhancer enhancer = new Enhancer();
//指定目标类,即父类
enhancer.setSuperclass(SomeService.class);
//设置回调接口对象 此方法需要实现MenthodInterceptor
enhancer.setCallback(this);
return (SomeService) enhancer.create();
}
//回调方法
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
Object result = method.invoke(target, args);
if (result!=null) {
result = ((String) result).toUpperCase();
}
return result;
}
}
其中使用Enhancer需要实现MethodInterceptor用到cglib-full.jar包
在实现回调方法时需要连接MethodInterceptor的源码,连接方式可以ctrl+鼠标点击MethodInterceptor,直接点击add然后连接cglib-src-2.2.jar包即可。
(3)定义测试类
public class MyTest {
public static void main(String[] args) {
SomeService service=new MyCglibFactory().myCglibCreator();
String result=service.doFirst();
System.out.println("result="+result);
service.doSecond();
}
}
4.CGLIB动态代理(有接口)
它与无接口的在实现方法上没什么区别,就是把无接口的目标类改成了有接口的目标类而已。其他完全一样。
实现:
(1)定义业务接口,与上同
(2)定义目标类,与上同
(3)定义代理工厂
//MyCglibFactory实现了MethodInterceptor接口,MethodInterceptor继承了Callback,也就是MyCglibFactory实现了Callback
//此处ctrl+MethodInterceptor查看是否连接源码
public class MyCglibFactory implements MethodInterceptor{
private ISomeService target;
public MyCglibFactory() {
super();
target = new SomeServiceImpl();
}
public ISomeService myCglibCreator(){
//创建增强器对象
Enhancer enhancer = new Enhancer();
//指定目标类,即父类
enhancer.setSuperclass(ISomeService.class);
//设置回调接口对象 此方法需要实现MenthodInterceptor
enhancer.setCallback(this);
return (ISomeService) enhancer.create();
}
//回调方法
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
Object result = method.invoke(target, args);
if (result!=null) {
result = ((String) result).toUpperCase();
}
return result;
}
}
(4)定义测试类
public class MyTest {
public static void main(String[] args) {
ISomeService service=new MyCglibFactory().myCglibCreator();
String result=service.doFirst();
System.out.println("result="+result);
service.doSecond();
}
}