之前是通过配置完成aop操作,如果自己写的话,太麻烦了,可以使用基于annotation的配置完成。

第一步:打开AOP的annotation支持

加上一句话:

  1. <context:annotation-config/>
  2. <context:component-scan base-package="com.Spring"/>
      <aop:aspectj-autoproxy  proxy-target-class="true"/>    //使用了jdk的自动动态代理,需要加上这句话,否则报错
  3. <aop:aspectj-autoproxy/>

随后需要在ServiceAspect类中编写所需要使用的annotation。

范例:修改serviceAspect类。

  1. package com.Spring.aop;
  2.  
  3. import java.lang.reflect.Array;
  4. import java.util.Arrays;
  5. import org.aspectj.lang.ProceedingJoinPoint;
  6. import org.aspectj.lang.annotation.After;
  7. import org.aspectj.lang.annotation.AfterReturning;
  8. import org.aspectj.lang.annotation.AfterThrowing;
  9. import org.aspectj.lang.annotation.Around;
  10. import org.aspectj.lang.annotation.Aspect;
  11. import org.aspectj.lang.annotation.Before;
  12. import org.springframework.stereotype.Component;
  13.  
  14. import com.Spring.Vo.Member;
  15.  
  16. @Component  //用来取代在xml中配置bean初始化
  17. @Aspect   //用来代替在xml中配置AOP操作
  18. public class ServiceAspect {
  19. @Before(value="execution(* com.Spring..*.*(..)))")
  20. public void serviceBefore()
  21. {
  22. System.out.println("AOP切面执行日志记录操作");
  23. }
  24. @Before(value="execution(* com.Spring..*.*(..)) and args(param)))",argNames="param")
  25. public void serviceBefore2(Object arg)
  26. {
  27. System.out.println("AOP切面执行增加前操作,参数=" +arg);
  28. }
  29. @After(value="execution(* com.Spring..*.*(..)))")
  30. public void serviceAfter()
  31. {
  32. System.out.println("AOP切面执行事务处理操作");
  33. }
  34. @AfterReturning(value="execution(* com.Spring..*.*(..)))",argNames="ret",returning="ret")
  35. public void serviceAfterReturn(Object val) //表示操作结果
  36. {
  37. System.out.println("AOP切面操作完成,返回结果:"+val);
  38. }
  39. @AfterThrowing(value="execution(* com.Spring..*.*(..)))",argNames="e",throwing="e")
  40. public void serviceAfterThrow(Exception e) //表示操作结果
  41. {
  42. System.out.println("AOP切面操作出现异常:"+e);
  43. }
  44. @Around(value="execution(* com.Spring..*.*(..)))")
  45. public Object serviceAround(ProceedingJoinPoint point) throws Throwable
  46. {
  47. System.out.println("AOP切面数据层方法调用之前,参数:"+Arrays.toString(point.getArgs()));
  48. Member vo=new Member();
  49. vo.setMid("TestAOP");
  50. vo.setName("测试AOP");
  51. Object retVal=point.proceed(new Object[]{ vo });
  52. System.out.println("AOP切面数据层方法调用之后,返回值:"+retVal);
  53. return true;
  54. }
  55. }

运行结果:

可以对照之前用注解的xml配置:

  1. <context:annotation-config/>
  2. <context:component-scan base-package="com.Spring"/>
  3. <aop:aspectj-autoproxy proxy-target-class="true"/>
  4.  
  5. <aop:config>
  6. <!-- 定义程序的切入点 -->
  7. <aop:pointcut expression="execution(* com.Spring..*.*(..)) and args(vo))" id="pointcut"/>
  8. <!-- 这里ref的对象是通过annotation配置@Component出来的, -->
  9. <!-- 定义面向方面的处理类 -->
  10. <aop:aspect ref="serviceAspect">
  11. <!--
  12. <aop:before method="serviceBefore2" pointcut-ref="pointcut" arg-names="vo"/>
  13. <aop:after method="serviceAfter" pointcut="execution(* com.Spring..*.*(..)))"/>
  14. <aop:after-returning method="serviceAfterReturn" pointcut="execution(* com.Spring..*.*(..)))" returning="haha" arg-names="haha"/>
  15. <aop:after-throwing method="serviceAfterThrow" pointcut="execution(* com.Spring..*.*(..)))" arg-names="e" throwing="abc"/>
  16. -->
  17. <aop:around method="serviceAround" pointcut="execution(* com.Spring..*.*(..)))" />
  18. </aop:aspect>
  19. </aop:config>

