欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談@Aspect@Order各個通知的執(zhí)行順序

 更新時間:2022年02月14日 11:37:43   作者:快樂妮子  
這篇文章主要介紹了@Aspect@Order各個通知的執(zhí)行順序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

@Aspect@Order各個通知的執(zhí)行順序

兩個切面類:【記錄日志】和【判斷參數(shù)】,分別對應(yīng)順序 @Order(0) 和@Order(1) 。

本文只是將重點說下 執(zhí)行順序 這么回事哈哈哈

代碼

【業(yè)務(wù)類】

/**
 * 登錄控制器
 */
@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...");
    }
    //環(huán)繞通知
    //ProceedingJoinPoint 只有環(huán)繞通知有
    @Around("execution(public void com.yuki.demo.aop.aspect.LoginController.*(String,String))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("LogAspectA#around開始...");
        //代理方法的執(zhí)行,如果沒有joinPoint.proceed() ,則前置通知@Before 不會執(zhí)行,其它的通知正常
        joinPoint.proceed();
        //執(zhí)行方法之后,如果joinPoint.proceed() 拋出了異常,則該句不會執(zhí)行,拋出異常后直接跳出了aroud方法了
        System.out.println("LogAspectA#around結(jié)束...");
    }
    //后置通知(只要連接點被執(zhí)行,不管是否拋出異常)
    @After("execution(public void com.yuki.demo.aop.aspect.LoginController.*(String,String))")
    public void after() {
        System.out.println("LogAspect#after...");
    }
    //異常通知(只有在joinPoint.proceed()方法執(zhí)行向外面拋出了異常,才會執(zhí)行該通知)
    @AfterThrowing("execution(public void com.yuki.demo.aop.aspect.LoginController.*(String,String))")
    public void afterThrowing() {
        System.out.println("LogAspect#afterThrowing...");
    }
    //正常的返回通知通知(正常結(jié)束了才會執(zhí)行該通知)
    @AfterReturning("execution(public void com.yuki.demo.aop.aspect.LoginController.*(String,String))")
    public void afterReturning() {
        System.out.println("LogAspect#afterReturning...");
    }
}

【切面類】

/**
 * 判斷請求參數(shù)的sign是否正確的 切面類
 */
@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結(jié)束...");
    }
}

【啟動配置】

省略。。。非重點

【測試類】

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

【控制臺輸出】

LogAspectA#around開始...
LogAspect#before...
SignAspect#around開始...
LoginController#login...
SignAspect#around結(jié)束...
LogAspectA#around結(jié)束...
LogAspect#after...
LogAspect#afterReturning...

小結(jié)

spring AspectJ order(順序)

@Aspect
@Order(2)
public class HelloWorldAspectAnnotation {
	/**
	 * JoinPoint接口
	 * @param joinPoint
	 */
	/*public interface JoinPoint {
	    String toString();         //連接點所在位置的相關(guān)信息
	    String toShortString();     //連接點所在位置的簡短相關(guān)信息
	    String toLongString();     //連接點所在位置的全部相關(guān)信息
	    Object getThis();         //返回AOP代理對象
	    Object getTarget();       //返回目標對象
	    Object[] getArgs();       //返回被通知方法參數(shù)列表
	    Signature getSignature();  //返回當前連接點簽名
	    SourceLocation getSourceLocation();//返回連接點方法所在類文件中的位置
	    String getKind();        //連接點類型
	    StaticPart getStaticPart(); //返回連接點靜態(tài)部分
	}*/	
	
