(六)Advice parameters(advice带参数的情况)

例子:

修改MyAspect(添加around_init方法):

package com.aop.schema;import org.aspectj.lang.ProceedingJoinPoint;/**
*
* 切面类
*
*/
public class MyAspect {public void before(){System.out.println("MyAspect.before");}public void afterreturning(){System.out.println("MyAspect.afterreturning");}public void afterthrowing(){System.out.println("MyAspect.afterthrowing");}public void after(){System.out.println("MyAspect.after");}public void around(ProceedingJoinPoint pjp) {try {System.out.println("MyAspect.around_1");Object obj=pjp.proceed();System.out.println("MyAspect.around_2");} catch (Throwable e) {e.printStackTrace();}}public void around_init(ProceedingJoinPoint pjp,String name,int age) {System.out.println(name+"  "+age);try {System.out.println("MyAspect.around_1");Object obj=pjp.proceed();System.out.println("MyAspect.around_2");} catch (Throwable e) {e.printStackTrace();}}
}

修改ApsectBiz类(添加init方法):

package com.aop.schema;
/**
*
* 业务类
*
*/
public class ApsectBiz {public void biz(){System.out.println("ApsectBiz.biz");//throw new RuntimeException();  //故意抛出异常}public void init(String name,int age){System.out.println("ApsectBiz.init : "+ name +"  " +age);}
}

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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsd"><bean id="myAspect" class="com.aop.schema.MyAspect"></bean><bean id="apsectBiz" class="com.aop.schema.ApsectBiz"></bean><aop:config><aop:aspect id="myAspectAOP" ref="myAspect"><!--  先注释掉,便于观察结果<aop:pointcut id="myPointcut" expression="execution(* com.aop.schema.ApsectBiz.*(..))" /><aop:before method="before" pointcut-ref="myPointcut"/><aop:after-returning method="afterreturning" pointcut-ref="myPointcut"/><aop:after-throwing method="afterthrowing" pointcut-ref="myPointcut"/><aop:after method="after" pointcut-ref="myPointcut"/><aop:around method="around" pointcut-ref="myPointcut"/>--><aop:around method="around_init" pointcut="execution(* com.aop.schema.ApsectBiz.init(String,int)) and args(name,age)"/></aop:aspect></aop:config></beans>

单元测试:

package com.aop.schema;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class UnitTest {@Testpublic void test(){ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-aop.xml");ApsectBiz biz = (ApsectBiz)context.getBean("apsectBiz");biz.init("Json",25);}
}

结果:

七月 09, 2015 11:48:42 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@118e0f0f: startup date [Thu Jul 09 23:48:42 CST 2015]; root of context hierarchy
七月 09, 2015 11:48:42 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-aop.xml]
Json  25
MyAspect.around_1
ApsectBiz.init : Json  25
MyAspect.around_2

转载于:https://www.cnblogs.com/JsonShare/p/4634535.html

Spring学习(22)--- AOP之Advice应用(下)相关推荐

  1. Spring学习之AOP

    Spring-AOP(Aspect-orented programming) 在业务流程中插入与业务无关的逻辑,这样的逻辑称为Cross-cutting concerns,将Crossing-cutt ...

  2. Spring中的AOP在Advice方法中获取目标方法的参

    参考:http://my.oschina.net/itblog/blog/211693 http://christang.iteye.com/blog/2037919 http://blog.csdn ...

  3. Spring 学习二-----AOP的原理与简单实践

    一.Spring  AOP的原理 AOP全名Aspect-Oriented Programming,中文直译为面向切面(方面)编程.何为切面,就比如说我们系统中的权限管理,日志,事务等我们都可以将其看 ...

  4. Spring中的AOP——在Advice方法中获取目标方法的参数(转)

    获取目标方法的信息 访问目标方法最简单的做法是定义增强处理方法时,将第一个参数定义为JoinPoint类型,当该增强处理方法被调用时,该JoinPoint参数就代表了织入增强处理的连接点.JoinPo ...

  5. 【Spring学习】AOP实现日志记录

    AOP知识点 AOP,面向切面编程.通过预编译方式和运行时动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. AOP编程思想就是把很多类对象中的横切问题点,从业务逻辑中分离出来,减少 ...

  6. Spring 学习 day3 : AOP,Spring中JdbcTemplate的使用

    1.AOP 1.1 什么是AOP 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方 式和运行期动态代理实现程序功能的统一维护的一种技术. ...

  7. Spring学习之AOP(面向切面编程)

    动态代理 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代理类与委托类之间通常会存在关联关 ...

  8. Spring学习之AOP的切点表达式详解

    1.背景知识 在Sping中使用AOP的主要工作是配置织入关系,使用<aop:config>标签代表这一段是织入关系的配置 首先,用<aop:aspect>标签声明一个切面,这 ...

  9. Spring学习,AOP术语

    连接点(Joinpoint),切点(Poincut),增强(Advice),目标对象(Tatget),引介(Introduction),织入(Weaving),代理(Proxy),切面(Aspect)

  10. Spring 学习笔记----->AOP

    Spring 学习笔记----->AOP 代理模式 为什么学代理模式? 因为这就是Spring Aop的底层 代理模式的分类: 静态代理 动态代理 静态代理 生活用的例子: 房东 public ...

最新文章

  1. 报名软件批次分类code不能为空_技能鉴定报名软件使用说明
  2. 无密码进去mysql_技术分享 | 安全地无密码登录 MySQL
  3. linux c 查看 结构体 宏 函数 关键字定义
  4. unity快速接入第三方sdk_直播美颜SDK实现需要具备哪些条件
  5. python中cgi到底是什么_十分钟搞懂什么是CGI(转)
  6. 黑马程序员之单例模式学习
  7. img 标签 点击跳出图层_你竟然不知道cad图层也可以导出与导入?
  8. 某公司数据恢复报告书
  9. matlab浮点数求绝对值_MATLAB仿真阵列天线切比雪夫综合法(附代码)
  10. 07/11/10 资料整理
  11. Resharper插件如何启用原VS的智能提示
  12. Python+socket完美实现TCP长连接保持存活
  13. 准备创建一个自己的校验提示Extender
  14. Web前端 HTTP1.0、 HTTP 1.1 、 HTTP2.0 区别与联系
  15. 鲁大师软件测试在哪,鲁大师测网速(鲁大师网速测试在哪里)
  16. 自动化之RPA工具之UiPath
  17. Delphi 跨语言环境 乱码问题
  18. 计算机中MAX函数是求什么,MAX函数
  19. 一文了解KingSwap
  20. 离散数学序关系求解最大/小元,极大/小元,上/下届,上/下确界

热门文章

  1. 全新 Win 9 概念 界面漂亮极了
  2. scp 是我小看了你-基于密钥传输!
  3. 2013年全球ERP市场格局(Gartner)
  4. 关于json_decode乱码及NULL的解决方法
  5. 【算法学习笔记】07.数据结构基础 链表 初步练习
  6. MSP430G2553需要注意的一些参数
  7. jquery对Select的操作
  8. windows 路由设置问题
  9. Oracle如何精确计算row的大小
  10. REDHAT6.3 udev 配置 存储器磁盘