Spring笔记4-AOP,前置通知,后置通知,返回通知,异常通知
2017-08-27 19:20
597 查看
<beans> <!--配置自动扫描的包--> <context:componemt-scan base-package="com.kcj.test"></context:componemt-scan> <!--使用Aspectj注解,自动为匹配的类生成代理对象--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
package com.kcj.test; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Order(1)//设置切面的优先级,数字越小优先级越高 @Aspect//声明为一个切面 @Component//放到IOC容器 public class LoggingAspect { /*** * 定义一个方法来声明切入点,提供引用 */ @Pointcut("execution(* com.kcj.test.*.*(..))") public void declareJoinPointExxcution(){ } /*** * 声明该方法是一个前置通知,方法执行之前执行 */ @Before("LoggingAspect.declareJoinPointExxcution()") public void before(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); Object[] args = joinPoint.getArgs(); } /*** * 声明该方法是一个后置通知,目标方法执行后执行,就算目标方法抛出异常也会执行 * 但是后置通知不能访问目标方法返回的结果 */ @Before("declareJoinPointExxcution()") public void after(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); Object[] args = joinPoint.getArgs(); } /*** * 在目标方法正常执行完了之后执行,可以访问到方法的返回值:result */ @AfterReturning(value = "declareJoinPointExxcution()", returning = "result") public void afterReturning(JoinPoint joinPoint, Object result) { } /*** * 在目标方法抛出异常执行,也可以指定出现制定异常时执行,Exception改为需要指定的一场类型 */ @AfterThrowing(value = "execution(* com.kcj.test.*.*(..)))",throwing = "e") public void afterThorwing(JoinPoint joinPoint, Exception e) { } }
相关文章推荐
- [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知
- Spring初学之xml实现AOP前置通知、后置通知、返回通知、异常通知等
- Spring aop 前置通知、后置通知、返回通知、 异常通知 、后置通知
- Spring初学之annotation实现AOP前置通知、后置通知、返回通知、异常通知。
- Spring 基于注解,实现 前置通知、后置通知、返回通知、 异常通知 、后置通知
- Aop的日志操作(前置,后置,环绕,返回,异常通知)
- Spring AOP学习笔记(3):AOP返回通知&异常通知&环绕通知
- Spring4笔记----AOP的返回通知和异常通知
- Spring_17-19前置通知&后置通知&返回通知&异常通知&环绕通知
- spring_AOP前置通知&后置通知&返回通知&环绕通知
- springaop术语概述及aop中常见通知的实现(前置,后置,环绕,异常)
- Spring通知(前置通知,后置通知,返回通知,异常通知,环绕通知)
- Spring AOP 面向切面编程 常见通知实现(前置,后置,环绕,异常)
- Spring AOP--返回通知,异常通知和环绕通知
- Spring初学之annotation实现AOP前置通知和后置通知
- AOP的一些东西(事务的传播特性,前置,后置,环绕,异常 等各种通知)
- Spring AOP--返回通知,异常通知和环绕通知
- Spring框架——AOP前置、后置、环绕、异常通知
- String4笔记----AOP的前置通知和后置通知
- Spring的AOP前置,后置,运行,异常