Spring AOP 注解方式简单实现

 
/**
 * 当前类作为切面,用来记录方法执行的日志
 */
@Aspect
@Component
public class LogUtil {
    /*
    * 通知执行的顺序
    * 正常执行的顺序:Before->After->AfterReturn
    * 异常执行的顺序:Before=>After->AfterThrowing
    *
    * 如果想要在方法中获取对应的的方法参数或者方法名称等信息的时候,必须要使用joinpoint对象获取
    * getSignature
    * getArgs()
    * 如果方法中有返回值 name您必须在注解中添加Returing="result" result必须和参数列表中的名称一致
    * 如果方法中有异常 信息  必须要throwing="e"  同时e的名称也要和参数列表的名称一致
    * */

    @Pointcut(value="execution (* com.spring.stydy.aop.ServiceLog.*())")
    public void pointCut(){};

    @Before(value = "pointCut()")
    public void before(JoinPoint joinPoint){
        //获取方法的签名
        Signature signature = joinPoint.getSignature();
        //获取方法的名称
        String name = signature.getName();
        System.out.println(name + "==>before方法被执行了!!!!!");
    }

    @After(value = "pointCut()")
    public void after(){
        System.out.println("after方法被执行了.....");
    }

    @AfterReturning("pointCut()")
    public void afterReturn() {
        System.out.println("afterReturn方法执行。。。");
    }

    @AfterThrowing("execution (* com.spring.stydy.aop.ServiceLog.*())")
    public void afterThrowing(){
        System.out.println("afterThrowing方法执行。。。");
    }

    //环绕通知
    /*环绕通知的执行优先于普通通知的
    * 正常的执行顺序 环绕前置->Before->环绕后置通知->环绕返回通知->After->AfterReturn..
     环绕通知如果捕获了异常信息, 其他通知不会接受到异常信息, 如果需要其他通知接收到,需要跑出异常
    * */
    @Around(value = "pointCut()")
    public void around(ProceedingJoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        System.out.println("环绕通知执行 方法执行开始。。");
        try {
            joinPoint.proceed();
            System.out.println("环绕后置通知 方法执行结束。。");
        } catch (Throwable throwable) {
            System.out.println("环绕异常通知执行。。");
        } finally {
            System.out.println("环绕通知 方法返回。。。");
        }
    }
}
public interface Serivece {

    void save();

    void delete();
}

 

@Service
public class ServiceLog implements Serivece{

    public void save(){
        System.out.println("save 方法执行");
    }

    public void delete(){
        System.out.println("delete 方法执行");
        try {
            int i = 1/0;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
@Test
public void testAop(){
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
    Serivece service = context.getBean("serviceLog", Serivece.class);
    System.out.println(service.getClass());
    service.save();
    System.out.println("------------异常的返回情况如下-----------");
    service.delete();

}
<context:component-scan base-package="com.spring.stydy"></context:component-scan>
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
class com.sun.proxy.$Proxy18
环绕通知执行 方法执行开始。。
save==>before方法被执行了!!!!!
save 方法执行
环绕后置通知 方法执行结束。。
环绕通知 方法返回。。。
after方法被执行了.....
afterReturn方法执行。。。
------------异常的返回情况如下-----------
环绕通知执行 方法执行开始。。
delete==>before方法被执行了!!!!!
delete 方法执行
环绕异常通知执行。。
环绕通知 方法返回。。。
after方法被执行了.....
afterReturn方法执行。。。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值