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

spring后置通知@AfterReturning的使用

 更新時間:2024年05月10日 10:05:43   作者:放肆的青春゛つ  
這篇文章主要介紹了spring后置通知@AfterReturning的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

后置通知

在目標方法執(zhí)行之后,增加的業(yè)務(wù)功能,由于目標方法執(zhí)行之后執(zhí)行,所有可以獲取到目標方法返回值,該注解是 returning屬性就是用于指定接收方法返回值的變量名的。

所有被注解為后置通知的方法,除了可以加入JoinPoint參數(shù)外,還可以包含一個用于接收返回值的變量,該變量最好使用Object類型的,目標方法的返回值可以是任何類型的。 

后置定義方法,方法是實現(xiàn)切面功能 

方法定義要求

  • public公共方法
  • 方法沒有返回值 void
  • 方法名稱自定義
  • 方法有參數(shù),推薦使用Object,參數(shù)名自定義,用于接收目標方法的返回值

屬性

  • value 切入點表達式
  • returning 自定義的變量,表示目標方法的返回值的
  • 自定義變量名必須和通知方法的形參名一樣

位置:在方法定義的上面 

特點:

  • 1 . 在目標方法之后執(zhí)行的
  • 2. 能夠獲取到目標方法的返回值,可以根據(jù)這個返回值做不同的處理操作,可以修改這個返回值
  • 3. 可以修改這個返回值

接口類

public interface Someservice {
    String doOther(String name);
    stdent doOther2(String anme,int age);
}

接口實現(xiàn)類

@Component("SomeserviceImpl")
public class SomeserviceImpl implements Someservice {
    @Override
    public String doOther(String name) {
        System.out.println("------目標方法執(zhí)行doOther()-------");
        return name;
    }

    @Override
    public stdent doOther2(String name, int age) {
        System.out.println("------目標方法執(zhí)行doOther()-------");
        stdent st = new stdent();
        st.setAge(age);
        st.setName(name);
        return st;
    }
}

增加業(yè)務(wù)功能類

@Component("myAspect2")
@Aspect
public class MyaspectJ {
    @AfterReturning(value ="execution(* *..SomeserviceImpl.doOther(..))",returning = "res")
    public void myaspectJ(Object res){
        System.out.println("后置通知的返回值為:"+res);
        res = "18204229-"+res;
        System.out.println("修改之后的:"+res);
    }

    @AfterReturning(value ="execution(* *..SomeserviceImpl.doOther2(..))",returning = "res")
    public void myaspectJ2(JoinPoint joinPoint ,Object res){
        System.out.println(joinPoint.getSignature());
        System.out.println(joinPoint.getSignature().getName());
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println(arg);
        }
        stdent stdent = new stdent();
        stdent.setAge(22);
        stdent.setName("44455");
        res = stdent;
        System.out.println("后置通知中:"+res);
    }

}

returning = "res"這個的變量res要和Object res的res命名一樣

主配置文件:

<?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="cn.com.Ycy.spring_aspectJ.bao02"/>
    <aop:aspectj-autoproxy/>
</beans>

測試類

    @Test
    public void test02(){
        String config="ApplicationContesxt1.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        //從容器獲取目標對象
        cn.com.Ycy.spring_aspectJ.bao02.Someservice someservice = (cn.com.Ycy.spring_aspectJ.bao02.Someservice) ac.getBean("SomeserviceImpl");
        //通過代理對象執(zhí)行方法,實現(xiàn)目標方法執(zhí)行時,增強了功能
        String str = someservice.doOther("ycy");
        System.out.println(str);
    }
    //返回一個對象,是否發(fā)生改變
    @Test
    public void test03(){
        String config="ApplicationContesxt1.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        //從容器獲取目標對象
        cn.com.Ycy.spring_aspectJ.bao02.Someservice someservice = (cn.com.Ycy.spring_aspectJ.bao02.Someservice) ac.getBean("SomeserviceImpl");
        //通過代理對象執(zhí)行方法,實現(xiàn)目標方法執(zhí)行時,增強了功能
        stdent str = someservice.doOther2("ycy",24);
        System.out.println(str);
        //someService
    }

