最近由于工作需要,要求掌握关于 Spring 方面的东西。所以花了两个星期的时间来学习 Spring 的基本知识,主要包括 Ioc 和 Aop 两方面。
本文为笔者的 Spring 在 Aop 方面的学习笔记,主要结合了 Spring In Action 第三章 和 Spring-Reference 第五章 为学习向导。根据自己的理解和书中的实例来一步一步完成对于在 Spring 中 Aop 方面的编程。其中基础部分 Ioc 需要读者自己参考资料了解,本文将不做描述。
说明:我将尽量缩短程序长度,在程序部分将减少注释说明,重点要读者自己根据上下文和程序结果理解体会,具体 api 信息请读者自己参考 Spring-api 文档和相关资料。
一. 准备工作:
1. 开发环境:
l 适合人群:
要了解 Spring Ioc ,对 Spring- Aop 可以不了解或者仅仅熟悉 Aop 概念,未参与 Spring Aop 开发实战的初学者。同时也希望高手对于本文的不足或理解错误之处给予指点,谢谢。
l 开发环境:
JDK 1.4_2
l 开发工具:
Eclipse 3.12 (未采用任何插件,主要是为初学者熟悉和理解 xml 文档的配置)
l 所需组件:
Spring-Framework-1.2.8
下载地址:
2. 建立工程:
首先用 Eclpse 建立一个普通 java 项目,导入 jar 文件到编译环境中,如下:
a) Spring.jar 为 Spring 的核心 jar 文件,必须;
b) Commons-loggin.jar 日志文件,必须;
c) Cglib.jar 动态代理文件,不是必须(本文需要);
d) Jak-oro.jar 使用 Perl 和 Awk 正则表达式进行文本解析工具,不是必须(本文需要);
建立工程如下:
好了,下来我们开始我们的 Spring-aop 之旅;
二. Spring -Aop 入门
AOP 全名 Aspect-oriented programming 。 Spring framework 是很有前途的 AOP 技术。作为一种非侵略性的,轻型的 AOP framework ,你无需使用预编译器或其他的元标签,便可以在 Java 程序中使用它。这意味着开发团队里只需一人要对付 AOP framework ,其他人还是像往常一样编程。
关键性概念:
1) Advice 是代码的具体实现,例如一个实现日志记录的代码。
2) Pointcut 是在将 Advice 插入到程序的条件。
3) advisor 是把 pointcut 和 advice 的组合在一起装配器。
图例:
你的程序可能如上,现在要在三个流程上同时加入日志控制和权限控制,如下:
你的程序可能如上,现在要在三个流程上同时加入日志控制和权限控制,如下:

其中拿日志为例,日志控制和流程之间的穿插点处叫做连接点( Joinpoint ),而 Advice 就是我们日志处理的具体代码, Pointcut 就是定义一个规则,对三个和业务有关的连接点进行过滤和匹配(例如我们对于业务 1 不做日志处理)。 Advisor 就是将符合的规则的剩下的两个连接点和具体的日志记录代码组合在一起。
建立一个接口;
public interface KwikEMart
{
Squish buySquish(Customer customer) throws KwikEMartException;
Pepper buyPepper(Customer customer) throws KwikEMartException;
Cheese buyCheese(Customer customer) throws KwikEMartException;
}
实现这个接口,我们实现三个方法,买奶酪,买胡椒粉,买果酱;
public class ApuKwikEMart implements KwikEMart
{
private boolean cheeseIsEmpty = false ;

private boolean pepperIsEmpty = false ;

private boolean squishIsEmpty = false ;


public Cheese buyCheese(Customer customer) throws NoMoreCheeseException
{


if (cheeseIsEmpty)
{
throw new NoMoreCheeseException();
}
Cheese s = new Cheese();
System.out.println( " --我想买: " + s);
return s;

}
public Pepper buyPepper(Customer customer) throws NoMorePepperException
{


if (pepperIsEmpty)
{
throw new NoMorePepperException();
}
Pepper s = new Pepper();
System.out.println( " --我想买: " + s);
return s;

}
public Squish buySquish(Customer customer) throws NoMoreSquishException
{


if (squishIsEmpty)
{
throw new NoMoreSquishException();
}
Squish s = new Squish();
System.out.println( " --我想买: " + s);
return s;

}
public void setCheeseIsEmpty( boolean cheeseIsEmpty)
{
this .cheeseIsEmpty = cheeseIsEmpty;
}
public void setPepperIsEmpty( boolean pepperIsEmpty)
{
this .pepperIsEmpty = pepperIsEmpty;
}
public void setSquishIsEmpty( boolean squishIsEmpty)
{
this .squishIsEmpty = squishIsEmpty;
}
}
环绕通知的实现,必须实现invoke方法,通过调用invoke.proceed()手工调用对象方法:
public class OnePerCustomerInterceptor implements MethodInterceptor
{
private Set customers = new HashSet();


public Object invoke(MethodInvocation invoke) throws Throwable
{
Customer customer = (Customer)invoke.getArguments()[ 0 ];

if (customers.contains(customer))
{
throw new KwikEMartException( " One per customer. " );
}
System.out.println( " 店员: " + customer.getName() + " ,Can I help you ? " );
Object squishee = invoke.proceed(); // 手工调用对象方法;
System.out.println( " 店员:OK! " + customer.getName() + " .give you! " );
customers.add(squishee);

return squishee;
}
}
前置通知的实现;

