基于Annotation的Spring AOP: @AfterReturning

来源:互联网 发布:三维家软件下载 编辑:程序博客网 时间:2024/06/07 15:39

AfterReturning 增强处理将在目标方法正常完成后被织入。

使用@AfterReturning可指定如下两个属性:

① pointcut / value : 两者都用于指定该切入点对应的切入表达式

returning : 指定一个返回值形参名,增强处理定义的方法可通过该行参名来访问目标方法的返回值。

Person.java :

public interface Person {public String sayHello(String name);public void eat(String food);}
Chinese.java :

import org.springframework.stereotype.Component;@Componentpublic class Chinese implements Person {@Overridepublic void eat(String food) {System.out.println("我正在吃:"+food);}@Overridepublic String sayHello(String name) {System.out.println("sayHello方法被执行了");return name+"Hello,Spring AOP";}}
AfterReturningAdviceTest.java :

import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.Aspect;/** * 定义一个切面 * @author Administrator * */@Aspectpublic class AfterReturningAdviceTest {@AfterReturning(returning="rvt",pointcut="execution(* com.bean.*.*(..))")public void log(Object rvt){System.out.println("获取目标方法返回值:"+rvt);System.out.println("模拟记录日志的功能...");}}
bean.xml :

<?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"        xmlns:tx="http://www.springframework.org/schema/tx"        xsi:schemaLocation="http://www.springframework.org/schema/beans                 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd                http://www.springframework.org/schema/context                 http://www.springframework.org/schema/context/spring-context-2.5.xsd                http://www.springframework.org/schema/tx                http://www.springframework.org/schema/tx/spring-tx-2.5.xsd                http://www.springframework.org/schema/aop                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">            <context:component-scan base-package="com.bean">        <context:include-filter type="annotation"                  expression="org.aspectj.lang.annotation.Aspect"/>    </context:component-scan>       <aop:aspectj-autoproxy/>     </beans>
Test.java :

public class Test {public static void main(String[] args) {ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");Person p=(Person) ctx.getBean("chinese");System.out.println(p.sayHello("张三"));p.eat("西瓜");}}
运行控制台输出:



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝4岁不说话怎么办 宜家柜子味道重怎么办 家里有小飞虫怎么办呀 汽车里进老鼠了怎么办 老鼠跑到车里了怎么办 我的小车有老鼠怎么办 车里面进了老鼠怎么办 街电充电宝丢了怎么办 充电宝充不了电怎么办 脚裸扭伤肿了怎么办 大货车电瓶亏电怎么办 货车电瓶被偷了怎么办 小乌龟尾巴断了怎么办 长青春痘怎么办简单小妙招 一关灯就有蚊子怎么办 狗狗误食蟑螂药怎么办 泰迪误食蟑螂药怎么办 猫咪吃了蟑螂药怎么办 狗把蟑螂药吃了怎么办 猫吃了蟑螂诱饵怎么办 猫吃了蟑螂中毒怎么办 孕妇胃烧的难受怎么办 6个月孕妇胃难受怎么办 飞机杯吸盘不上怎么办 我的车位被占用怎么办 占别人车位的车怎么办 头受凉受风了疼怎么办 看电脑时间长了眼睛疼怎么办 电脑玩久了头疼怎么办 屋里有死老鼠味怎么办 老鼠死在车里面怎么办 手机锁屏怎么办求解锁 玩lol左键失灵怎么办 小米鼠标没电了怎么办 电脑键盘鼠标没反应怎么办 win10玩游戏很卡怎么办 鞋子夹脚怎么办小妙招 新鞋两边夹脚怎么办 新鞋子两边夹脚怎么办 鞋子瘦两边夹脚怎么办 鞋子小脚趾磨脚怎么办