iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >浅谈@Aspect@Order各个通知的执行顺序
  • 851
分享到

浅谈@Aspect@Order各个通知的执行顺序

2024-04-02 19:04:59 851人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录@Aspect@Order各个通知的执行顺序代码小结spring AspectJ order(顺序)@Aspect@Order各个通知的执行顺序 两个切面类:【记录日志】和【判断

@Aspect@Order各个通知的执行顺序

两个切面类:【记录日志】和【判断参数】,分别对应顺序 @Order(0) 和@Order(1) 。

本文只是将重点说下 执行顺序 这么回事哈哈哈

代码

【业务类】


@Controller
public class LoginController {
    //向外面抛出异常
    public void loginWithThrow(String username, String passWord) throws Exception {
        if (username == null || password == null) {
            throw new Exception("登录信息不可为空啊");
        }
        System.out.println("LoginController#login...");
    }
    //抛出异常自己捕获的情况
    public void loginWithTryCatch(String username, String password) {
       try{
           if (username == null || password == null) {
               throw new Exception("登录信息不可为空啊");
           }
           System.out.println("LoginController#login...");
       }catch (Exception e){
           e.printStackTrace();
       }
    }
}

【切面类】


@Order(0)
@Aspect
@Component
public class LogAspect {
    //抽出共通的execution用的
    //com.yuki.demo.aop.aspect 包或者子包下所有类的方法
    @Pointcut("execution(* com.yuki.demo.aop.aspect..*.*(..))")
    public void pointcut(){
    }
    //前置通知
//    @Before("execution(public void com.yuki.demo.aop.aspect.LoginController.*(String,String))")
    @Before("pointcut()")
    public void before() {
        System.out.println("LogAspect#before...");
    }
    //环绕通知
    //ProceedingJoinPoint 只有环绕通知有
    @Around("execution(public void com.yuki.demo.aop.aspect.LoginController.*(String,String))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("LogAspectA#around开始...");
        //代理方法的执行,如果没有joinPoint.proceed() ,则前置通知@Before 不会执行,其它的通知正常
        joinPoint.proceed();
        //执行方法之后,如果joinPoint.proceed() 抛出了异常,则该句不会执行,抛出异常后直接跳出了aroud方法了
        System.out.println("LogAspectA#around结束...");
    }
    //后置通知(只要连接点被执行,不管是否抛出异常)
    @After("execution(public void com.yuki.demo.aop.aspect.LoginController.*(String,String))")
    public void after() {
        System.out.println("LogAspect#after...");
    }
    //异常通知(只有在joinPoint.proceed()方法执行向外面抛出了异常,才会执行该通知)
    @AfterThrowing("execution(public void com.yuki.demo.aop.aspect.LoginController.*(String,String))")
    public void afterThrowing() {
        System.out.println("LogAspect#afterThrowing...");
    }
    //正常的返回通知通知(正常结束了才会执行该通知)
    @AfterReturning("execution(public void com.yuki.demo.aop.aspect.LoginController.*(String,String))")
    public void afterReturning() {
        System.out.println("LogAspect#afterReturning...");
    }
}

【切面类】


@Order(1)
@Aspect
@Component
public class SignAspect {
    @Around("execution(public void com.yuki.demo.aop.aspect.LoginController.*(String,String))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("SignAspect#around开始...");
        joinPoint.proceed();
        System.out.println("SignAspect#around结束...");
    }
}

【启动配置】

省略。。。非重点

测试类】

@SpringBootTest
class AopApplicationTests {
    @Autowired
    private LoginController loginController;
    @Test
    void contextLoads() {
        loginController.loginWithTryCatch("yuki", "1234");
    }
}

【控制台输出】

LogAspectA#around开始...
LogAspect#before...
SignAspect#around开始...
LoginController#login...
SignAspect#around结束...
LogAspectA#around结束...
LogAspect#after...
LogAspect#afterReturning...

小结

spring AspectJ order(顺序)

@Aspect
@Order(2)
public class HelloWorldAspectAnnotation {
	
		
	
    //定义前置通知,注意这里是sayHello2
	//使用@Before进行前置通知声明,其中value用于定义切入点表达式或引用命名切入点
	@Before(value="execution(* com.boventech..*.sayHello2(..))&& args(param)",argNames="param")
	public void beforeAdvice(JoinPoint joinPoint,String param) {
		System.out.println(1);
		System.out.println("=======================");
		System.out.println("===param:" + param);
		System.out.println("=======================");
		System.out.println(joinPoint.getArgs().length);
		System.out.println("=======================");
		System.out.println(joinPoint.toString());
		System.out.println("=======================");
		System.out.println(joinPoint.getTarget());
		System.out.println("=======================");
		System.out.println(joinPoint.getThis());
		System.out.println("=======================");
		System.out.println("===========before advice");
	}
	
	@AfterReturning(value="execution(* com.boventech..*.sayHello2(..))&& args(param)",argNames="param",pointcut="execution(* com.boventech..*.sayHello2(..))&& args(param)")
	public void afterFinallyAdvice(JoinPoint joinPoint,String param) {
		System.out.println("param:"+param);
		System.out.println("===========");
		System.out.println("===========after finally advice");
	}
}
@Aspect
@Order(1)
public class HelloWorldAspectAnnotation2 {
	
		
	
    //定义前置通知,注意这里是sayHello2
	//使用@Before进行前置通知声明,其中value用于定义切入点表达式或引用命名切入点
	@Before(value="execution(* com.boventech..*.sayHello2(..))&& args(param)",argNames="param")
	public void beforeAdvice(JoinPoint joinPoint,String param) {
		System.out.println(2);
		System.out.println("=======================");		
	}
	
	
	@AfterReturning(value="execution(* com.boventech..*.sayHello2(..))&& args(param)",argNames="param",pointcut="execution(* com.boventech..*.sayHello2(..))&& args(param)")
	public void afterFinallyAdvice(JoinPoint joinPoint,String param) {
		System.out.println("order:" + 2);
	}
}
public class AopAnnotationTest {	
	@Test
    public void testHelloworld() {
        ApplicationContext ctx =  new ClassPathXmlApplicationContext("/helloWorld2.xml");
        IHelloWorld2Service helloworldService =ctx.getBean("helloWorld2Service", IHelloWorld2Service.class);
        String param = "12";
        helloworldService.sayHello2(param);
    } 
}
<aop:aspectj-autoproxy/>
	<bean id="helloWorld2Service" class="com.boventech.learning.serviceImpl.HelloWorld2ServiceImpl"/>
	
    <bean id="aspect"
             class="com.boventech.learning.aspect.HelloWorldAspectAnnotation"/>
             
    <bean id="aspect2"
             class="com.boventech.learning.aspect.HelloWorldAspectAnnotation2"/>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 浅谈@Aspect@Order各个通知的执行顺序

本文链接: https://www.lsjlt.com/news/139094.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作