public class WelcomeAdvice implements MethodBeforeAdvice
{


public void before(Method method, Object[] args, Object target) throws Throwable
{
Customer customer = (Customer) args[0];
System.out.println("店员::Hello " + customer.getName() + " . How are you doing?");

}
}

public class Customer
{
private String name = "悠~游!";

public String getName()
{
return name;
}
}
后置通知实现;

public class ThankYouAdvice implements AfterReturningAdvice
{


public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable
{
Customer customer = (Customer) args[0];
System.out.println("店员:Thank you " + customer.getName() + " . Come again! " );

}
}
public class KwikEmartExceptionAdvice implements ThrowsAdvice
{


public void afterThrowing(NoMoreSquishException e)
{
System.out.println( " 系统:NoMoreSquisheesException异常截获了: " + e.getMessage());
}
public void afterThrowing(NoMoreCheeseException e)
{
System.out.println( " 系统:NoMoreCheeseException异常截获了: " + e.getMessage());
}
public void afterThrowing(NoMorePepperException e)
{
System.out.println( " 系统:NoMorePepperException异常截获了: " + e.getMessage());
}
}
自定义的异常接口;

public class KwikEMartException extends Exception
{

private static final long serialVersionUID = -3962577696326432053L;

String retValue = "KwikEMartException 异常!";


public KwikEMartException(String name)
{
retValue = name;
}

public KwikEMartException()
{

}

public String getMessage()
{
return retValue;
}

}
没有更多的奶酪异常;

public class NoMoreCheeseException extends KwikEMartException
{
private static final long serialVersionUID = -3961123496322432053L;

String retValue = "NoMoreCheeseException 异常!";


public NoMoreCheeseException()
{
super();
}


public NoMoreCheeseException(String name)
{
super(name);
}


public String getMessage()
{

return retValue;
}
}
public class NoMorePepperException extends KwikEMartException
{
private static final long serialVersionUID = - 3961234696322432053L ;

String retValue = " NoMorePepperException 异常! " ;


public NoMorePepperException()
{
super ();
}
public NoMorePepperException(String name)
{
super (name);
}
public String getMessage()
{

return retValue;
}
}
没有更多的果酱异常;
public class NoMoreSquishException extends KwikEMartException
{
private static final long serialVersionUID = - 3121234696322432053L ;

String retValue = " NoMoreSquishException 异常! " ;


public NoMoreSquishException()
{
super ();
}
public NoMoreSquishException(String name)
{
super (name);
}
public String getMessage()
{

return retValue;
}
}
运行实例类;

public class RunDemo
{


public static void kwikEMart()
{

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("demo/kwikemart.xml");

//如果你想通过类来引用这个的话,就要用到CGLIB.jar了,同时在代理工厂里面设置:
//<property name="proxyTargetClass" value="true" />
KwikEMart akem = (KwikEMart) context.getBean("kwikEMart");

try
{
akem.buySquish(new Customer());
akem.buyPepper(new Customer());
akem.buyCheese(new Customer());

} catch (KwikEMartException e)
{
//异常已经被截获了,不信你看控制台!~;
}
}


public static void main(String[] args)
{
kwikEMart();
}
}
Xml 文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">


