基于注解开发Spring AOP 通知类

由于在applicationContext.xml中注入bean以及注册通知,比较麻烦,当运用注解的方式来完成十分便捷

首先要在xml中配置自动扫描以及注解支持

<context:component-scan base-package="spring.aop.anno"></context:component-scan>
 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

在通知类上加上@Aspect@Component,将他标记为切面的bean

前置通知使用@Before(execution …)

@Before("execution(* spring.aop.anno.*.*(..))")

其中第一个* 代表任意的方法返回值
第二个和第三表示类名以及方法名
…表示参数列表
下面是方法体

@Before("execution(* spring.aop.anno.*.*(..))")
 public void logerBefore(JoinPoint jp) {
  String methodName = jp.getSignature().getName();
  System.out.println("method: " + methodName + "將要被執行!");
  Object[] args = jp.getArgs();
  for(Object arg : args) {
   System.out.println("=======:>"+arg);
  }
 }

同理

后置通知:

@After("execution(* spring.aop.anno.*.*(..))")
 public void logerAfter(JoinPoint jp) {
  String methodName = jp.getSignature().getName();
  System.out.println("method: " + methodName + "已經被執行!");
  
  Object[] args = jp.getArgs();
  for(Object arg : args) {
   System.out.println("============:>" + arg);
  }
 }

环绕通知:

@Around("execution(* spring.aop.anno.*.*(..))")
 public Object logerAround(ProceedingJoinPoint pjp) {
  String methodName = pjp.getSignature().getName();
  System.out.println("環繞通知 =========>method: " + methodName + "將要被執行已經被執行!");
  Object[] args = pjp.getArgs();//獲取參數
  args[0] = "1";
  try {
   Object returnValue = pjp.proceed(args);
   
   System.out.println("環繞通知 =========>method: " + methodName + "已經被執行返回值:! " + returnValue);
//   returnValue = "hello World!!";
   return new BigDecimal("999999999999999");
  } catch (Throwable e) {
   e.printStackTrace();
  }
  return null;
 }

异常通知

@AfterThrowing(value="execution(* spring.aop.anno.*.*(..))",throwing="exception")
 public void loggerAfterThrowing(JoinPoint jp, Exception exception) {
  System.out.println("後置異常通知:" + exception);
 }

值得注意的是如果在注解中只有一个值,它默认赋值给value此时可不写value=
但是有两个或多个值的时候必须加上value=否则就会报错

@AfterThrowing("execution(* spring.aop.anno.*.*(..))",throwing="exception")

在这里插入图片描述
Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘bankService’ defined in file [F:\eWorkSpace\spring.aop\target\classes\spring\aop\anno\BankServiceImpl.class]: BeanPostProcessor before instantiation of bean failed; nested exception is java.lang.IllegalStateException: Returning argument name ‘resultValue’ was not bound in advice arguments

可将切面用pointcut注解表示

@Pointcut("execution(* spring.aop.anno.*.*(..))")
 public void loggerPointCut() {
  
 }

在返回通知可用它作为参数

后置返回通知

@AfterReturning(pointcut="loggerPointCut()", returning="resultValue")
 public void logerAfterReturning(JoinPoint jp, Object resultValue) {
  String methodName = jp.getSignature().getName();
  System.out.println("後置返回通知 =========>method: " + methodName + "已經被執行!");
  System.out.println("後置返回通知  返回值:" + resultValue);
 }

以下是main方法与接口和他的实现类

main方法:

ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext-anno.xml");
  BankService bs = ioc.getBean("bankService",BankService.class);
  bs.transfer("a", "b", new BigDecimal("400"));

接口

在这里插入图片描述

实现类
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值