实际操作中,需要进行一些辅助性功能编写的时候(比如日志记录),建议使用annotation的配置操作,这样的代码是最简化的,也是最直观的。

18-spring学习-利用Annotation配置AOP的更多相关文章

  1. Spring学习记录(十二)---AOP理解和基于注解配置

    Spring核心之二:AOP(Aspect Oriented Programming) --- 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软 ...

  2. Spring学习笔记IOC与AOP实例

    Spring框架核心由两部分组成: 第一部分是反向控制(IOC),也叫依赖注入(DI); 控制反转(依赖注入)的主要内容是指:只描述程序中对象的被创建方式但不显示的创建对象.在以XML语言描述的配置文 ...

  3. Spring学习资料以及配置环境

    一.Spring4 1.介绍 新特性 SpringIDE 插件 IOC DI 在 Spring 中配置 Bean 自动装配 Bean 之间的关系(依赖.继承) Bean 的作用域 使用外部属性文件 S ...

  4. Spring学习之第一个AOP程序

    IOC和AOP是Spring的两大基石,AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对 ...

  5. Spring 学习(三)AOP

    (1)AOP概述 - AOP:面向切面编程,扩展功能不修改源代码实现 - AOP采取横向抽取机制,取代了传统的纵向继承体系重复性代码 (2)AOP底层原理 原始方法------->纵向继承体系 ...

  6. 【JavaEE】SSH+Spring Security基础上配置AOP+log4j

    Spring Oauth2大多数情况下还是用不到的,主要使用的还是Spring+SpringMVC+Hibernate,有时候加上SpringSecurity,因此,本文及以后的文章的example中 ...

  7. Spring学习(八)AOP详解

    文章更新时间:2020/04/06 一.一个例子 在上面的例子中,包租婆的核心业务就是签合同,收房租,那么这就够了,灰色框起来的部分都是重复且边缘的事,交给中介商就好了,这就是 AOP 的一个思想:让 ...

  8. Spring初学之annotation实现AOP前置通知、后置通知、返回通知、异常通知。

    实现两个整数的加减乘除.在执行每个方法之前打印日志. ArithmeticCalculator.java: package spring.aop.impl; public interface Arit ...

  9. Spring学习之旅(五)--AOP

    什么是 AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是 OOP(Object-Oriented Programing,面向对象编程)的补充和完善. OO ...

随机推荐

  1. Codeforces Round #346 (Div. 2) A. Round House 水题

    A. Round House 题目连接: http://www.codeforces.com/contest/659/problem/A Description Vasya lives in a ro ...

  2. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) A. Bear and Three Balls 水题

    A. Bear and Three Balls 题目连接: http://www.codeforces.com/contest/653/problem/A Description Limak is a ...

  3. PYPY_GC

    Author:Jin Date: 2014-7-8 http://doc.pypy.org/en/latest/windows.html http://www.pypy.org/download.ht ...

  4. 微信小程序的坑

    虽然官方文档,可以在.json中给页面设置背景颜色,用backgroundColor,但是实际上并不好使,所以设置背景颜色只能在wxss中设置 <import src="../comm ...

  5. CentOS 6.9开启iptables的日志实现调试

    系统日志配置在CentOS 5上叫syslog,而在CentOS 6上叫rsyslog(增强版的syslog),CentOS 5上的配置文件在/etc/syslog.conf下,而CentOS 6在/ ...

  6. How to use transparent PNG icons with Delphi ImageList

    http://www.aha-soft.com/faq/delphi-imagelist-png.htm Query: "Embarcadero Delphi ImageList does ...

  7. C# WebHelper-CookieHelper,CacheHelper,SessionHelper

    常用web操作工具类,记录一下,本文记录的工具类,都要求引用 System.Web 1.CookieHelper /// <summary> /// Cookie工具类 /// </ ...

  8. java反射机制简单介绍

    1.字节码.所谓的字节码就是当java虚拟机载入某个类的对象时,首先须要将硬盘中该类的源码编译成class文件的二进制代码(字节码),然后将class文件的字节码载入到内存中,之后再创建该类的对象 2 ...

  9. ffmpeg的IO操作

    ffmpeg 可以通过IO操作将数据读取和存储在文件或网络中 作为数据的读取和写入地址,数据被存放在file,http, ffmpeg 不仅可以编解常用的音视频格式,还可以将数据导入/导出到各种媒介中 ...

  10. fdopen()和fileno()函数

    转:http://book.2cto.com/201212/11763.html 文件描述字函数是流函数的初等函数,每一个流都与一个描述字相连.给定一个打开的文件描述字,可以用fdopen()函数为它 ...