(六)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/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://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 { @Test
public 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

Spring学习(22)--- AOP之Advice应用(下)的更多相关文章

  1. Spring学习之AOP的实现方式

    Spring学习之AOP的三种实现方式 一.介绍AOP 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能 ...

  2. spring学习(二) ———— AOP之AspectJ框架的使用

    前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...

  3. Spring学习之AOP总结帖

    AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...

  4. Spring学习之Aop的基本概念

    转自:http://my.oschina.net/itblog/blog/209067 AOP的基本概念 AOP从运行的角度考虑程序的流程,提取业务处理过程的切面.AOP面向的是程序运行中的各个步骤, ...

  5. Spring学习之Aop的各种增强方法

    AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5一样的注解,但并没有使用AspectJ的 ...

  6. Spring学习之AOP与事务

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

  7. spring学习笔记-AOP

    1.aop:aspect oriented programming 面向切面编程 2.aop在spring中的作用:   提供声明式服务(声明式事务) 允许用户实现自定义切面 3.aop:在不改变原有 ...

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

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

  9. Spring学习之AOP

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

  10. Spring 学习之AOP

    1. 走进面前切面编程 编程范式: 面向过程编程,c语言: 面向对象编程:c++,java,c#; 函数式编程: 事件驱动编程: 面向切面编程: AOP是一种编程范式,不是编程语言:解决特定问题,不能 ...

随机推荐

  1. java线程控制安全

    synchronized() 在线程运行的时候,有时会出现线程安全问题例如:买票程序,有可能会出现不同窗口买同一张编号的票 运行如下代码: public class runable implement ...

  2. 一、AspNet Core通过控制台编译程序的基本指令:

    1.先创建文件夹 mkdir "文件夹"2.在对应的文件夹里边 用 dotnet new 命令创建了Program.cs和project.json俩个文件3.使用 dotnet r ...

  3. CSS3 制作网格动画效果

    在线演示      源码下载

  4. [KISSY5系列]KISSY5安装使用(一)

    本文将从零开始安装KISSY环境 一.安装nodejs 从nodejs网站下载nodejs安装 地址: https://nodejs.org/en/download/ 二.下载KISSY 下载地址:  ...

  5. python2与python3的不兼容_urllib2

    网页下载器:将URL对应的网页以HTML下载到本地,用于后续分析 常见网页下载器:Python官方基础模块:urllib2 第三方功能包:requests python 3.x中urllib库和uri ...

  6. ubuntu下搭建nginx+mysql+php-fpm站点

    概述 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器.  nginx的优势在于能以低内存高 ...

  7. javascript 表达式和运算符 (二)

    表达式是一种JS短语,可使JS解释器用来产生一个值. 一.表达式 表达式分类 1.原始表达式 常量.直接量 (3.14,"test"); 关键字 (null,this,true): ...

  8. 用ng-view创建单页APP

    我们假设我们有一个单页面的程序,并且想为这个页面添加动画效果.点击某一个链接会将一个试图滑出,同时将另一个试图滑入. 我们将会使用: 使用 ngRoute 来为我们的页面路由 使用 ngAnimate ...

  9. hadoop集群的节点启动问题

    start-all.sh 启动集群时,NameNode或DataNode节点启动不了,但之前可以启动. 查看hadoop中hdfs-site.xml配置文件 <property> < ...

  10. Commonjs规范及Node模块实现

    前面的话 Node在实现中并非完全按照CommonJS规范实现,而是对模块规范进行了一定的取舍,同时也增加了少许自身需要的特性.本文将详细介绍NodeJS的模块实现 引入 nodejs是区别于java ...