Spring AOP @Aspect 基本用法

Spring使用的AOP注解分为三个层次:

前提条件是在xml中放开了<aop:aspectj-autoproxy proxy-target-class="true"/><!-- 开启切面编程功能 -->

1、@Aspect放在类头上,把这个类作为一个切面。

2、 @Pointcut放在方法头上,定义一个可被别的方法引用的切入点表达式。

3、5种通知。

3.1、@Before,前置通知,放在方法头上。

3.2、@After,后置【finally】通知,放在方法头上。

3.3、@AfterReturning,后置【try】通知,放在方法头上,使用returning来引用方法返回值。

3.4、@AfterThrowing,后置【catch】通知,放在方法头上,使用throwing来引用抛出的异常。

3.5、@Around,环绕通知,放在方法头上,这个方法要决定真实的方法是否执行,而且必须有返回值。

示例代码: ` @Component
@Aspect
public class LogAspect {

1 /** 2 * 定义Pointcut,Pointcut的名称 就是simplePointcut,无返回值,该方法只是一个切点的标识 3 * @author haopeng 4 */ 5 @Pointcut("execution(public * com.mcgrady.service.impl..*.*(..))") 6 public void recordLog() { 7 } 8 9 @AfterReturning(pointcut = "recordLog()") 10 public void simpleAdvice() { 11 LogUtil.info("最后执行after后置通知 "); 12 } 13 14 @Around("recordLog()") 15 public Object aroundLogCalls(ProceedingJoinPoint jp) throws Throwable { 16 LogUtil.info("环绕通知........"); 17 return jp.proceed(); 18 } 19 20 @Before("recordLog()") 21 public void before(JoinPoint jp) { 22 String className = jp.getThis().toString(); 23 String methodName = jp.getSignature().getName(); // 获得方法名 24 LogUtil.info("位于:" + className + "调用了" + methodName + "()方法!!!!!"); 25 Object[] args = jp.getArgs(); // 获得参数列表 26 if (args.length <= 0) { 27 LogUtil.info("====" + methodName + "()方法没有参数"); 28 } else { 29 for (int i = 0; i < args.length; i++) { 30 LogUtil.info("====参数 " + (i + 1) + ":" + args[i]); 31 } 32 } 33 LogUtil.info("====================================="); 34 } 35 36 @AfterThrowing("recordLog()") 37 public void catchInfo() { 38 LogUtil.info("异常信息"); 39 } 40 41 @After("recordLog()") 42 public void after(JoinPoint jp) { 43 LogUtil.info("后置通知.........." + jp.getSignature().getName() + "()方法-结束!"); 44 } 45}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Spring 学习笔记(四):Spring AOP

@\TOC\1概述本文主要讲述了AOP的基本概念以及在Spring中AOP的几种实现方式。2AOPAOP,即AspectOrientedProgramming,面向切面编程,与OOP相辅相成。类似的,在OOP中,以类为程序的基本单元,在AOP中的基本单元是Aspect

SpringAOP

Aspect切面:一个关注点的模块化,这个关注点可能会横切多个对象Joinpoint连接点:程序执行过程中的某个特定的点Advice通知:在切面的某个连接点上执行的动作Pointcut切入点:匹配连接点的断言,在AOP的通知和一个切入点表达式关联Introduction引入:在不修改类代码的前提下,为类添加新的方法

Java——基于AspectJ的AOP开发

1.AspectJ简介AspectJ是一个基于Java语言的AOP框架。Spring2.0以后新增了对AdpectJ切点表达式的支持。@AspectJ是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面。新版本Spring框架,建议使用AspectJ方式来开发AOP。使用AspectJ需要导

Spring AOP @Aspect 基本用法 - HelloWorld