动态代理
代理模式的解释:为其他对象提供一种代理以控制对这个对象的访问,增强一个类中的某个方法,对程序进行扩展。
cglib动态代理 方式一:
public class UserService {
public void test() {
System.out.println("test...");
}
}
UserService target = new UserService();
// 通过cglib技术
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(UserService.class);
// 定义额外逻辑,也就是代理逻辑
enhancer.setCallbacks(new Callback[]{new MethodInterceptor() {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("before...");
Object result = methodProxy.invoke(target, objects);
System.out.println("after...");
return result;
}
}});
// 动态代理所创建出来的UserService对象
UserService userService = (UserService) enhancer.create();
// 执行这个userService的test方法时,就会额外会执行一些其他逻辑
userService.test();
执行结果:
cglib动态代理 方式二:
声明两个不同的方法
public class UserService {
public void test() {
System.out.println("test...");
}
public void a() {
System.out.println("aaaaa...");
}
}
添加无任何逻辑的拦截器:NoOp.INSTANCE
然后执行不同的代理方法test() 和a()
public static void main(String[] args) {
UserService target = new UserService();
// 通过cglib技术
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(UserService.class);
// 定义额外逻辑,也就是代理逻辑
enhancer.setCallbacks(new Callback[]{new MethodInterceptor() {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("before...");
// Object result = methodProxy.invoke(target, objects);
// Object result = methodProxy.invokeSuper(o, objects);
Object result = method.invoke(target, objects);
System.out.println("after...");
return result;
}
}, NoOp.INSTANCE}); //NoOp.INSTANCE为无任何逻辑的拦截器
enhancer.setCallbackFilter(new CallbackFilter() {
@Override
public int accept(Method method) {
if(method.getName().equals("test")){
return 0; //该数字为上面数组拦截器的下标
}else {
return 1;
}
}
});
// 动态代理所创建出来的UserService对象
UserService userService = (UserService) enhancer.create();
// 执行这个userService的test方法时,就会额外会执行一些其他逻辑
userService.test();
//userService.a();
}
当代理执行 userService.test();时,结果:
当代理执行 userService.a();时,结果:
JDK动态代理:
基于接口的动态代理
public interface UserInterface {
public void test();
}
public class UserService implements UserInterface {
public void test() {
System.out.println("test...");
}
}
public static void main(String[] args) {
UserService target = new UserService();
// UserInterface接口的代理对象
Object proxy = Proxy.newProxyInstance(UserService.class.getClassLoader(), new Class[]{UserInterface.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("before...");
Object result = method.invoke(target, args);
System.out.println("after...");
return result;
}
});
UserInterface userService = (UserInterface) proxy;
userService.test();
}
执行结果:
Spring中的动态代理:
ProxyFactory
public class UserService {
public void test() {
System.out.println("test...中奖了");
}
public void a() {
System.out.println("aaaaa...");
}
}
public static void main(String[] args) {
UserService target = new UserService();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(target);
proxyFactory.setInterfaces(UserInterface.class);
proxyFactory.addAdvice(new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("before...买彩票");
Object result = invocation.proceed();
System.out.println("after...兑换成功");
return result;
}
});
UserInterface userService = (UserInterface) proxyFactory.getProxy();
userService.test();
}
执行结果:
Advice的分类
Before Advice:方法之前执行
After returning advice:方法return后执行
After throwing advice:方法抛异常后执行
After (finally) advice:方法执行完finally之后执行,这是最后的,比return更后
Around advice:这是功能最强大的Advice,可以自定义执行顺序
public class UserService {
public void test() {
System.out.println("test...中奖了");
}
public void a() {
System.out.println("aaaaa...");
}
}
Before Advice:方法之前执行
After returning advice:方法return后执行
After (finally) advice:方法执行完finally之后执行,这是最后的,比return更后
Around advice:这是功能最强大的Advice,可以自定义执行顺序
public class ZsjBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("中奖之前,先去买财票");
}
}
public class ZsjAfterAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("中奖后去兑换财票,换钱");
}
}
public static void main(String[] args) {
UserService target = new UserService();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(target);
proxyFactory.setInterfaces(UserInterface.class);
proxyFactory.addAdvice(new ZsjBeforeAdvice());
proxyFactory.addAdvice(new ZsjAfterAdvice());
UserInterface userService = (UserInterface) proxyFactory.getProxy();
userService.test();
}
执行结果:
After throwing advice:方法抛异常后执行
public static void main(String[] args) {
UserService target = new UserService();
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(target);
proxyFactory.setInterfaces(UserInterface.class);
proxyFactory.addAdvice(new ZsjBeforeAdvice());
proxyFactory.addAdvice(new ZsjAfterAdvice());
proxyFactory.addAdvice(new ZsjThrowAdvice());
UserInterface userService = (UserInterface) proxyFactory.getProxy();
userService.a();
}
public class ZsjThrowAdvice implements ThrowsAdvice {
public void afterThrowing(Method method, Object[] objects, Object target,NullPointerException e){
System.out.println("方法抛出异常后执行");
}
}
执行结果: