基于注解的声明式AspectJ

  1. <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.3.xsd">
            
            
            <context:component-scan base-package="com.wangfei"/>
            <aop:aspectj-autoproxy/>
    </beans>

  2. package com.wangfei.aspectj.zhushi;

    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;

    @Aspect
    @Component

    public class MyAspect {

        @Pointcut("execution(* com.wangfei.jdk.*.*(..))")
        private void myPointCut(){}

        
        @Before("myPointCut()")
        public void myBefore(JoinPoint joinPoint){
            System.out.print("前置通知:模拟权限检查");
            System.out.print("  目标类是:"+joinPoint.getTarget());
            System.out.println(",被植入增强处理的目标方法是:"+joinPoint.getSignature().getName());
        }
        
        @AfterReturning(value="myPointCut()")
        public void myAfterReturning(JoinPoint joinPoint){
            System.out.print("后置通知:模拟记录日志");
            System.out.println("被植入的目标方法为"+joinPoint.getSignature().getName());
        }
        
        @Around("myPointCut()")
        public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
            System.out.println("环绕开始:开始目标方法前,模拟开启事务。。。");
            Object obj=proceedingJoinPoint.proceed();
            System.out.println("环绕结束:执行目标方法,模拟关闭事务");
            return obj;
        }
        
        @AfterThrowing(value="myPointCut()",throwing="e")
        public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
            System.out.println("异常通知:出错了"+e.getMessage());
        }
        
        @After("myPointCut()")
        public void myAfter(JoinPoint joinPoint){
            System.out.println("最终通知:模拟方法结束,释放资源");
        }
    }
  3. package com.wangfei.jdk;

    import org.springframework.stereotype.Repository;

    @Repository("useDao")
    public class UseDaoImpl implements UseDao{

        @Override
        public void addUser() {
            // TODO Auto-generated method stub
            System.out.println("add/");
        }

        @Override
        public void deleteUser() {
            // TODO Auto-generated method stub
            System.out.println("delete//");
        }

    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cherishYoY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值