Spring中AspectJ通过注解的方式,来实现几种AOP通知

首先配置的applicationContext.xml中需要扫描注解驱动,并且添加aspectJ扫描,这个aop:aspectJ会自动的扫描使用的注解:

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.bjsxt.pojo,com.bjsxt.advice"></context:component-scan>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

我们知道使用aspectJ的方式最大的好处是只要一个类的方法,通过配置,便能够实现前置通知等其他通知,这个时候我们加入注解,@Component 创建对象,@Aspect是告诉spring我们要使用aspect的方式来添加aop,@Before等其他方式是确定前置通知后置通知,环绕通知,异常通知。

package com.bjsxt.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AspectAdvice {


    @Before("execution(* com.bjsxt.pojo.Admin.a())")
    public   void  beforAdvice(){

        System.out.println("--前置通知--");
    }

    @After("execution(* com.bjsxt.pojo.Admin.a())")
    public   void  AfterAdvice(){

        System.out.println("--后置通知--");
    }


    @Around("execution(* com.bjsxt.pojo.Admin.a())")
    public   void  aroundAdvice(ProceedingJoinPoint  point) throws Throwable {

        System.out.println("--环绕通知(前)--");

        Object proceed = point.proceed();

        System.out.println("--环绕通知(后)--");
    }


    @AfterThrowing("execution(* com.bjsxt.pojo.Admin.a())")
    public   void   ThrowsAdvice(){

        System.out.println("--异常通知--");
    }

}

pojo类通过使用注解,创建对象:

package com.bjsxt.pojo;

import org.springframework.stereotype.Component;

@Component("admin")
public class Admin {


    public   void  a(){

        System.out.println("方法A");
    }
}

测试:

package com.bjsxt.test;

import com.bjsxt.pojo.Admin;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestB {

    public static void main(String[] args) {

        ApplicationContext  app =new ClassPathXmlApplicationContext("applicationContext2.xml");

        Admin admin = app.getBean("admin", Admin.class);


        admin.a();


    }
}

结果:

tips:这里我们需要注意,我们不使用注解的方式进行配置:打印的顺序是:

前置通知

环绕前置通知

切点方法

环绕后置通知

后置通知

但是使用注解后,执行的顺序就发生了改变:

环绕前置通知

前置通知

切点方法

环绕后置通知

后置通知

我们为了方便记忆:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值