动态代理中有两个重要的接口,一个是InvocationHandle,另一个是Proxy。
分别来说明这两个接口的作用吧!
InvocationHandle接口
java.lang.reflect.InvocationHandler

InvocationHandler 是代理实例的调用处理程序 实现的接口。每个代理实例都具有一个关联的调用处理程序。对代理实例调用方法时,将对方法调用进行编码并将其指派到它的调用处理程序的 invoke 方法。简单来说invoke就是在在代理实例上处理方法调用并返回结果。

invoke方法介绍:

参数:
proxy - 在其上调用方法的代理实例也就是该参数是代理的真实对象
method - 对应于在代理实例上调用的接口方法的 Method 实例。Method 对象的声明类将是在其中声明方法的接口,该接口可以是代理类赖以继承方法的代理接口的超接口。
args - 包含传入代理实例上方法调用的参数值的对象数组,如果接口方法不使用参数,则为 null。基本类型的参数被包装在适当基本包装器类(如 java.lang.Integer 或 java.lang.Boolean)的实例中。
返回:
从代理实例的方法调用返回的值。如果接口方法的声明返回类型是基本类型,则此方法返回的值一定是相应基本包装对象类的实例;否则,它一定是可分配到声明返回类型的类型。如果此方法返回的值为 null 并且接口方法的返回类型是基本类型,则代理实例上的方法调用将抛出 NullPointerException。否则,如果此方法返回的值与上述接口方法的声明返回类型不兼容,则代理实例上的方法调用将抛出 ClassCastException。
抛出:
Throwable - 从代理实例上的方法调用抛出的异常。该异常的类型必须可以分配到在接口方法的 throws 子句中声明的任一异常类型或未经检查的异常类型 java.lang.RuntimeException 或 java.lang.Error。如果此方法抛出经过检查的异常,该异常不可分配到在接口方法的 throws 子句中声明的任一异常类型,代理实例的方法调用将抛出包含此方法曾抛出的异常的 UndeclaredThrowableException。

invoke代码实例

    }
/*  1 proxy,该参数是代理的真实对象 2 method,该参数是代理的方法 3 代理方法中接受的参数 */public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println("初始化");Object invoke = method.invoke(us, args);System.out.println("执行完毕");return invoke;}

Proxy接口
java.lang.reflect.Proxy

Proxy 提供用于创建动态代理类和实例的静态方法,它还是由这些方法创建的所有动态代理类的超类。

Proxy.newProxyInstance方法:
public static Object newProxyInstance(ClassLoader loader,Class<?>[] interfaces,InvocationHandler h)throws IllegalArgumentException返回一个指定接口的代理类实例,该接口可以将方法调用指派到指定的调用处理程序。此方法相当于: Proxy.getProxyClass(loader, interfaces).getConstructor(new Class[] { InvocationHandler.class }).newInstance(new Object[] { handler });Proxy.newProxyInstance 抛出 IllegalArgumentException,原因与 Proxy.getProxyClass 相同。 参数:
loader - 定义代理类的类加载器
interfaces - 代理类要实现的接口列表
h - 指派方法调用的调用处理程序
返回:
一个带有代理类的指定调用处理程序的代理实例,它由指定的类加载器定义,并实现指定的接口

代码实例:

定义一个代理的接口和类:
package com.proxy;public interface UserService {void Run();
}
package com.proxy;
接口的实现类:
public class UserServiceImpl implements UserService {public void Run() {System.out.println("运行啦!");}}
动态代理类:
package com.proxy;import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;public class UserServiceProxyFactory implements InvocationHandler{public UserService us;public UserServiceProxyFactory(UserService us){this.us=us;}public UserService getProxyUserServiceInstance(){UserService newProxyInstance = (UserService) Proxy.newProxyInstance(UserServiceProxyFactory.class.getClassLoader(), UserServiceImpl.class.getInterfaces(), this);return newProxyInstance;}
/*  1 proxy,该参数是代理的真实对象 2 method,该参数是代理的方法 3 代理方法中接受的参数 */public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println("初始化");Object invoke = method.invoke(us, args);System.out.println("执行完毕");return invoke;}
}测试类:
package com.proxy;import org.junit.Test;public class UserServiceDemo {@Testpublic void run(){UserService us = new UserServiceImpl();us.Run();UserServiceProxyFactory proxy = new UserServiceProxyFactory(us);us = proxy.getProxyUserServiceInstance();us.Run();}
}

