前面的各种Aop编程,都是基于XML的,这篇文章讲的是把XML方式改为注解方式来做。

Spring注解开发和xml开发所需要的包是一样的,所以只要把xml开发方式的包复制到以注解为开发方式的包的项目下就可以了。

第一步:导入相应的jar包

第二步:需要在applicationContext.xml中引入aop的名称空间

开启注解自动代理:

  1. <!-- 自动注解 -->
  2. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

第三步:AOP编程:

第(一)步.目标对象:

  1. //编写目标类(真正被代理的)
  2. public class UserDaoImpl implements UserDao {
  3.  
  4. public int delete() {
  5. System.out.println("删除了");
  6. return 1;
  7.  
  8. }
  9.  
  10. }

第(二)步:编写切面类和Advice方法

  1. //这个注解表明这个类就是切面类,在这个类里面可以写Advice(通知)方法。
    @Aspect
  2. public class Myspect {
  3. //我们配置一个前置方法的Advice方法,这是一个Advice方法,当然要配置切点了。配了切点就表示在这个切点执行这个Advice方法。
  4. @Before("execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))")
  5. public void before1(JoinPoint joinPoint)
  6. {
  7. System.out.print("this is 前置方法");
  8. }
  9. //后置通知,这是一个Advice方法,当然要配置切点了。配了切点就表示在这个切点执行这个Advice方法。
  10. @AfterReturning(value="execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))",returning="returnVal")
  11. public void afterReturning(JoinPoint joinPoint,Object returnVal)
  12. {
  13. System.out.println("this is afterReturning 方法");
  14. System.out.println(returnVal);
  15.  
  16. }
  17.  
  18. }

第四步:在Spring配置文件中配置目标Bean和切面Bean

  1. <!-- xml配置目标bean和切面bean -->
  2. <bean id="UserDao" class="com.guigu.shen.anotion.UserDaoImpl"></bean>
  3. <bean id="Myspect" class="com.guigu.shen.anotion.Myspect"></bean>

接下来给出Aspect5种通知的注解形式的具体实例

前置通知@Before

  1. //前置通知
    @Before("execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))")
  2. public void before1(JoinPoint joinPoint)
  3. {
  4. System.out.print("this is 前置方法");
  5. }

后置通知:

  1. //后置通知
  2. @AfterReturning(value="execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))",returning="returnVal")
  3. public void afterReturning(JoinPoint joinPoint,Object returnVal)
  4. {
  5. System.out.println("this is afterReturning 方法");
  6. System.out.println(returnVal);
  7.  
  8. }

环绕通知:

  1. //环绕通知,在方法的前后执行,在工作中可以计算方法执行的时间,性能的监控,权限的设置,缓存的实行等
  2. @Around("execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))")
  3. public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
  4. {
  5. long begintime=System.currentTimeMillis();
  6. Object result=proceedingJoinPoint.proceed();
  7. long endtime=System.currentTimeMillis();
  8. System.out.println("方法执行了"+(endtime-begintime)+"时间");
  9. return result;
  10.  
  11. }

异常通知:指的是当发生一些异常时会输出异常信息(如果方法没有异常的话,这个通知不会被执行的,比如如果.UserDaoImpl中的delete()方法会产生一个异常,

那么这个切点通知方法会执行,如果UserDaoImpl中的delete()方法没哟异常就不会执行这个方法)

  1. @AfterThrowing(value="execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))",throwing="e")
  2. public void afterThrowing(JoinPoint joinPoint,Exception e)
  3. {
  4. System.out.print(joinPoint.toLongString()+"方法发生了异常"+e.getMessage());
  5. }

情形一:

被代理类的方法:

  1. //编写目标类(真正被代理的)
  2. public class UserDaoImpl implements UserDao {
  3.  
  4. public int delete() {
  5. System.out.println("删除了");
  6. //int a=1/0;
  7. return 1;
  8.  
  9. }
  10.  
  11. }

测试方法:

  1. @Test
  2. public void deletetest()
  3. {
  4. userDao.delete();
  5. }

结果:不会输出异常的信息。

情形二:

被代理类的方法:

  1. //编写目标类(真正被代理的)
  2. public class UserDaoImpl implements UserDao {
  3.  
  4. public int delete() {
  5. System.out.println("删除了");
  6. int a=1/0;
  7. return 1;
  8.  
  9. }
  10.  
  11. }

