spring-210728-02---AOP-AspectJ注解-抽取相同切入点&设置优先级

spring-210728-02—AOP-AspectJ注解-抽取相同切入点&设置优先级


相同切入点抽取

UserProxy02.java

package com.bgy.spring.aoptest;

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

// 增强的类
@Component
@Aspect     // 生成代理对象
public class UserProxy02 {

    // 注解@Pointcut,用于抽取相同的切入点
    @Pointcut(value = "execution(* com.bgy.spring.aoptest.User.add(..))")
    public void pointdemo(){

    }

    // 前置通知
    // 注解@Before 表示作为前置通知
    @Before(value = "pointdemo())")
    public void before(){
        System.out.println("before..........");
    }

    // 最终通知
    @After(value = "pointdemo())")
    public void after(){
        System.out.println("after.............");
    }

    // 后置通知,如果有异常就不执行了
    @AfterReturning(value = "pointdemo())")
    public void afterReturning(){
        System.out.println("afterReturning.............");
    }

    // 异常通知
    @AfterThrowing(value = "pointdemo())")
    public void afterThrowing(){
        System.out.println("afterThrowing.............");
    }

    // 环绕通知
    @Around(value = "pointdemo())")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("around   环绕之前.............");

        // 被增强的方法
        proceedingJoinPoint.proceed();

        System.out.println("around   环绕之后.............");
    }
}

多个增强类,同一个方法进行增强,设置增强类优先级

在增强 类上面 添加 注解@Order(数字类型值) ,数字越小优先级越高

演示

Person.java

package com.bgy.spring.aoptest;
import org.springframework.stereotype.Component;

@Component
public class Person {

    public void add(){
        System.out.println("person add.......");
    }
}

PersonProxy01.java

package com.bgy.spring.aoptest;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/*
    注解@Order(数字类型值)设置优先级,数字越小优先级越高
*/
@Component
@Aspect     // 生成代理对象
@Order(2)
public class PersonProxy01 {

    @Before(value = "execution(* com.bgy.spring.aoptest.Person.add(..))")
    public void before(){
        System.out.println("personProxy01 before........");
    }
}

PersonProxy02.java

package com.bgy.spring.aoptest;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/*
    注解@Order(数字类型值)设置优先级,数字越小优先级越高
*/
@Component
@Aspect     // 生成代理对象
@Order(1)
public class PersonProxy02 {

    @Before(value = "execution(* com.bgy.spring.aoptest.Person.add(..))")
    public void before(){
        System.out.println("personProxy02 before........");
    }
}

bean01.xml

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.bgy.spring.aoptest"></context:component-scan>

    <!--
        开启Aspect生成代理对象
        意思就是只要类上面有注解@Aspect,就生成代理对象。
    -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

TestPerson.java

import com.bgy.spring.aoptest.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestPerson {

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");

        Person person = context.getBean("person", Person.class);

        person.add();
    }
}

//    运行结果:
//            personProxy02 before........
//            personProxy01 before........
//            person add.......
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值