【java实例】<转>spring : advice 前增强、后增强、环绕增强、异常增加 --其实就是动态代理

spring : advice 前增强、后增强、环绕增强、异常增加 --其实就是动态代理

标签:springstringclassbeanobjecthibernate

2010-02-09 11:291264人阅读评论(0)收藏举报

分类:

spring(3)

一个spring框架的AOP例子

2010-02-09 11:28:55

首先,你必须对增强(advice)有所了解,增强就是在特定连接点执行的动作。advice contains the logic of your aspect。增强,分为4类:

前增强(before) 在连接点之前调用

后增强(after) 在连接点执行之后调用、

环绕增强(around) 完整控制整个方法流程,必须调用MethodInvocation的proceed促使真实操作发生

异常增强针对某个异常抛出时调用

书店,一个buyBook业务:

package com.denny_blue.springdemo.aop;

public interface BuyBook {

public void buyBook(String customer,String book)throws NoThisBookException;

}

实现此接口的一个业务对象,如果顾客要买就抛出NoThisBookException异常。

package com.denny_blue.springdemo.aop;

public class MyBuyBook implements BuyBook {

public void buyBook(String customer,String book)throws NoThisBookException{

if(book.equals(""))

throw new NoThisBookException("对不起,没有"+book+"存货了!");

System.out.println(customer+",你好,你已经购买了一本"+book+"!");

}

}

自定义两个异常类,其中NoThisBookException被抛出时将触发MyThrowsAdvice调用:

//NoThisBookException.Java

package com.denny_blue.springdemo.aop;

public class NoThisBookException extends RuntimeException {

public NoThisBookException(String msg){

super(msg);

}

}

//BuyBookException

package com.denny_blue.springdemo.aop;

public class BuyBookException extends RuntimeException {

public BuyBookException(String msg){

super(msg);

System.out.println(msg);

}

}

OK,接下来就是各类增强了,前增强,在业务方法buyBook之前调用,显示欢迎信息:

package com.denny_blue.springdemo.aop;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class MyBeforeAdvice implements MethodBeforeAdvice {

public void before(Method arg0, Object[] arg1, Object target)

throws Throwable {

String customer=(String)arg1[0];      //第2个参数组就是被增强的方法传入的参数,本例中即customer,book

System.out.println("欢迎光临!"+customer+"!");   //显示欢迎信息!,在buyBook方法前调用

}

}

然后是后增强,当顾客已经买了书之后,显示欢送信息:

package com.denny_blue.springdemo.aop;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class MyAfterAdvice implements AfterReturningAdvice {

public void afterReturning(Object arg0, Method arg1, Object[] arg2,

Object arg3) throws Throwable {

String customer=(String)arg2[0];      //同前增强一样,参数组3为传入参数,具体见spring doc

System.out.println("欢迎下次再来!"+customer+"!");   //显示欢送信息!

}

}

OK,有了上面两个advice我们就能提供给顾客很好的服务态度了,等等?我们还有规则没实现,不是说一名顾客只能买一本书吗?OK,我们用环绕增强来解决,在环绕增强中保存一个HashSet,判断顾客是否来过,来过就抛出一个异常,没来过再放入此Set中:

package com.denny_blue.springdemo.aop;

import java.util.HashSet;

import java.util.Set;

import org.aopalliance.intercept.MethodInterceptor;

import org.aopalliance.intercept.MethodInvocation;

public class MyAroundAdvice implements MethodInterceptor {

private Set customers=new HashSet();    //保存购过书的顾客信息

public Object invoke(MethodInvocation invocation) throws Throwable {

String customer=(String)invocation.getArguments()[0];

if(customers.contains(customer)){

throw new BuyBookException("对不起,一名顾客只能买一本打折书!");

}

Object result=invocation.proceed();   //调用MyBuyBook中的buyBook方法,即真实操作

customers.add(customer);

return result;

}

}

好了,最后一个,异常增强,当顾客要买时,我们的书店没书了,请仓库部门赶快订货!!

package com.denny_blue.springdemo.aop;

import org.springframework.aop.ThrowsAdvice;

public class MyThrowsAdvice implements ThrowsAdvice {

public void afterThrowing(NoThisBookException e){    //可以定义多个方法,只要传入的参数是不同异常

System.out.print("通知仓库,赶紧加订书!");

}

}

好了没?还没,我们需要一个XML文件来组装这些对象,来代理业务接口,完整的beans.xml如下,各元素的含义请自己查看spring reference

?xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/dtd/spring-beans.dtd">

com.denny_blue.springdemo.aop.BuyBook

myBeforeAdvice

myAfterAdvice

myAroundAdvice

myThrowsAdvice

我们先声明所有的bean,通过

确定将要被增强的对象——目标对象(target),我们可以很容易地替换这个目标对象,只要它实现业务接口。代理的接口通过:

com.denny_blue.springdemo.aop.BuyBook

设定,然后是要用到一系列增强,注意,顺序是很有影响的!你可以尝试着改变顺序看看结果:)

myBeforeAdvice

myAfterAdvice

myAroundAdvice

myThrowsAdvice

一切准备好了,我们来测试吧,GogoGO

package test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.denny_blue.springdemo.aop.BuyBook;

public class TestAdvice {

public static void main(String args[]){

ApplicationContext ctx=new FileSystemXmlApplicationContext("/src/beans.xml");

//我的beans.xml放在项目下的src目录,eclipse环境下,请自己调整

BuyBook buybook=(BuyBook)ctx.getBean("buyBook");

buybook.buyBook("jordan","<深入浅出hibernate>");

// buybook.buyBook("dennis","");    //去掉注释即可观察异常增强

// buybook.buyBook("jordan","<深入浅出hibernate>");   //去掉注释即可观察环绕增强

}

}

一个简单的例子,脱胎于spring reference,希望对您帮助:)

你可能感兴趣的:(【java实例】<转>spring : advice 前增强、后增强、环绕增强、异常增加 --其实就是动态代理)