测试方法:

  1. @Test
  2. public void deletetest()
  3. {
  4. userDao.delete();
  5. }

结果:execution(public abstract int com.guigu.shen.anotion.UserDao.delete())方法发生了异常/ by zero18:36:25,064  INFO GenericApplicationContext:1042 - Closing org.springframework.context.support.GenericApplicationContext@24f9fdc: startup date [Mon Aug 01 18:36:07 CST 2016]; root of context hierarchy

最终通知:

  1. @After("execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))")
  2. public void after()
  3. {
  4. System.out.print("最终通知,释放资源");
  5.  
  6. }

结果:

this is 前置方法

删除了
方法执行了0时间
最终通知,释放资源

this is afterReturning 方法

下面给出整个案例的结构图以及代码

Myspect.java是切面类
  1. //定义一个切面类。以及Advice方法
  2. //这个注解表明这是切面类
  3. @Aspect
  4. public class Myspect {
  5. //我们配置一个前置方法的Advice方法,这是一个Advice方法,当然要配置切点了
  6. @Before("execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))")
  7. public void before1(JoinPoint joinPoint)
  8. {
  9. System.out.print("this is 前置方法");
  10. }
  11. //后置通知
  12. @AfterReturning(value="execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))",returning="returnVal")
  13. public void afterReturning(JoinPoint joinPoint,Object returnVal)
  14. {
  15. System.out.println("this is afterReturning 方法");
  16. System.out.println(returnVal);
  17.  
  18. }
  19.  
  20. //环绕通知,在方法的前后执行,在工作中可以计算方法执行的时间,性能的监控,权限的设置,缓存的实行等
  21. @Around("execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))")
  22. public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
  23. {
  24. long begintime=System.currentTimeMillis();
  25. Object result=proceedingJoinPoint.proceed();
  26. long endtime=System.currentTimeMillis();
  27. System.out.println("方法执行了"+(endtime-begintime)+"时间");
  28. return result;
  29.  
  30. }
  31. @AfterThrowing(value="execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))",throwing="e")
  32. public void afterThrowing(JoinPoint joinPoint,Exception e)
  33. {
  34. System.out.print(joinPoint.toLongString()+"方法发生了异常"+e.getMessage());
  35. }
  36. @After("execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))")
  37. public void after()
  38. {
  39. System.out.print("最终通知,释放资源");
  40.  
  41. }
  42.  
  43. }

UserDao接口类:

  1. public interface UserDao {
  2. public int delete();
  3. }

UserDao接口的实现类:

  1. //编写目标类(真正被代理的)
  2. public class UserDaoImpl implements UserDao {
  3.  
  4. public int delete() {
  5. System.out.println("删除了");
  6. //int a=1/0;
  7. return 1;
  8.  
  9. }
  10.  
  11. }

Junit测试类:

  1. //JUnit与Spring的整合 (用注解)
    @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations="classpath:applicationContext.xml")
  3.  
  4. public class TestMyspect {
  5. @Autowired
  6. private UserDao userDao;
  7.  
  8. @Test
  9. public void deletetest()
  10. {
  11. userDao.delete();
  12. }
  13. public void aroundTest()
  14. {
  15.  
  16. userDao.delete();
  17. }
  18. }

ApplicationContext.xml文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  9. http://www.springframework.org/schema/aop
  10. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
  15.  
  16. <!-- AspectJ AOP -->
  17.  
  18. <!-- 自动注解 -->
  19. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  20. <!-- xml配置目标bean和切面bean -->
  21. <bean id="UserDao" class="com.guigu.shen.anotion.UserDaoImpl"></bean>
  22. <bean id="Myspect" class="com.guigu.shen.anotion.Myspect"></bean>
  23.  
  24. <!--
  25. 流程解释:
  26. xml配置目标bean和切面bean,这样一来bean就会被加载,对象就创建好了,然后我们在之前不是配置了一个<aop:aspectj-autoproxy></aop:aspectj-autoproxy>了吗
  27. 这样就能给我们创建的UserDao和Myspect对象做代理了
  28. -->
  29.  
  30. </beans>

