aop的四种增强以及JDK动态代理、Cglib动态代理
动态代理 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动态代理相关推荐
- Java设计模式(五)代理设计模式—静态代理—JDK动态代理—Cglib动态代理
文章目录 什么是代理模式 代理模式应用场景 代理的分类 静态代理 什么是静态代理 深入解析静态代理 小结 动态代理 什么是动态代理 JDK动态代理 原理和实现方式 代码实现 优缺点 Cglib动态代理 ...
- java jdk动态代理 cglib动态代理demo
最近在研究java动态代理这块,以前也看了很多次java动态代理,感觉一直不是怎么明白,这两天看了看又明白了些,现给出我参考网上写的一个demo jdk动态代理实现: View Code import ...
- 静态代理,cglib动态代理,jdk动态代理区别以及流程详解
1.静态代理 静态代理使用的是代理设计模式,不讲高大上的思想,我们直接实战 这是动物接口,其中有一个吃饭方法 这是其中的一只动物,实现了动物接口,覆盖了吃饭方法 现在我们思考,我想要给猫找一个代理,希 ...
- 动态代理——CGLIB动态代理原理示例解析
觉得可以的话点个关注,转个发呗,陆续奉上干货~~~~ 前文我们讲解了JDK动态代理的原理(动态代理--JDK动态代理原理),今天我们来看看CGLIB动态代理是如何实现,最后我们总结下JDK动态代理和C ...
- Spring AOP的两种动态代理方式的原理和实现(JDK和CGLIB)
代理机制 Spring 的AOP 部分使用使用JDK动态代理,部分使用CGLIB来为目标对象创建代理.如果被代理的目标对象实现了至少一个接口,则会使用JDK动态代理:如果目标对象没有实现任何接口,则会 ...
- Java动态代理的两种实现方法:JDK动态代理和CGLIB动态代理
Java动态代理的两种实现方法:JDK动态代理和CGLIB动态代理 代理模式 JDK动态代理 CGLIB动态代理 代理模式 代理模式是23种设计模式的一种,指一个对象A通过持有另一个对象B,可以具有B ...
- Java两种动态代理JDK动态代理和CGLIB动态代理
目录 代理模式 JDK动态代理 cglib动态代理 测试 代理模式 代理模式是23种设计模式的一种,他是指一个对象A通过持有另一个对象B,可以具有B同样的行为的模式.为了对外开放协议,B往往实现了一个 ...
- Java之代理(jdk静态代理,jdk动态代理,cglib动态代理,aop,aspectj)
Java之代理... 1 一. 概念... 1 二. jdk的静态代理... 1 三. jdk动态代理... 4 四. cglib 动态 ...
- AOP常用的几种增强方式,各自的特点
SpringAOP的5种增强类型应用讲解 一.前言 spring框架中为我们提供的增强包括针对切面的增强和针对切入点的增强,对一个方法的增强底层使用的是动态代理,所以在学习springAop增强之前大 ...
最新文章
- Unknown host 'android.oa.com'. You may need to adjust the proxy settings in Gradle.
- 电脑下载python多少位的在哪看-python64位
- 算法学习的链接(持续更新)
- 【温故知新】CSS学习笔记(开发者工具介绍)
- VS2010调试时,对于一些语句不能单步运行也不能对变量添加监视的问题
- 检查Red Hat JBoss BRMS部署架构的规则和事件(第二部分)
- STM32-串口接收、发送数据实验-程序代码分析
- eclipse+ADT 进行android应用签名详解
- 用VS2013如何编写C语言
- 设计灵感|电商版面首页界面设计案例
- 表达式必须是常量表达式
- selenium遇到的问题记录
- 流媒体播放器EasyPlayer.js如何实现动态设置解码H.265音频?
- java 非科学计数法_Java设置大数非科学计数法显示
- 快速记忆之简单词语联想记忆
- 内嵌html5,显示:内嵌HTML5元素
- 在网页中调用iOS客户端
- 常用的设计模式(七)——门面设计模式
- moment获取近期时间
- 数据库实验第五周【数据查询】
热门文章
- MPLS *** 高级教程(张洋讲解演示版)
- 寻找兄弟单词(2012.5.6百度实习)
- Symbian的内存管理机制
- 一个WEB***的处理过程
- PostgreSQL 10.1 手册_部分 II. SQL 语言_第 10 章 类型转换_10.4. 值存储
- c++11 线程:让你的多线程任务更轻松
- Apache的性能优化
- 2007年IT企业定向培养就业班
- 认识RAID磁盘阵列
- CoinEx将于8月6日开启Spice加速器申购