动态代理 AOP底层实现:有接口自动应用的就是JDK动态代理
(1).JDK 在运行时运行时注入
本质:在内存中构建出接口的实现类
特点:被代理对象,必须有接口

实例:

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class Test {public static void main(String[] args) {final SomeService service = new SomeServiceImpl();SomeService proxy =(SomeService)Proxy.newProxyInstance(service.getClass().getClassLoader(),service.getClass().getInterfaces(), new InvocationHandler() {public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println("before=======");method.invoke(service,args);                      System.out.println("after========");return null;}});proxy.doSome();}
}

Cglib 底层,注入,编译期已经注入了
本质:在内存中生成被代理类(目标类)的【子类】
特点:可以在没有接口的情况下代理
对于不使用接口的业务类,无法使用JDK动态代理,cglib采用非常底层的字节码技术,
可以为一个类创建子类

import cn.jdkdl.SomeServiceImpl;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;import java.lang.reflect.Method;public class Test {public static void main(String[] args) {final SomeServiceImpl someService = new SomeServiceImpl();Enhancer enhancer = new Enhancer();enhancer.setSuperclass(someService.getClass());enhancer.setCallback(new MethodInterceptor() {public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {System.out.println("=======before");methodProxy.invoke(someService,objects);return null;}});SomeServiceImpl proxy = (SomeServiceImpl) enhancer.create();proxy.doSome();}
}

接下来是四种增强

前置增强 MethodBeforeAdvice

import org.springframework.aop.AfterReturningAdvice;import java.lang.reflect.Method;public class AfterAdvice implements AfterReturningAdvice {public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {System.out.println("after========");}
}

<bean id="service" class="cn.jdkdl.SomeServiceImpl"></bean><bean id="beforeAdvice" class="cn.jdkdl.BeforeAdvice"></bean><bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="target" ref="service"></property><property name="interceptorNames" value="beforeAdvice"></property></bean>

后置增强 AfterReturingAdvice

import org.springframework.aop.AfterReturningAdvice;import java.lang.reflect.Method;public class AfterAdvice implements AfterReturningAdvice {public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {System.out.println("after========");}
}

xml文件内容

   <bean id="service" class="cn.jdkdl.SomeServiceImpl"></bean><bean id="afterAdvice" class="cn.jdkdl.AfterAdvice"></bean><bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="target" ref="service"></property><property name="interceptorNames" value="afterAdvice"></property></bean>

3.环绕增强

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class HrAdvice implements MethodInterceptor {public Object invoke(MethodInvocation methodInvocation) throws Throwable {System.out.println("before====");methodInvocation.proceed();System.out.println("after====");return null;}
}

xml文件

   <bean id="service" class="cn.jdkdl.SomeServiceImpl"></bean><bean id="hrAdvice" class="cn.jdkdl.HrAdvice"></bean><bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="target" ref="service"></property><property name="interceptorNames" value="hrAdvice"></property></bean>

异常增强

public class MyThrowsAdvice implements ThrowsAdvice {public void afterThrowing(Exception e){System.out.println("网络异常......");}
}

   <bean id="service" class="cn.jdkdl.SomeServiceImpl"></bean><bean id="throwsAdvice" class="cn.jdkdl.MyThrowsAdvice"></bean><bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="target" ref="service"></property><property name="interceptorNames" value="throwsAdvice"></property></bean>

转载于:https://www.cnblogs.com/ztm1021810064/p/8539083.html

