spring aop 第一个程序

本文介绍了如何在Spring中实现AOP,从添加依赖、配置支持到编写AOP类,详细讲解了切入点表达式的使用和五种通知类型的含义,提供了一个完整的AOP程序示例。
摘要由CSDN通过智能技术生成

1,pom中添加aspectj相关jar包

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.8.13</version>
</dependency>

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.13</version>
</dependency>

2,在spring配置文件中加入aop支持:

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

3,创建一个AOP类:

@Aspect
@Component
public class HymanInterceptor {
    @Pointcut("execution(public * hyman.study..*(..))")
    public void pc(){

    }

    @Before("pc()")
    public void before(){
        System.out.println("before...");
    }

    @Around("pc()")
    public void around(ProceedingJoinPoint pjp){
        System.out.println("around begin");
        try {
            pjp.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("around end");
    }


//注:如果around方法报错,可以把它改成下面的形式,即让它返回Objet,而不是void
/*public Object around(ProceedingJoinPoint pjp) throws Throwable{
    System.out.println("around begin");//方法执行前  
    Object object=pjp.proceed();
    System.out.println("around end");//方法执行后
    return object;
}*/



    @After("pc()")
    public void after(){
        System.out.println("after");
    }

    @AfterReturning("pc()")
    public void afterReturning(){
        System.out.println("AfterReturning");
    }


    @AfterThrowing(throwing="ex",pointcut="pc()")
    public void afterThrowing(Throwable ex){
        System.out.println("AfterThrowing");
        System.out.println(ex.getMessage());
    }
}

注意:切入点语法可以直接写在注解后面,也可以先定义好,然后在注解后面直接使用。

4,测试:

@Component("aopTest")
public class AopTest {
    public void test() {
        System.out.println("hello world....");
    }
}

@Test
public void testProxy(){
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring/applicationContext.xml");
    AopTest aopTest=(AopTest) applicationContext.getBean("aopTest");
    aopTest.test();
}

打印结果如下:
这里写图片描述

5,AOP切入点语法如下
语法结构:execution(方法修饰符(例如public) 方法返回值类型 方法所属类 匹配方法名(方法参数) 方法声明抛出的异常),其中:
方法返回值类型,匹配方法名(方法参数)是不能省略的,其他部分如果省略,代表,也就是全部。另外,每个部分都支持通配符来匹配全部。
方法参数比较特殊,支持两种通配符:
* : 代表一个任意类型的参数,注意是一个参数,但是可以是任意类型
.. : 代表0个或多个任意类型的参数,参数个数和类型都任意。

例如:
()匹配一个无参方法
(..)匹配一个可以接受任意数量和任意类型参数的方法;
(*)匹配一个接受一个任意类型参数的方法;
(*,integer) :匹配一个接受两个参数的方法,第一个可以为任意类型,第二个必须为integer。

下面举例说明:
execution(public * * (..)) : 匹配所有修饰符为 public 的方法;
execution( * * (..)) : 匹配所有方法;
execution(* save* (..)) : 匹配所有类中的以save开头的方法。
execution(* save(*,String)): 匹配所有类中的接受两个参数,并且第二个参数为String类型的save方法;
execution(* hyman.study.service.Test.save(..)): 匹配hyman.study.service.Test类中的save方法。
execution(* hyman.study...(..)): 匹配hyman.study的最长到孙子包下的所有类的所有方法。包中的代表一层。注意这里代表的是延伸的最深的层次,包括之前的层次。这里就是寻找hyman.study包下的类、hyman.study子包下的类,hyman.study子包的子包下的类。
execution(* hyman.study..*(..)) : 匹配hyman.study所有包中的所有类中的所有方法,包括子包,子包的子包。。。等等。
execution(* hyman.study...*ServiceImpl.(..)): 匹配hyman.study所有包(包括子孙包)中的以ServiceImpl为结尾的类中的所有方法。
execution(* foo(String,Long)): 匹配所有方法名为foo,并且第一个参数为String类型,第二个参数为Long类型的方法。
execution(* foo(String,..)): 匹配所有方法名为foo,并且第一个参数是String类型的方法(后面可以有任意数量任意类型的参数)。

6,Spring aop advice

(1)@Before:方法执行前调用
(2)@After:方法执行后调用
(3)@AfterThrowing:方法抛出异常后调用
(4)@AfterReturning:方法返回后调用
(5)@Around:方法前后都执行。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值