Java动态代理模拟spring的AOP

      广州疯狂软件学院拥有三大课程体系包括:java课程,android课程,ios课程,疯狂软件年终钜惠,报名java就业班,免费赠送基础班,名额有限,本月火热报名中,欢迎有志之士电话或者咨询。

  spring中的AOP是通过Java的动态代理实现的,有关动态代理的详细解释参见:

  现在就使用Java的动态代理模拟AOP(使用JDK的动态代理)

  1.建立实体对象

  public class User {

  Integer id;

  String name;

  String pwd;

  public User() {

  }

  public User(int id, String name, String pwd) {

  this.id = id;

  this.name = name;

  this.pwd = pwd;

  }

  //setter/getter

  }

  2.建立接口

  public interface UserDao {

  public void addUser(User user);

  }

  3.实现接口

  public class UserDaoImpl implements UserDao{

  @Override

  public void addUser(User user){

  System.out.println("保存User到数据库中");

  throw new RuntimeException();

  }

  }

  4.创建拦截器(这个类就相当于aop中的切面类)

  public class MyInterceptor implements InvocationHandler {

  private Object target;

  public MyInterceptor() {

  }

  public MyInterceptor(Object target) {

  this.target = target;

  }

  public Object getTarget() {

  return target;

  }

  public void setTarget(Object target) {

  this.target = target;

  }

  public void before() {

  System.out.println("这是一个前置通知");

  }

  public void afterReturning() {

  System.out.println("这是一个后置通知");

  }

  public void afterThrowing() {

  System.out.println("这是一个异常通知");

  }

  public void after() {

  System.out.println("这是一个最终通知");

  }

  @Override

  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

  Object returnValue = null;

  try {

  before();

  returnValue = method.invoke(target, args);

  afterReturning();

  } catch (Exception e) {

  afterThrowing();

  } finally {

  after();

  }

  return returnValue;

  }

  }

  5.通过代理对象实现目标方法

  @Test

  public void testAOP() {

  UserDao userDao = new UserDao();

  MyInterceptor interceptor = new MyInterceptor();

  interceptor.setTarget(userDao);

  //通过代理的静态方法创建一个代理对象

  //这个代理创建的时候,接收了接口类型和拦截器的处理类,在代理的背后,他会调运拦截器的invoke这个

  方法。

  //因为传递的有接口类型,所以可以强制类型转换到我们指定的接口类型

  UserDao userDaoProxy = (UserDao) Proxy.newProxyInstance(this.getClass().getClassLoader(),

  userDao.getClass().getInterfaces(),

  interceptor);

  //然后在代理对象处理的时候,就会将我的逻辑织如到被代理的对象上

  userDaoProxy.addUser(new User(1, "a", "b"));

  }

  6.程序输出

  这是一个前置通知

  保存User到数据库中

  这是一个异常通知

  这是一个最终通知

  疯狂Java培训专注软件开发培训,提升学员就业能力,重点提升实践动手能力。技术知识沉淀深厚的老师,让你感受Java的魅力,激发你对于编程的热爱,让你在半年的时间内掌握8-10万的代码量,掌握Java核心技术,成为真正的技术高手;通过大量全真企业项目疯狂训练,迅速积累项目经验。让你成为技能型的现代化高端人才,迅速获得高薪就业!时间不等人,赶紧联系我们吧!疯狂java培训中心地址:广州天河区车陂沣宏大厦3楼。

  疯狂Java培训专注软件开发培训,提升学员就业能力,重点提升实践动手能力。疯狂软件开设了java课程,ios课程,android课程,为你提供一个学习java技能的好机会,疯狂软件特大优惠活动,加疯狂软件微信号(疯狂软件),抢优惠,优惠100元+赠送iOS教材一本 详情请看疯狂java培训官网。IT从业着仍是社会所需要的高端人才,广州疯狂软件之力于培养企业所需要的中高端IT人才,让你成为备受企业青睐的人才。

 

转载于:https://www.cnblogs.com/gojava/p/3498977.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377377.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

xlrd.biffh.XLRDError: Excel xlsx file; not supported解决方法

将原本的xlrd卸载,安装旧版本: pip uninstall xlrd pip install xlrd1.2.0转自:https://www.cnblogs.com/xiaoqiangink/p/14144517.html

生产消费是什么设计模式_快速消费品的完整形式是什么?

生产消费是什么设计模式快消品:快速消费品 (FMCG: Fast-Moving Consumer Goods) FMCG is an abbreviation of Fast-Moving Consumer Goods which are also known as Consumer Packed Goods (CPG). These consumer packed goods allude to the products that are sol…

分类释义概述

分类(classification) 是人工智能领域基本的研究领域之一,也是知识表示和获取的主要途径之一。一般认为,分类属于科学发展的较初级阶段,即形成理论之前的阶段。 分类的释义: 中文解释:分类指的是将无规律的事物按照其性…

占位博客

占位博客 转载于:https://www.cnblogs.com/CharmingDang/p/9663895.html

通过从全局和类内部重载operator new /delete来获取内存管理权

目录1、通过重载获得内存管理权2、容器的内存管理3、重载new、array new、replacement new,接管内存控制权1、重载全局::operator new / ::operator delete以及array版本2、在类里面去重载1、通过重载获得内存管理权 之前的几章学习,是红色的路线。此时…