17Spring_AOP编程(AspectJ)_AspectJ的注解编程的更多相关文章

  1. 18Spring_AOP编程(AspectJ)_AspectJ的各种通知总结

    小结: 前置通知(权限控制). 后置通知 ---- 不怎么用 环绕通知(权限控制. 性能监控. 缓存技术 ) 异常通知 (发生异常后, 记录错误日志 ) 最终通知 (释放资源 ) 环绕通知 是取代任何 ...

  2. Spring 注解(一)Spring 注解编程模型

    Spring 注解(一)Spring 注解编程模型 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring 注解系列 ...

  3. Spring 注解编程之注解属性别名与覆盖

    前两篇文章咱聊了深入了解了 Spring 注解编程一些原理,这篇文章我们关注注解属性方法,聊聊 Spring 为注解的带来的功能,属性别名与覆盖. 注解属性方法 在进入了解 Spring 注解属性功能 ...

  4. 理解 Spring 注解编程模型

    理解 Spring 注解编程模型 Spring 中有一个概念叫「元注解」(Meta-Annotation),通过元注解,实现注解的「派生性」,官方的说法是「Annotation Hierarchy」. ...

  5. 跟Evan学Sprign编程思想 | Spring注解编程模式【译】

    Spring注解编程模式 概况 多年来,Spring Framework不断发展对注解.元注解和组合注解的支持. 本文档旨在帮助开发人员(Spring的最终用户以及Spring Framework和S ...

  6. [.net 面向对象编程基础] (2) 关于面向对象编程

    [.net 面向对象编程基础]  (2)  关于面向对象编程 首先是,面向对象编程英文 Object-Oriented Programming 简称 OOP 通俗来说,就是 针对对象编程的意思 那么问 ...

  7. 5天玩转C#并行和多线程编程 —— 第五天 多线程编程大总结

    5天玩转C#并行和多线程编程系列文章目录 5天玩转C#并行和多线程编程 —— 第一天 认识Parallel 5天玩转C#并行和多线程编程 —— 第二天 并行集合和PLinq 5天玩转C#并行和多线程编 ...

  8. AspectJ获取方法注解的信息

    在使用Aspectj获取方法注解信息的时候,可以使用下面的代码片段: /** * Get value of annotated method parameter */ private <T ex ...

  9. linux tcp/ip编程和windows tcp/ip编程差别以及windows socket编程详解

    最近要涉及对接现有应用visual c++开发的tcp客户端,花时间了解了下windows下tcp开发和linux的差别,从开发的角度而言,最大的差别是头文件(早期为了推广尽可能兼容,后面越来越扩展, ...

随机推荐

  1. ubuntu下nagios配置

    参考文献: http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html http://my.oschina.net/duangr/blo ...

  2. vsphere vcenter server下安装ubuntu的vmwaretools

    0.参考文献 百度经验:这里面是以redhat桌面版为实例进行介绍的,我的环境是ubuntu-server,虽然不一样,也可以参考 http://jingyan.baidu.com/article/2 ...

  3. view.performClick()触发点击事件

    1.主要作用 自动触发控件的点击事件 2.界面的布局文件  activity_main.xml <RelativeLayout xmlns:android="http://schema ...

  4. 【原】iOSCoreAnimation动画系列教程(二):CAKeyFrameAnimation【包会】

    在上一篇专题文章[原]iOSCoreAnimation动画系列教程(一):CABasicAnimation[包会]中我们学习了iOS核心动画CoreAnimation中CABasicAnimation ...

  5. XML学习总结(一)——XML介绍

    一.XML概念 Extensible Markup Language,翻译过来为可扩展标记语言.Xml技术是w3c组织发布的,目前推荐遵循的是W3C组织于2000发布的XML1.0规范. 二.学习XM ...

  6. yum安装mariadb

    安装mysql yum install mariadb mariadb-server MySQL-python mysql-devel Package MySQL-python-1.2.5-1.ibm ...

  7. ubuntu下安装wireshark

    ubuntu下安装wireshark  download: http://www.wireshark.org/download.html   choose source code 安装编译工具: $s ...

  8. ASP.NET Session的共享

    注: 在ashx文件中使用Session 首先添加引用 using System.Web.SessionState; 实现接口 public class XXXX: IHttpHandler ==&g ...

  9. redis客户端--jedis

    一.jedis jedis 是 redis推荐的java客户端.通过Jedis我们可以很方便地使用java代码的方式,对redis进行操作.jedis使用起来比较简单,它的操作方法与redis命令相类 ...

  10. java代码调用oracle存储过程

    一.简介 与调用mysql存储过程类型过程都是如下 1.创建连接 Connection conn = DriverManager.getConnection(url, user, password); ...