java priorityqueue,20Spring切面的優先級

 2023-10-18 阅读 22 评论 0

摘要:通過使用@order注解指定切面的優先級,值越小,優先級越高代碼: package com.cn.spring.aop.impl; //加減乘除的接口類 public interface ArithmeticCalculator {int add(int i, int j);int sub(int i, int j);int mul(int i, int j);int div(int i, i
通過使用@order注解指定切面的優先級,值越小,優先級越高
代碼:
package com.cn.spring.aop.impl;
//加減乘除的接口類
public interface ArithmeticCalculator {int add(int i, int j);int sub(int i, int j);int mul(int i, int j);int div(int i, int j);
}
 
package com.cn.spring.aop.impl;import org.springframework.stereotype.Component;//實現類
@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {@Overridepublic int add(int i, int j) {int result = i + j;return result;}@Overridepublic int sub(int i, int j) {int result = i - j;return result;}@Overridepublic int mul(int i, int j) {int result = i * j;return result;}@Overridepublic int div(int i, int j) {int result = i / j;return result;}
}
package com.cn.spring.aop.impl;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;import java.util.Arrays;
import java.util.List;//把這個類聲明為一個切面:首先需要把該類放入到IOC容器中,在聲明為一個切面
//可以使用@order注解指定切面的優先級,值越小,優先級越高
@Order(2)
@Aspect
@Component
public class LoggingAspect {//聲明該方法是一個前置通知:在目標方法開始之前執行@Before("execution(public int ArithmeticCalculator.*(int, int))")public void beforeMethod(JoinPoint joinPoint) {String methodName = joinPoint.getSignature().getName();List<Object> args = Arrays.asList(joinPoint.getArgs());System.out.println("The method " +  methodName + " begins with " + args);}//后置通知:在目標方法執行后(無論是否發生異常),執行的通知//在后置通知中還不能訪問目標方法執行的結果@After("execution(public int ArithmeticCalculator.*(int, int))")public void afterMethod(JoinPoint joinPoint) {String methodName = joinPoint.getSignature().getName();List<Object> args = Arrays.asList(joinPoint.getArgs());System.out.println("The method " +  methodName + " ends with " + args);}/*** 在方法正常結束后執行的代碼* 返回通知是可以訪問到方法的返回值* @param joinPoint*/@AfterReturning(value = "execution(public int ArithmeticCalculator.*(int, int))",returning = "result")public void afterReturning(JoinPoint joinPoint, Object result) {String methodName = joinPoint.getSignature().getName();List<Object> args = Arrays.asList(joinPoint.getArgs());System.out.println("The method  ends witd " + result);}//在目標方法出現異常時會執行的代碼//可以訪問到異常對象;且可以指定在出現特定異常時再執行通知代碼@AfterThrowing(value = "execution(public int ArithmeticCalculator.*(int, int))",throwing = "ex")public void afterReturning(JoinPoint joinPoint, Exception ex) {String methodName = joinPoint.getSignature().getName();System.out.println("The method " +  methodName + " occures exception with: " + ex);}/*** 環繞通知需要攜帶ProceedingJoinPoint類型的參數* 環繞通知類似于動態代理的全過程:ProceedingJoinPoint類型的參數可以決定是否執行目標方法* 且環繞通知必須有返回值,返回值為目標方法的返回值* @param proceedingJoinPoint*/@Around("execution(public int ArithmeticCalculator.*(int, int))")public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) {Object result = null;String methodName = proceedingJoinPoint.getSignature().getName();//執行目標方法try {//前置通知System.out.println("The method " +  methodName + " begins with " + Arrays.asList(proceedingJoinPoint.getArgs()));result = proceedingJoinPoint.proceed();//返回通知System.out.println("The method ends with " + result);} catch (Throwable throwable) {//異常通知System.out.println("The method " +  methodName + " occures exception with: " + throwable);throw new RuntimeException(throwable);}//后置通知System.out.println("The method " +  methodName + " ends");return result;}
}
package com.cn.spring.aop.impl;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;import java.util.Arrays;/*** Created by jecyhw on 2015/6/21.*/
@Order(1)
@Aspect
@Component
public class ValidationAspect {@Before("execution(public int ArithmeticCalculator.*(..))")public void validateArgs(JoinPoint joinPoint) {System.out.println("validate:" + Arrays.asList(joinPoint.getArgs()));}
}
package com.cn.spring.aop.impl;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {//1.創建Spring的IOC容器ApplicationContext ctx = new ClassPathXmlApplicationContext("17-1.xml");//2.從IOC容器中huo獲取bean的實例ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);//3.使用beanint result = arithmeticCalculator.add(3, 6);System.out.println("result:" + result);//result = arithmeticCalculator.div(3, 0);// System.out.println("result:" + result);
    }
}
<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><context:component-scan base-package="com.cn.spring.aop.impl"></context:component-scan><!--使AspjectJ注解起作用:自動為匹配的類生成代理對象--><aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

?

?

轉載于:https://www.cnblogs.com/jecyhw/p/4595542.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/147377.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息