注意:

在后置通知中也是可以使用JoinPoint的,并且這個必須放在第一個位置

@Component("myAspect2")
@Aspect
public class MyaspectJ {
@AfterReturning(value ="execution(* *..SomeserviceImpl.doOther2(..))",returning = "res")
    public void myaspectJ2(JoinPoint joinPoint ,Object res){
        System.out.println(joinPoint.getSignature());
        System.out.println(joinPoint.getSignature().getName());
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println(arg);
        }
}

總結(jié)

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

相關(guān)文章

  • 淺談Hibernate n+1問題

    淺談Hibernate n+1問題

    這篇文章主要介紹了淺談Hibernate n+1問題,怎么解決n+1問題,文中也作了簡要分析,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • log4j如何根據(jù)變量動態(tài)生成文件名

    log4j如何根據(jù)變量動態(tài)生成文件名

    這篇文章主要介紹了log4j如何根據(jù)變量動態(tài)生成文件名方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java Management Extensions管理擴展原理解析

    Java Management Extensions管理擴展原理解析

    這篇文章主要介紹了Java Management Extensions管理擴展原理解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • java?JIT調(diào)優(yōu)的實現(xiàn)

    java?JIT調(diào)優(yōu)的實現(xiàn)

    JIT編譯器調(diào)優(yōu)方法包括啟用JIT日志、優(yōu)化熱點代碼、循環(huán)展開、內(nèi)聯(lián)優(yōu)化、逃逸分析以及使用性能分析工具等,本文主要介紹了java?JIT調(diào)優(yōu)的實現(xiàn),感興趣的可以了解一下
    2025-02-02
  • JPA中@JoinColumn的name和referencedColumnName屬性的區(qū)別及說明

    JPA中@JoinColumn的name和referencedColumnName屬性的區(qū)別及說明

    這篇文章主要介紹了JPA中@JoinColumn的name和referencedColumnName屬性的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • SpringBoot 配置文件中配置的中文,程序讀取出來是亂碼的解決

    SpringBoot 配置文件中配置的中文,程序讀取出來是亂碼的解決

    這篇文章主要介紹了SpringBoot 配置文件中配置的中文,程序讀取出來是亂碼的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • SpringMVC攔截器超詳細解讀

    SpringMVC攔截器超詳細解讀

    SpringMVC的處理器攔截器,類似于Servlet開發(fā)中的過濾器Filter,用于對處理器進行預(yù)處理和后處理。?依賴于web框架,在實現(xiàn)上基于Java的反射機制,屬于面向切面編程(AOP)的一種運用
    2022-07-07
  • mybatis plus 自動轉(zhuǎn)駝峰配置小結(jié)

    mybatis plus 自動轉(zhuǎn)駝峰配置小結(jié)

    SpringBoot提供兩種配置Mybatis的方式,第一種是通過yml或application.properties文件開啟配置,第二種是使用自定義配置類,通過給容器添加一個ConfigurationCustomizer來實現(xiàn)更靈活的配置,這兩種方法可以根據(jù)項目需求和個人喜好選擇使用
    2024-10-10
  • Java接口和抽象類的區(qū)別深入剖析

    Java接口和抽象類的區(qū)別深入剖析

    這篇文章主要介紹了Java接口和抽象類的區(qū)別,對于Java的初學(xué)者來說是需要準確掌握的概念!
    2014-07-07
  • Spring 使用Validation 驗證框架的問題詳解

    Spring 使用Validation 驗證框架的問題詳解

    Spring Boot在內(nèi)部通過集成hibernate-validation已經(jīng)實現(xiàn)了JSR-349驗證規(guī)范接口,在Spring Boot項目中只要直接使用就行了。 一般用在Controller中用于驗證前端傳來的參數(shù)。這篇文章給大家介紹Spring Validation 驗證框架的相關(guān)知識,感興趣的朋友一起看看吧
    2021-07-07

最新評論