最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

本文只是將重點(diǎn)說下 執(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 不會(huì)執(zhí)行,其它的通知正常
        joinPoint.proceed();
        //執(zhí)行方法之后,如果joinPoint.proceed() 拋出了異常,則該句不會(huì)執(zhí)行,拋出異常后直接跳出了aroud方法了
        System.out.println("LogAspectA#around結(jié)束...");
    }
    //后置通知(只要連接點(diǎn)被執(zhí)行,不管是否拋出異常)
    @After("execution(public void com.yuki.demo.aop.aspect.LoginController.*(String,String))")
    public void after() {
        System.out.println("LogAspect#after...");
    }
    //異常通知(只有在joinPoint.proceed()方法執(zhí)行向外面拋出了異常,才會(huì)執(zhí)行該通知)
    @AfterThrowing("execution(public void com.yuki.demo.aop.aspect.LoginController.*(String,String))")
    public void afterThrowing() {
        System.out.println("LogAspect#afterThrowing...");
    }
    //正常的返回通知通知(正常結(jié)束了才會(huì)執(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é)束...");
    }
}

【啟動(dòng)配置】

省略。。。非重點(diǎn)

【測試類】

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

【控制臺(tái)輸出】

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();         //連接點(diǎn)所在位置的相關(guān)信息
	    String toShortString();     //連接點(diǎn)所在位置的簡短相關(guān)信息
	    String toLongString();     //連接點(diǎn)所在位置的全部相關(guān)信息
	    Object getThis();         //返回AOP代理對象
	    Object getTarget();       //返回目標(biāo)對象
	    Object[] getArgs();       //返回被通知方法參數(shù)列表
	    Signature getSignature();  //返回當(dāng)前連接點(diǎn)簽名
	    SourceLocation getSourceLocation();//返回連接點(diǎn)方法所在類文件中的位置
	    String getKind();        //連接點(diǎn)類型
	    StaticPart getStaticPart(); //返回連接點(diǎn)靜態(tài)部分
	}*/	
	
    //定義前置通知,注意這里是sayHello2
	//使用@Before進(jìn)行前置通知聲明,其中value用于定義切入點(diǎn)表達(dá)式或引用命名切入點(diǎn)
	@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:指定切入點(diǎn)表達(dá)式或命名切入點(diǎn);
    pointcut:同樣是指定切入點(diǎn)表達(dá)式或命名切入點(diǎn),如果指定了將覆蓋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();         //連接點(diǎn)所在位置的相關(guān)信息
	    String toShortString();     //連接點(diǎn)所在位置的簡短相關(guān)信息
	    String toLongString();     //連接點(diǎn)所在位置的全部相關(guān)信息
	    Object getThis();         //返回AOP代理對象
	    Object getTarget();       //返回目標(biāo)對象
	    Object[] getArgs();       //返回被通知方法參數(shù)列表
	    Signature getSignature();  //返回當(dāng)前連接點(diǎn)簽名
	    SourceLocation getSourceLocation();//返回連接點(diǎn)方法所在類文件中的位置
	    String getKind();        //連接點(diǎn)類型
	    StaticPart getStaticPart(); //返回連接點(diǎn)靜態(tài)部分
	}*/	
	
    //定義前置通知,注意這里是sayHello2
	//使用@Before進(jìn)行前置通知聲明,其中value用于定義切入點(diǎn)表達(dá)式或引用命名切入點(diǎn)
	@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:指定切入點(diǎn)表達(dá)式或命名切入點(diǎn);
    pointcut:同樣是指定切入點(diǎn)表達(dá)式或命名切入點(diǎn),如果指定了將覆蓋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"/>

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