但是在Spring中已容器已经帮我们封装好了方法,只需要配置注解就可以实现如上动态代理

方法一、xml配置
1.导包 (4+2) spring的aop包有两个需要用到:aop、aspect(lib目录下的包) Spring需要第三方的AOP包(在依赖包目录下)com.springsource.org.aopalliance、和com.springsource.org.aspectj.weaver目录下的包
2.准备目标对象

接口和实现类

package cn.itcast.service;public interface UserService {void save();void delete();void update();void find();
}
package cn.itcast.service;public class UserServiceImpl implements UserService {@Overridepublic void save() {System.out.println("保存用户!");//int i = 1/0;}@Overridepublic void delete() {System.out.println("删除用户!");}@Overridepublic void update() {System.out.println("更新用户!");}@Overridepublic void find() {System.out.println("查找用户!");}
}

3.准备通知

package cn.itcast.e_annotationaop;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;//通知类
@Aspect
//表示该类是一个通知类
public class MyAdvice {//如果我们不想每次@Around("execution(* cn.itcast.service.*ServiceImpl.*(..))")这样引入方法,我们可以把方法放入切点,然后切点注解在自己新添加的一个方法上,这样只需要@Arround(MyAdvice.pc())就会去加载方法@Pointcut("execution(* cn.itcast.service.*ServiceImpl.*(..))")public void pc(){}//前置通知//指定该方法是前置通知,并制定切入点@Before("MyAdvice.pc()")public void before(){System.out.println("这是前置通知!!");}//后置通知@AfterReturning("execution(* cn.itcast.service.*ServiceImpl.*(..))")public void afterReturning(){System.out.println("这是后置通知(如果出现异常不会调用)!!");}//环绕通知@Around("execution(* cn.itcast.service.*ServiceImpl.*(..))")public Object around(ProceedingJoinPoint pjp) throws Throwable {System.out.println("这是环绕通知之前的部分!!");Object proceed = pjp.proceed();//调用目标方法System.out.println("这是环绕通知之后的部分!!");return proceed;}//异常通知@AfterThrowing("execution(* cn.itcast.service.*ServiceImpl.*(..))")public void afterException(){System.out.println("出事啦!出现异常了!!");}//后置通知@After("execution(* cn.itcast.service.*ServiceImpl.*(..))")public void after(){System.out.println("这是后置通知(出现异常也会调用)!!");}
}

4.配置进行织入,将通知织入目标对象中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "><!-- 准备工作: 导入aop(约束)命名空间 -->
<!-- 1.配置目标对象 --><bean name="userService" class="cn.itcast.service.UserServiceImpl" ></bean>
<!-- 2.配置通知对象 --><bean name="myAdvice" class="cn.itcast.d_springaop.MyAdvice" ></bean>
<!-- 3.配置将通知织入目标对象 --><aop:config><!-- 配置切入点:expression中的表达式表示目标对象userServiceImpl的save方法public void cn.itcast.service.UserServiceImpl.save() void cn.itcast.service.UserServiceImpl.save()* cn.itcast.service.UserServiceImpl.save()* cn.itcast.service.UserServiceImpl.*()* cn.itcast.service.*ServiceImpl.*(..)* cn.itcast.service..*ServiceImpl.*(..)--><aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="pc"/><aop:aspect ref="myAdvice" ><!-- 指定名为before方法作为前置通知 --><aop:before method="before" pointcut-ref="pc" /><!-- 后置 --><aop:after-returning method="afterReturning" pointcut-ref="pc" /><!-- 环绕通知 --><aop:around method="around" pointcut-ref="pc" /><!-- 异常拦截通知 --><aop:after-throwing method="afterException" pointcut-ref="pc"/><!-- 后置 --><aop:after method="after" pointcut-ref="pc"/></aop:aspect></aop:config>
</beans>

5.测试类以及结果

package cn.itcast.e_annotationaop;import javax.annotation.Resource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import cn.itcast.bean.User;
import cn.itcast.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:cn/itcast/e_annotationaop/applicationContext.xml")
public class Demo {@Resource(name="userService")private UserService us;@Testpublic void fun1(){us.save();}}

Result:

这是环绕通知之前的部分!!
这是前置通知!!
保存用户!
这是环绕通知之后的部分!!
这是后置通知(出现异常也会调用)!!
这是后置通知(如果出现异常不会调用)!!

方法二、注入配置
导包过程、目标对象和通知类如上一样,只是不需要都在xml中手动配置,在java代码中注入配置
通知类中的配置:

package cn.itcast.e_annotationaop;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;//通知类
@Aspect
//表示该类是一个通知类
public class MyAdvice {@Pointcut("execution(* cn.itcast.service.*ServiceImpl.*(..))")public void pc(){}//前置通知//指定该方法是前置通知,并制定切入点@Before("MyAdvice.pc()")public void before(){System.out.println("这是前置通知!!");}//后置通知@AfterReturning("execution(* cn.itcast.service.*ServiceImpl.*(..))")public void afterReturning(){System.out.println("这是后置通知(如果出现异常不会调用)!!");}//环绕通知@Around("execution(* cn.itcast.service.*ServiceImpl.*(..))")public Object around(ProceedingJoinPoint pjp) throws Throwable {System.out.println("这是环绕通知之前的部分!!");Object proceed = pjp.proceed();//调用目标方法System.out.println("这是环绕通知之后的部分!!");return proceed;}//异常通知@AfterThrowing("execution(* cn.itcast.service.*ServiceImpl.*(..))")public void afterException(){System.out.println("出事啦!出现异常了!!");}//后置通知@After("execution(* cn.itcast.service.*ServiceImpl.*(..))")public void after(){System.out.println("这是后置通知(出现异常也会调用)!!");}
}

xml文件中的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "><!-- 准备工作: 导入aop(约束)命名空间 -->
<!-- 1.配置目标对象 --><bean name="userService" class="cn.itcast.service.UserServiceImpl" ></bean>
<!-- 2.配置通知对象 --><bean name="myAdvice" class="cn.itcast.e_annotationaop.MyAdvice" ></bean>
<!-- 3.开启使用注解完成织入 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>

测试类如上所同!

spring面向AOP之动态代理相关推荐

  1. Spring 初识Aop JDK动态代理实现 原理初显

    Spring 初识Aop JDK动态代理实现 原理初显 一.项目结构 二.具体步骤: 1.创建maven项目 创建好包结构 2.写一个TestDao接口 及实现类 3. 写一个自己的切面类 4.jav ...

  2. spring的aop的动态代理机制都有哪些_Spring学习(4):Spring AOP

    Spring AOP说明 AOP(Aspect Oriented Pragraming)面向切面编程,AOP采用横向抽取机制,取代了传统纵向继承体系的重复性代码(性能监视.事务管理.安全检查.缓存). ...

  3. spring 的AOP和动态代理分析

    spring的AOP到底是什么呢? 看看百度怎么说 AOP(Aspect Oriented Programming 面向切面编程),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AO ...

  4. Spring : Spring Aop CGLIB动态代理调用过程

    1.美图 2.概述 CGLIB动态代理参考: CGLIB动态代理 CGLIB原理解析参考:CGLIB原理解析 3.分析 Spring AOP CGLIB动态代理调用过程分析,CGLIB动态代理调用过程 ...

  5. spring—AOP 的动态代理技术

    AOP 的动态代理技术 常用的动态代理技术 JDK 代理 : 基于接口的动态代理技术 cglib 代理:基于父类的动态代理技术 JDK 代理 public class proxy {@Testpubl ...

  6. java中的静态、动态代理模式以及Spring中的CgLib动态代理解读(面试必问)

    java中的静态.动态代理模式以及Spring中的CgLib动态代理解读(面试必问) 静态代理 动态代理 CgLib动态代理     基础知: 反射知识 代理(Proxy)是一种设计模式,提供了对目标 ...

  7. AOP、动态代理、CGlib(原理实践)

    Java 代理模式实现方式,主要有如下五种方法: 1. 静态代理,工程师编辑代理类代码,实现代理模式:在编译期就生成了代理类. 2. 基于 JDK 实现动态代理,通过jdk提供的工具方法Proxy.n ...

  8. SpringBoot/Spring AOP默认动态代理方式

    Spring 5.x中AOP默认依旧使用JDK动态代理 SpringBoot 2.x开始,AOP为了解决使用JDK动态代理可能导致的类型转换异常,而使用CGLIB. 在SpringBoot 2.x中, ...

  9. Spring : Spring Aop JDK动态代理调用过程

    1.美图 2.概述 JDK动态代理参考 : JDK动态代理 3.源码 打开JdkDynamicAopProxy类,查看invoke方法: /*** Implementation of {@code I ...

最新文章

  1. 「程序员做饭指南」霸榜GitHub,还用数学公式解决「吃什么」世纪难题,微软程序员出品...
  2. 人工智能之神经网络要进入的下一个阶段-演绎推理
  3. 如何将注册中心从 Consul 平滑的迁移到 Nacos ?
  4. 02 面向对象之:类空间问题以及类之间的关系
  5. 安装yum软件包的方法
  6. [YTU]_2384 ( 矩形类中运算符重载【C++】)
  7. 【ros】1.ros的安装
  8. ERDAS软件应用(三)遥感图像的拼接
  9. [Win32]一个调试器的实现(四)读取寄存器和内存
  10. presto支持标准sql吗_presto技术文档
  11. 操作系统ppt_华为车BU王军:华为三大汽车操作系统,及跨域集成软件框架(内含PPT)...
  12. 为什么阿里巴巴的市值比京东高,世界500排名比京东靠后?
  13. Android NDK开发之 NEON 矩阵转置,矩阵乘法实现
  14. 【从C到C++学习笔记】C++介绍/推荐书籍/开发工具
  15. 项目分享| 自制巡线机器人
  16. 计算机怎么设置u盘拷贝,如何快速实现电脑u盘拷贝文件?
  17. 用python实现简版区块链-交易(2)
  18. 电视机芯片介绍-海思Hi3751 V600
  19. 阿里西西网页特效代码演示中心-QQ在线客服代码演示
  20. pytorch RuntimeError: size mismatch, m1: [64 x 784], m2: [784 x 10] at

热门文章

  1. caj转pdf python_PDF怎么转换成Word?2种方法3秒一键转换!建议收藏
  2. 现代软件工程系列 学生的精彩文章 (6) 项目总结
  3. 邻接矩阵和邻接表_[力扣743] 带权邻接表的单源最短路
  4. Linux上的JAVA的IDE,在linux上运行的基于文本的强大java IDE
  5. 决策树 prepruning_智能建筑运维前探 AI天天见之五:决策树算法应用探索
  6. 苹果x计算机按键有声音吗,iPhoneX拍照总有声音怎么办?拨动静音键即可关闭拍照声!...
  7. linux 找不到php命令,bash scp:未找到命令的解决方法
  8. php 网站计数器,PHP实现网站访问量计数器
  9. js输出100以内的质数_Python 计数质数
  10. java类验证和装载顺序_Java类加载机制实现流程及原理详解