4种实例 advice aop_Spring学习(十五)----- Spring AOP通知实例 – Advice

基础

Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点。简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后添加额外的功能。

在Spring AOP中,有 4 种类型通知(advices)的支持:

通知(Advice)之前 - 该方法执行前运行.                                               (实现 MethodBeforeAdvice接口)

通知(Advice)返回之后 – 运行后,该方法返回一个结果                       (实现AfterReturningAdvice接口)

通知(Advice)抛出之后 – 运行方法抛出异常后,                                  (实现ThrowsAdvice接口,并创建一个afterThrowing方法拦截)

环绕通知 – 环绕方法执行运行,结合以上这三个通知。                      (实现MethodInterceptor接口;必须调用“methodInvocation.proceed();)

下面的例子显示Spring AOP 通知如何工作。

简单的 Spring 例子

创建一个简单的客户服务类及一个print方法作为演示。

packagecom.yiibai.customer.services;public classCustomerService {privateString name;privateString url;public voidsetName(String name) {this.name =name;

}public voidsetUrl(String url) {this.url =url;

}public voidprintName() {

System.out.println("Customer name : " + this.name);

}public voidprintURL() {

System.out.println("Customer website : " + this.url);

}public voidprintThrowException() {throw newIllegalArgumentException();

}

}

File : applicationContext.xml – 一个bean配置文件

执行它

packagecom.yiibai.common;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importcom.yiibai.customer.services.CustomerService;public classApp {public static voidmain(String[] args) {

ApplicationContext appContext= newClassPathXmlApplicationContext(new String[] { "Spring-Customer.xml"});

CustomerService cust= (CustomerService) appContext.getBean("customerService");

System.out.println("*************************");

cust.printName();

System.out.println("*************************");

cust.printURL();

System.out.println("*************************");try{

cust.printThrowException();

}catch(Exception e) {

}

}

}

输出

*************************Customer name : Yiibai Mook Kim*************************Customer website : http://www.yiibai.com

*************************

一个简单的Spring项目用来注入(DI)bean和输出一些字符串。

Spring AOP 通知

现在,附加 Spring AOP 建议到上述的客户服务。

1. 之前通知

它会在方法执行之前执行。创建一个实现 MethodBeforeAdvice 接口的类。

packagecom.yiibai.aop;importjava.lang.reflect.Method;importorg.springframework.aop.MethodBeforeAdvice;public class HijackBeforeMethod implementsMethodBeforeAdvice

{

@Overridepublic voidbefore(Method method, Object[] args, Object target)throwsThrowable {

System.out.println("HijackBeforeMethod : Before method hijacked!");

}

}

在 bean 配置文件(applicationContext.xml),创建一个 bean 的 HijackBeforeMethod 类,并命名为“customerServiceProxy” 作为一个新的代理对象。

‘target’ – 定义你想拦截的bean。

‘interceptorNames’ – 定义要应用这个代理/目标对象的类(通知)。

class="org.springframework.aop.framework.ProxyFactoryBean">

hijackBeforeMethodBean

再次运行它,现在得到新的 customerServiceProxy bean,而不是原来的CustomerService bean。

packagecom.yiibai.common;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importcom.yiibai.customer.services.CustomerService;public classApp {public static voidmain(String[] args) {

ApplicationContext appContext= newClassPathXmlApplicationContext(new String[] { "Spring-Customer.xml"});

CustomerService cust=(CustomerService) appContext.getBean("customerServiceProxy");

System.out.println("*************************");

cust.printName();

System.out.println("*************************");

cust.printURL();

System.out.println("*************************");try{

cust.printThrowException();

}catch(Exception e) {

}

}

}

输出结果

*************************HijackBeforeMethod : Before method hijacked!Customer name : Yiibai Mook Kim*************************HijackBeforeMethod : Before method hijacked!Customer website : http://www.yiibai.com

*************************HijackBeforeMethod : Before method hijacked!