相關(guān)文章

  • Springboot3整合Mybatis-plus3.5.3報(bào)錯(cuò)問題解決

    Springboot3整合Mybatis-plus3.5.3報(bào)錯(cuò)問題解決

    在日常學(xué)習(xí)springboot3相關(guān)的代碼時(shí),在使用 SpringBoot3 整合 MyBatisplus 時(shí)出現(xiàn)了一些問題,花了不少時(shí)間處理,這篇文章主要介紹了Springboot3整合Mybatis-plus3.5.3報(bào)錯(cuò)問題解決,需要的朋友可以參考下
    2023-11-11
  • JSON.toJSONString()空字段不忽略修改的問題

    JSON.toJSONString()空字段不忽略修改的問題

    這篇文章主要介紹了JSON.toJSONString()空字段不忽略修改的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • JAVA正則表達(dá)式校驗(yàn)qq號碼的方法

    JAVA正則表達(dá)式校驗(yàn)qq號碼的方法

    Java作為一種開發(fā)語言,有許多值得推薦的地方,但是它一直以來沒有自帶對正則表達(dá)式的支持。下面小編給大家?guī)砹薐AVA正則表達(dá)式校驗(yàn)qq號碼的方法,需要的朋友參考下吧
    2018-04-04
  • Mybatis?Mapper中多參數(shù)方法不使用@param注解報(bào)錯(cuò)的解決

    Mybatis?Mapper中多參數(shù)方法不使用@param注解報(bào)錯(cuò)的解決

    這篇文章主要介紹了Mybatis?Mapper中多參數(shù)方法不使用@param注解報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
    2022-01-01
  • Spring?Boot分離配置文件的多種方式總結(jié)

    Spring?Boot分離配置文件的多種方式總結(jié)

    Spring Boot可以外部化程序配置,以便可以在不同環(huán)境中使用相同的應(yīng)用程序代碼;當(dāng)然Spring Boot可以將配置文件進(jìn)行拆分,以便于激活不同的運(yùn)行環(huán)境,下面這篇文章主要給大家總結(jié)介紹了關(guān)于Spring?Boot分離配置文件的多種方式,需要的朋友可以參考下
    2022-11-11
  • JavaWeb中轉(zhuǎn)發(fā)與重定向的區(qū)別小結(jié)

    JavaWeb中轉(zhuǎn)發(fā)與重定向的區(qū)別小結(jié)

    轉(zhuǎn)發(fā)和重定向是JavaWeb中常用的兩種頁面跳轉(zhuǎn)方式,它們在實(shí)現(xiàn)上有一些區(qū)別,本文主要介紹了JavaWeb中轉(zhuǎn)發(fā)與重定向的區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • Java平閏年判斷的方法總結(jié)

    Java平閏年判斷的方法總結(jié)

    本篇文章給大家整理了Java平閏年判斷的兩種方法,大家在寫程序的時(shí)候如果用的到參考下吧。
    2018-02-02
  • Java 實(shí)戰(zhàn)項(xiàng)目之精美物流管理系統(tǒng)的實(shí)現(xiàn)流程

    Java 實(shí)戰(zhàn)項(xiàng)目之精美物流管理系統(tǒng)的實(shí)現(xiàn)流程

    讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+Vue+maven+Mysql實(shí)現(xiàn)一個(gè)精美的物流管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平
    2021-11-11
  • SpringBoot工程中Spring Security應(yīng)用實(shí)踐記錄流程分析

    SpringBoot工程中Spring Security應(yīng)用實(shí)踐記錄流程分析

    Spring Security是一個(gè)能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問控制解決方案的安全框架。這篇文章主要介紹了SpringBoot工程中Spring Security應(yīng)用實(shí)踐,需要的朋友可以參考下
    2021-09-09
  • 高并發(fā)下restTemplate的錯(cuò)誤分析方式

    高并發(fā)下restTemplate的錯(cuò)誤分析方式

    這篇文章主要介紹了高并發(fā)下restTemplate的錯(cuò)誤分析方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10

最新評論

灵璧县| 商洛市| 长岭县| 郧西县| 赫章县| 浙江省| 郁南县| 云安县| 成武县| 京山县| 长沙市| 鹿邑县| 文登市| 靖江市| 商城县| 石门县| 湖州市| 铜陵市| 樟树市| 临猗县| 房产| 澜沧| 崇左市| 涟水县| 融水| 金乡县| 凤台县| 左云县| 体育| 武汉市| 贵定县| 霍林郭勒市| 酒泉市| 黄骅市| 连州市| 高碑店市| 宾阳县| 札达县| 盱眙县| 舞钢市| 绥江县|