aop的四种增强以及JDK动态代理、Cglib动态代理相关推荐

  1. Java设计模式(五)代理设计模式—静态代理—JDK动态代理—Cglib动态代理

    文章目录 什么是代理模式 代理模式应用场景 代理的分类 静态代理 什么是静态代理 深入解析静态代理 小结 动态代理 什么是动态代理 JDK动态代理 原理和实现方式 代码实现 优缺点 Cglib动态代理 ...

  2. java jdk动态代理 cglib动态代理demo

    最近在研究java动态代理这块,以前也看了很多次java动态代理,感觉一直不是怎么明白,这两天看了看又明白了些,现给出我参考网上写的一个demo jdk动态代理实现: View Code import ...

  3. 静态代理,cglib动态代理,jdk动态代理区别以及流程详解

    1.静态代理 静态代理使用的是代理设计模式,不讲高大上的思想,我们直接实战 这是动物接口,其中有一个吃饭方法 这是其中的一只动物,实现了动物接口,覆盖了吃饭方法 现在我们思考,我想要给猫找一个代理,希 ...

  4. 动态代理——CGLIB动态代理原理示例解析

    觉得可以的话点个关注,转个发呗,陆续奉上干货~~~~ 前文我们讲解了JDK动态代理的原理(动态代理--JDK动态代理原理),今天我们来看看CGLIB动态代理是如何实现,最后我们总结下JDK动态代理和C ...

  5. Spring AOP的两种动态代理方式的原理和实现(JDK和CGLIB)

    代理机制 Spring 的AOP 部分使用使用JDK动态代理,部分使用CGLIB来为目标对象创建代理.如果被代理的目标对象实现了至少一个接口,则会使用JDK动态代理:如果目标对象没有实现任何接口,则会 ...

  6. Java动态代理的两种实现方法:JDK动态代理和CGLIB动态代理

    Java动态代理的两种实现方法:JDK动态代理和CGLIB动态代理 代理模式 JDK动态代理 CGLIB动态代理 代理模式 代理模式是23种设计模式的一种,指一个对象A通过持有另一个对象B,可以具有B ...

  7. Java两种动态代理JDK动态代理和CGLIB动态代理

    目录 代理模式 JDK动态代理 cglib动态代理 测试 代理模式 代理模式是23种设计模式的一种,他是指一个对象A通过持有另一个对象B,可以具有B同样的行为的模式.为了对外开放协议,B往往实现了一个 ...

  8. Java之代理(jdk静态代理,jdk动态代理,cglib动态代理,aop,aspectj)

    Java之代理... 1 一.         概念... 1 二.         jdk的静态代理... 1 三.         jdk动态代理... 4 四.         cglib 动态 ...

  9. AOP常用的几种增强方式,各自的特点

    SpringAOP的5种增强类型应用讲解 一.前言 spring框架中为我们提供的增强包括针对切面的增强和针对切入点的增强,对一个方法的增强底层使用的是动态代理,所以在学习springAop增强之前大 ...

最新文章

  1. Unknown host 'android.oa.com'. You may need to adjust the proxy settings in Gradle.
  2. 电脑下载python多少位的在哪看-python64位
  3. 算法学习的链接(持续更新)
  4. 【温故知新】CSS学习笔记(开发者工具介绍)
  5. VS2010调试时,对于一些语句不能单步运行也不能对变量添加监视的问题
  6. 检查Red Hat JBoss BRMS部署架构的规则和事件(第二部分)
  7. STM32-串口接收、发送数据实验-程序代码分析
  8. eclipse+ADT 进行android应用签名详解
  9. 用VS2013如何编写C语言
  10. 设计灵感|电商版面首页界面设计案例
  11. 表达式必须是常量表达式
  12. selenium遇到的问题记录
  13. 流媒体播放器EasyPlayer.js如何实现动态设置解码H.265音频?
  14. java 非科学计数法_Java设置大数非科学计数法显示
  15. 快速记忆之简单词语联想记忆
  16. 内嵌html5,显示:内嵌HTML5元素
  17. 在网页中调用iOS客户端
  18. 常用的设计模式(七)——门面设计模式
  19. moment获取近期时间
  20. 数据库实验第五周【数据查询】

热门文章

  1. MPLS *** 高级教程(张洋讲解演示版)
  2. 寻找兄弟单词(2012.5.6百度实习)
  3. Symbian的内存管理机制
  4. 一个WEB***的处理过程
  5. PostgreSQL 10.1 手册_部分 II. SQL 语言_第 10 章 类型转换_10.4. 值存储
  6. c++11 线程:让你的多线程任务更轻松
  7. Apache的性能优化
  8. 2007年IT企业定向培养就业班
  9. 认识RAID磁盘阵列
  10. CoinEx将于8月6日开启Spice加速器申购