sml完整形式_教资会的完整形式是什么?

sml完整形式教资会:大学教育资助委员会 (UGC: University Grants Commission) UGC is an abbreviation of the University Grants Commission. It is an organization established by the Indian Union government in agreement with the UGC Act 1956 under the Mi…

ASP.NET线程相关配置

1、ASP.NET 同一时刻只能发起的工作线程数量&#xff1a; (maxWorkerThreads * CPU逻辑数量&#xff09;-minFreeThreads 比如2个CPU默认配置maxWorkerThreads100&#xff0c;minFreeThreads176&#xff0c;则同时最大只能有24个工作线程。&#xff08;这里不管 <system.ne…

Android 编程下 AlarmManager

对应 AlarmManager 有一个 AlarmManagerServie 服务程序&#xff0c;该服务程序才是正真提供闹铃服务的&#xff0c;它主要维护应用程序注册的各类闹铃并适时的设置即将触发的闹铃给闹铃设备 ( 在系统中&#xff0c;Linux 实现的设备名为 ”/dev/alarm” ) &#xff0c;并且一直…

erp开发模式_ERP的完整形式是什么?

erp开发模式ERP&#xff1a;企业资源计划 (ERP: Enterprise Resource Planning) ERP is an abbreviation of Enterprise Resource Planning. It is incorporated business management that is executed by a lot of numerous business houses to enhance their productivity an…

关于placement new 和 placement delete的重载,以及basic_string重载new()实例

关于placement new 在https://blog.csdn.net/qq_42604176/article/details/111997397中已经介绍了placement new的形式。 它的形式为new()/delete().我们将分配好内存的指针送入括号中&#xff0c;就完成了初步的调用了。 其实我们可以定义放任何的东西到()内部。只放一个指针…

cwc云莱特链_CWC的完整形式是什么?

cwc云莱特链CWC&#xff1a;工作条件的改变 (CWC: Change in Working Conditions) CWC is an abbreviation of "Change in Working Conditions". CWC是“工作条件更改”的缩写 。 It is an expression, which is commonly used in the Gmail platform. In a particu…

在eclipse中创建web项目(非myeclipse)

在eclipse中创建web项目(非myeclipse) 在eclipse中如何创建dynamic web project项目 本文的演示是从本地文件创建dynamic web project&#xff0c;从svn检出的同时创建dynamic web project于此类似。我们推荐使用解压版的tomcat6.x版本&#xff0c;来作为服务器。可以到http://…

opengl glut 编译

新建工程glut dll工程&#xff0c;本来想创建lib,工程的&#xff0c;但是想起来&#xff0c;gl是状态机机制。dll方便资源共享等。 添加两个include目录 各种手机电脑平台&#xff0c;网络多媒体开发,mmsplayer&#xff0c;QQ514540005 然后将目录下的lib/glut下面所有的.c文件…

划分数据集代码(按照4:1的比例)以及根据各自文件名写入txt文件

会将图片分到两个文件夹中&#xff1a; #include <opencv2/opencv.hpp> #include "opencv2/features2d.hpp" #include <vector> #include <algorithm> #include <iostream> #include "windows.h" #include <stdio.h> #incl…

bpo是什么意思_BPO的完整形式是什么?

bpo是什么意思BPO&#xff1a;业务流程外包 (BPO: Business Process Outsourcing) BPO is an abbreviation of Business process outsourcing. It is a convention of a company to another company which is an external provider of services or business operations process…

在进行 ASP.NET 开发时,有时候需要对页面输出的最终 HTML 源代码进行控制

在进行 ASP.NET 开发时&#xff0c;有时候需要对页面输出的最终 HTML 源代码进行控制&#xff0c;是页面的 render 方法中很容易实现这个功能。下面就是一个实现的方法&#xff0c;注释都在代码中。 [c-sharp] view plaincopy <% Page Language"C#" %> <% …

FireFox插件SQLite Manager的使用

最近几天开始高IOS数据库来着&#xff0c;一开始就CoreData的学习&#xff0c;结果高了一天没有一点进展。 没法&#xff0c;还是先老实代码着吧&#xff0c;不过用的火狐插件可视化数据库的操作也是不错的似乎。 网上搜了搜用法&#xff0c;还真没找到有什么的&#xff0c;最后…

简单的数据增强代码(C++与opencv)

包括了图片批量平移、旋转、以及像素变换 #include <opencv2/opencv.hpp> #include "opencv2/features2d.hpp" #include <vector> #include <algorithm> #include <iostream> #include "windows.h" #include <stdio.h> #in…

aes模式_AES的完整形式是什么?

aes模式AES&#xff1a;高级加密标准 (AES: Advanced Encryption Standard) AES is an abbreviation of Advanced Encryption Standard, also known by its original name Rijndael. It is an arrangement of standard for the encryption of electronic data set up by the U.…

IOS ----UIButton用法详解

这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举了.希望对大家有用. //这里创建一个圆角矩形的按钮UIButton *button1 [UIButton buttonWithType:UIButtonTypeRoundedRect];// 能够定义的button类型有以下6种&#xff0c;// typedef enum {// UIButtonTypeCusto…