基于Annotation的Spring AOP: @AfterReturning

news/2024/7/24 6:32:59

基于Annotation的Spring AOP: @AfterReturning

2013年08月03日 06:20:37

阅读数:7916

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

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

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

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

Person.java :

 

 
  1. public interface Person {

  2. public String sayHello(String name);

  3. public void eat(String food);

  4. }

Chinese.java :

 

 

 
  1. import org.springframework.stereotype.Component;

  2.  
  3. @Component

  4. public class Chinese implements Person {

  5.  
  6. @Override

  7. public void eat(String food) {

  8. System.out.println("我正在吃:"+food);

  9. }

  10.  
  11. @Override

  12. public String sayHello(String name) {

  13. System.out.println("sayHello方法被执行了");

  14. return name+"Hello,Spring AOP";

  15. }

  16.  
  17. }

AfterReturningAdviceTest.java :

 

 

 
  1. import org.aspectj.lang.annotation.AfterReturning;

  2. import org.aspectj.lang.annotation.Aspect;

  3.  
  4. /**

  5. * 定义一个切面

  6. * @author Administrator

  7. *

  8. */

  9. @Aspect

  10. public class AfterReturningAdviceTest {

  11.  
  12. @AfterReturning(returning="rvt",pointcut="execution(* com.bean.*.*(..))")

  13. public void log(Object rvt){

  14. System.out.println("获取目标方法返回值:"+rvt);

  15. System.out.println("模拟记录日志的功能...");

  16. }

  17.  
  18. }

bean.xml :

 

 

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xmlns:aop="http://www.springframework.org/schema/aop"

  6. xmlns:tx="http://www.springframework.org/schema/tx"

  7. xsi:schemaLocation="http://www.springframework.org/schema/beans

  8. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

  9. http://www.springframework.org/schema/context

  10. http://www.springframework.org/schema/context/spring-context-2.5.xsd

  11. http://www.springframework.org/schema/tx

  12. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

  13. http://www.springframework.org/schema/aop

  14. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

  15.  
  16.  
  17. <context:component-scan base-package="com.bean">

  18. <context:include-filter type="annotation"

  19. expression="org.aspectj.lang.annotation.Aspect"/>

  20. </context:component-scan>

  21.  
  22. <aop:aspectj-autoproxy/>

  23.  
  24. </beans>

Test.java :

 

 

 
  1. public class Test {

  2. public static void main(String[] args) {

  3.  
  4. ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");

  5. Person p=(Person) ctx.getBean("chinese");

  6. System.out.println(p.sayHello("张三"));

  7. p.eat("西瓜");

  8. }

  9. }

运行控制台输出:

 


http://www.niftyadmin.cn/n/1529070.html

相关文章

jqgrid应用过程中的问题

jqGrid 是一个用来显示网格数据的jQuery插件&#xff0c;通过使用jqGrid可以轻松实现前端页面与后台数据的ajax异步通信。文档比较全面&#xff0c;其官方网址为&#xff1a;http://www.trirand.com。一、jqGrid特性&#xff1a; Ajax分页&#xff0c;可以控制每页显示的记录数…

spring的proxy-target-class详解

spring的proxy-target-class详解 2017年08月01日 08:18:16 阅读数&#xff1a;9564 proxy-target-class属性值决定是基于接口的还是基于类的代理被创建。首先说明下proxy-target-class"true"和proxy-target-class"false"的区别&#xff0c;为true则是基…

computed (计算属性) 和 methods (方法) 的区别

methods: vue 里面可以放函数的地方 <div id"div"> <p >{{message}}</p> <button id"btn" v-on:click"change">点击翻译成中文</button> </div><script src"https://cdn.jsdelivr.net/npm/vue/d…

windows 安装node.js

第一步&#xff1a;下载nodejs&#xff0c;地址 http://www.nodejs.org/download/ 打开后选择与自己相匹配的 rc 路径下 我选择的是node-v4.4.3-x86.msi 第二步&#xff1a; 安装 node.js 下载完成之后&#xff0c;双击"node-v4.4.3-x86.msi"&#xff0c;开始…

vue 搭建

Vue.js是一套构建用户界面的 “渐进式框架”。与其他重量级框架不同的是&#xff0c;Vue 采用自底向上增量开发的设计。Vue 的核心库只关注视图层&#xff0c;并且非常容易学习&#xff0c;非常容易与其它库或已有项目整合。2016年&#xff0c;Vue同Angular、React形成三足鼎立…

computed 的get 和set

当你读取一个变量的时候会触发该变量的getter. 当你修改该变量时候会触发他的setter. <html> <head> <meta http-equiv"Content-Type" content"text/html; charsetgb2312" /> <title>无标题文档</title> <script src&quo…

详细图解mongoDB下载,安装,配置与使用

转载 原文地址 详细图解&#xff0c;记录 win7 64 安装mongo数据库的过程。安装的版本是 MongoDB-win32-x86_64-2008plus-ssl-3.4.1-signed。 我下载的源文件&#xff1a;mongodb-win32-x86_64-2008plus-ssl-3.4.1-signed我的系统&#xff1a;win 7 64 bit 8g内存为了方便下载&…

v-bind 绑定class 和 style 的几种方法

这篇文章介绍v-bind 绑定html 样式的方法。包括绑定class 和style&#xff08;内联样式&#xff09;的方法 ● 绑定class有2种语法 &#xff08;一、对象语法&#xff1a;1.数据属性对象、2.计算属性对象&#xff1b;二、数组语法&#xff09; ● 绑定style有3种语法 &…