    //定義前置通知,注意這里是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");
	}
	/*value:指定切入點表達式或命名切入點;
    pointcut:同樣是指定切入點表達式或命名切入點,如果指定了將覆蓋value屬性指定的,pointcut具有高優(yōu)先級;*/
	@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 {
	/**
	 * JoinPoint接口
	 * @param joinPoint
	 */
	/*public interface JoinPoint {
	    String toString();         //連接點所在位置的相關(guān)信息
	    String toShortString();     //連接點所在位置的簡短相關(guān)信息
	    String toLongString();     //連接點所在位置的全部相關(guān)信息
	    Object getThis();         //返回AOP代理對象
	    Object getTarget();       //返回目標對象
	    Object[] getArgs();       //返回被通知方法參數(shù)列表
	    Signature getSignature();  //返回當前連接點簽名
	    SourceLocation getSourceLocation();//返回連接點方法所在類文件中的位置
	    String getKind();        //連接點類型
	    StaticPart getStaticPart(); //返回連接點靜態(tài)部分
	}*/	
	
    //定義前置通知,注意這里是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("=======================");		
	}
	
	/*value:指定切入點表達式或命名切入點;
    pointcut:同樣是指定切入點表達式或命名切入點,如果指定了將覆蓋value屬性指定的,pointcut具有高優(yōu)先級;*/
	@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"/>

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java實現(xiàn)Excel轉(zhuǎn)換為圖片

    java實現(xiàn)Excel轉(zhuǎn)換為圖片

    在實際開發(fā)過程中,經(jīng)常會有這樣的需求,需要將Excel表格或特定區(qū)域轉(zhuǎn)換為圖片,所以小編今天就來為大家介紹一下如何使用Java將Excel轉(zhuǎn)化為圖片吧
    2023-10-10
  • SpringBoot前后端分離實現(xiàn)個人博客系統(tǒng)

    SpringBoot前后端分離實現(xiàn)個人博客系統(tǒng)

    這篇文章主要為大家詳細介紹了使用springboot+mybatis+前端vue,使用前后端分離架構(gòu)實現(xiàn)的個人博客系統(tǒng),感興趣的小伙伴可以動手嘗試一下
    2022-06-06
  • Spring中@Scheduled功能的使用方法詳解

    Spring中@Scheduled功能的使用方法詳解

    @Scheduled 由Spring定義,用于將方法設(shè)置為調(diào)度任務(wù),下面這篇文章主要給大家介紹了關(guān)于Spring中@Scheduled功能的使用方法,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-04-04
  • Java面向?qū)ο笾甪inal關(guān)鍵字詳細解讀

    Java面向?qū)ο笾甪inal關(guān)鍵字詳細解讀

    這篇文章主要介紹了Java面向?qū)ο笾甪inal關(guān)鍵字詳細解讀,final修飾的屬性又叫常量,一般用 XX_XX_XX來命名,final修飾的屬性在定義時必須賦初始值,并且以后不能再修改,需要的朋友可以參考下
    2024-01-01
  • Spring Cloud Feign組成配置過程解析

    Spring Cloud Feign組成配置過程解析

    這篇文章主要介紹了Spring Cloud Feign組成配置過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • Spring Security如何基于Authentication獲取用戶信息

    Spring Security如何基于Authentication獲取用戶信息

    這篇文章主要介紹了Spring Security如何基于Authentication獲取用戶信息,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • Spring Boot 2 整合 QuartJob 實現(xiàn)定時器實時管理功能

    Spring Boot 2 整合 QuartJob 實現(xiàn)定時器實時管理功能

    Quartz是一個完全由java編寫的開源作業(yè)調(diào)度框架,形式簡易,功能強大。接下來通過本文給大家分享Spring Boot 2 整合 QuartJob 實現(xiàn)定時器實時管理功能,感興趣的朋友一起看看吧
    2019-11-11
  • 微信小程序--Ble藍牙

    微信小程序--Ble藍牙

    本文主要介紹了微信小程序--Ble藍牙的實現(xiàn)方法。文中附上源碼下載,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • Java如何實現(xiàn)URL帶請求參數(shù)(get/post)及得到get和post請求url和參數(shù)列表的方法

    Java如何實現(xiàn)URL帶請求參數(shù)(get/post)及得到get和post請求url和參數(shù)列表的方法

    本文給大家介紹Java如何實現(xiàn)URL帶請求參數(shù)(get/post)及得到get和post請求url和參數(shù)列表的方法,涉及到 java獲取post請求參數(shù)的方法,感興趣的朋友一起看看吧
    2015-10-10
  • 使用java實現(xiàn)網(wǎng)絡(luò)爬蟲

    使用java實現(xiàn)網(wǎng)絡(luò)爬蟲

    這篇文章主要介紹了使用java實現(xiàn)網(wǎng)絡(luò)爬蟲,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評論