<beans>
<bean id="kwikEMartTarget" class="demo.ApuKwikEMart">
<!--
把这里注释去掉的话,程序调用的时候测试异常通知;
<property name="cheeseIsEmpty">
<value>true</value>
</property>
<property name="pepperIsEmpty">
<value>true</value>
</property>
<property name="squishIsEmpty">
<value>true</value>
</property>
-->
</bean>

<!-- 方法调用前通知 -->
<bean id="welcomeAdvice" class="demo.advice.WelcomeAdvice" />
<!-- 方法调用后通知 -->
<bean id="thankYouAdvice" class="demo.advice.ThankYouAdvice" />
<!-- 环绕调用通知 -->
<bean id="onePerCustomerInterceptor" class="demo.advice.OnePerCustomerInterceptor" />
<!-- 异常调用通知 -->
<bean id="kwikEmartExceptionAdvice" class="demo.advice.KwikEmartExceptionAdvice" />
<bean id="kwikEMart" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="demo.KwikEMart" />
<property name="interceptorNames">
<list>
<value>welcomeAdvice</value>
<value>thankYouAdvice</value>
<value>onePerCustomerInterceptor</value>
<value>kwikEmartExceptionAdvice</value>
</list>
</property>
<property name="target">
<ref bean="kwikEMartTarget" />
</property>
</bean>

</beans>

这个例子东西很多,不过每个类的代码都不大。如果你对 org.springframework.aop.framework.ProxyFactoryBean 不是很了解的话可以看我下篇尾处的介绍。 读清楚之后,我们运行RunDemo 类,查看控制台结果,如下:
店员::Hello 悠~游! . How are you doing?
店员:悠~游! ,Can I help you ?
-- 我想买:果酱!
店员:OK! 悠~游!.give you!
店员:Thank you 悠~游! . Come again!
店员::Hello 悠~游! . How are you doing?
店员:悠~游! ,Can I help you ?
-- 我想买:胡椒粉!
店员:OK! 悠~游!.give you!
店员:Thank you 悠~游! . Come again!
店员::Hello 悠~游! . How are you doing?
店员:悠~游! ,Can I help you ?
-- 我想买:奶酪!
店员:OK! 悠~游!.give you!
店员:Thank you 悠~游! . Come again!
我们将 kwikEMartTarget 里面的注释去掉,测试异常实现,如下:
店员::Hello 悠~游! . How are you doing?
店员:悠~游! ,Can I help you ?
系统:NoMoreSquisheesException异常截获了: NoMoreSquishException 异常!
好好理解一下,我就不废话了,我们进行下一节。
当上面两个Pointcut定义的规则不通过的时候,程序开始校验我们的myPointcut。运行,结果如下:
店员:悠~游! ,Can I help you ?
--我想买:果酱!
店员:OK! 悠~游!.give you!
店员:悠~游! ,Can I help you ?
--我想买:胡椒粉!
店员:OK! 悠~游!.give you!
门卫:你要买的东西太贵,你的钱 85 太少!~ , 取消服务!
--我想买:奶酪!//服务员没了...
好了,是不是我们想要的结果呢?呵呵。
同时,Spring 提供动态Pointcut。关于动态的说明我就不在熬述了,我们这里只关心具体Spring带给我们的具体实现方法,具体应用请读者自己斟酌使用。
运行,结果如下:
店员:悠~游! ,Can I help you ?
--我想买:果酱!
店员:OK! 悠~游!.give you!
店员:悠~游! ,Can I help you ?
--我想买:胡椒粉!
店员:OK! 悠~游!.give you!
店员:悠~游! ,Can I help you ?
--我想买:奶酪!
店员:OK! 悠~游!.give you!
动态切入点是根据当前堆栈信息进行方法匹配的一种规则,读者可以自己修改demo.RunDemo,如java.lang.Integer,来看看结果。
--我想买:果酱!
--我想买:胡椒粉!
--我想买:奶酪!
到这里能够读下来已经很不容易了,呵呵。还是站起来走动一下吧,接下来我们将搞定其他的一些东东。