它将运行 HijackBeforeMethod 的 before() 方法,在每个 CustomerService 的方法之前执行。

2.返回后通知

该方法返回一个结果之后它将执行。创建一个实现AfterReturningAdvice接口的类。

packagecom.yiibai.aop;importjava.lang.reflect.Method;importorg.springframework.aop.AfterReturningAdvice;public class HijackAfterMethod implementsAfterReturningAdvice

{

@Overridepublic voidafterReturning(Object returnValue, Method method,

Object[] args, Object target)throwsThrowable {

System.out.println("HijackAfterMethod : After method hijacked!");

}

}

bean配置文件

hijackAfterMethodBean

再次运行,输出

*************************Customer name : Yiibai Mook Kim

HijackAfterMethod : After method hijacked!

*************************Customer website : http://www.yiibai.com

HijackAfterMethod : After method hijacked!

*************************

它将运行 HijackAfterMethod 的 afterReturning()方法,在每次 CustomerService 方法返回结果之后。

3.抛出后通知

它将在执行方法抛出一个异常后。创建一个实现ThrowsAdvice接口的类,并创建一个afterThrowing方法拦截抛出:IllegalArgumentException异常。

packagecom.yiibai.aop;importorg.springframework.aop.ThrowsAdvice;public class HijackThrowException implementsThrowsAdvice {public void afterThrowing(IllegalArgumentException e) throws Throwable {//afterThrowing:不能修改;IllegalArgumentException:捕获什么异常就发什么异常类型(Throwable:捕获任何异常);

System.out.println("HijackThrowException : Throw exception hijacked!");

}

}

bean配置文件

class="org.springframework.aop.framework.ProxyFactoryBean">

hijackThrowExceptionBean

再次运行,输出

*************************Customer name : Yiibai Mook Kim*************************Customer website : http://www.yiibai.com

*************************HijackThrowException : Throw exception hijacked!

它将运行 HijackThrowException 的 afterThrowing()方法,如果 CustomerService 的方法抛出异常。

4.环绕通知

它结合了上面的三个通知,在方法执行过程中执行。创建一个实现了MethodInterceptor接口的类。必须调用“methodInvocation.proceed();” 继续在原来的方法执行,否则原来的方法将不会执行。

packagecom.yiibai.aop;importjava.util.Arrays;importorg.aopalliance.intercept.MethodInterceptor;importorg.aopalliance.intercept.MethodInvocation;public class HijackAroundMethod implementsMethodInterceptor { //MethodInterceptor:方法拦截器

@Overridepublic Object invoke(MethodInvocation methodInvocation) throwsThrowable {

System.out.println("Method name : "

+methodInvocation.getMethod().getName());

System.out.println("Method arguments : "

+Arrays.toString(methodInvocation.getArguments()));//same with MethodBeforeAdvice

System.out.println("HijackAroundMethod : Before method hijacked!");try{//proceed to original method call

Object result =methodInvocation.proceed();//same with AfterReturningAdvice

System.out.println("HijackAroundMethod : Before after hijacked!");returnresult;

}catch(IllegalArgumentException e) {//same with ThrowsAdvice

System.out.println("HijackAroundMethod : Throw exception hijacked!");throwe;

}

}

}

bean配置文件

class="org.springframework.aop.framework.ProxyFactoryBean">

hijackAroundMethodBean

再次运行,输出

*************************Method name : printName

Method arguments : []

HijackAroundMethod : Before method hijacked!Customer name : YiiBai Mook Kim

HijackAroundMethod : Before after hijacked!

*************************Method name : printURL

Method arguments : []

HijackAroundMethod : Before method hijacked!Customer website : http://www.yiibai.com

HijackAroundMethod : Before after hijacked!

*************************Method name : printThrowException

Method arguments : []

HijackAroundMethod : Before method hijacked!HijackAroundMethod : Throw exception hijacked!

它将运行HijackAroundMethod 的 invoke()方法,在每一个 CustomerService 方法执行后。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值