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

spring后置通知@AfterReturning的使用

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

后置通知

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

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

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

方法定義要求

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

屬性

  • value 切入點(diǎn)表達(dá)式
  • returning 自定義的變量,表示目標(biāo)方法的返回值的
  • 自定義變量名必須和通知方法的形參名一樣

位置:在方法定義的上面 

特點(diǎn):

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

接口類

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

接口實(shí)現(xiàn)類

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

    @Override
    public stdent doOther2(String name, int age) {
        System.out.println("------目標(biāo)方法執(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"這個(gè)的變量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>

測(cè)試類

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

注意:

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

@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é)

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

相關(guān)文章

最新評(píng)論

康保县| 屯留县| 马龙县| 闸北区| 万全县| 上饶市| 拉孜县| 阿克苏市| 平舆县| 当涂县| 南涧| 个旧市| 逊克县| 仁寿县| 黄龙县| 奉新县| 云南省| 海伦市| 界首市| 蓬安县| 盈江县| 承德市| 呼和浩特市| 福泉市| 左权县| 观塘区| 金乡县| 周至县| 通州区| 邛崃市| 连城县| 湖南省| 鄂托克前旗| 永修县| 祁连县| 苏尼特右旗| 浦北县| 宜黄县| 赫章县| 镇江市| 元氏县|