AOP是Aspect Oriented Programing的简称--面向切面编程。在软件开发中,分布于应用中多处的功能被称为横切关注点。通常这些横切关注点从概念上是与应用的业务逻辑相分离(但是往往直接嵌入到应用的业务逻辑之中)。将这些横切关注点与业务逻辑相分离正是面向切面编程(AOP)所要解决的。
AOP术语
通知(Advice) 包含5种类型:Before(在方法被调用之前调用通知)、After(在方法完成之后调用通知,无论方法执行是否成功)、After-returing(在方法成功执行之后调用通知)、After(在方法抛出异常后调用通知)和Around(通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为)
连接点(Joinpoint)连接点是程序执行的某个特定位置。一个类或一段程序代码拥有一些具有边界性质的特定点,这些代码中的特定点就称为“连接点”。连接点由两个信息确定:其一是用方法表示的程序执行点;其二是用相对点表示的方位。例如,在Test.foo()方法执行前的连接点,执行点为Test.foo(),方位为该方法执行前的位置。Spring使用切点对执行点进行定位,而方位则在增强类型中定义。
切点(Pointcut)一个切面并不需要通知应用的所有连接点,切点有助于缩小切面所通知连接点的范围。
切面(Aspect)切面是通知和切点的结合。通知和切点共同定义了关于切面的全部内容--它是什么,在何时和何处完成其功能。
引入(Introduction)允许我们向现有的类添加新方法或属性。
织入(Weaving)将切面应用到目标对象来创建新的代理对象的过程,其中在目标对象的生命周期有三处可以织入:编译器、类加载期和运行期。
下面通过一个简单的例子具体说明:
新建一个Java Project,目录结构如下:
Audience源码:
package com;
import org.aspectj.lang.ProceedingJoinPoint;
public class Audience {
public void takeSeats(){
System.out.println("-----BeforeAdvice-----\nThe audience is taking their seats.");
}
public void wathPerformance(ProceedingJoinPoint joinPoint){
System.out.println("\n-----AroundAdvice-----");
long start=System.currentTimeMillis();
System.out.println("start:"+start);
try {
joinPoint.proceed();
long end=System.currentTimeMillis();
System.out.println("end:"+end);
System.out.println("The performance took "+(end-start)+" milliseconds.\n");
} catch (Throwable e) {
System.out.println("Boo!We want our money back!\n");
}
}
public void leaveSeats(){
System.out.println("-----AfterReturningAdvice-----\nThe audience is leaving their seats.");
}
public void end(){
System.out.println("\n-----AfterAdvice-----\nOver!");
}
public void demandRefund(){
System.out.println("\n-----AfterThrowing-----\nBoo!We want our money back!");
}
}
NaughtWaiter源码:
package com;
public class NaughtWaiter{
public void greetTo(String name) {
System.out.println("NaughtWaiter:greet to "+name+"..." );
}
}
Performer源码:
package com;
public class Performer{
public void perform() {
System.out.println("\nThe performer is performing!\n" );
}
public void accident(){
throw new RuntimeException("运行时异常!");
}
}
beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="audience" class="com.Audience" />
<bean id="performer" class="com.Performer"/>
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut expression="execution(* com.Performer.perform(..))" id="pointcut"/>
<aop:before method="takeSeats" pointcut-ref="pointcut"/>
<aop:after method="end" pointcut-ref="pointcut"/>
<aop:after-returning method="leaveSeats" pointcut-ref="pointcut"/>
<aop:around method="wathPerformance" pointcut-ref="pointcut"/>
<aop:after-throwing method="demandRefund" pointcut="execution(* com.Performer.accident(..))"/>
</aop:aspect>
</aop:config>
</beans>
Test源码:
package com;
import java.io.File;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Test {
public static void main(String[] args) {
String path=new File("").getAbsolutePath()+"/src/com/beans.xml";
BeanFactory factory=new FileSystemXmlApplicationContext(path);
Performer performer=(Performer) factory.getBean("performer");
performer.perform();
performer.accident();
}
}
运行Test,结果为:
-----BeforeAdvice-----
The audience is taking their seats.
-----AroundAdvice-----
start:1430099696553
The performer is performing!
end:1430099696569
The performance took 16 milliseconds.
-----AfterReturningAdvice-----
The audience is leaving their seats.
-----AfterAdvice-----
Over!
-----AfterThrowing-----
Boo!We want our money back!
Exception in thread "main" java.lang.RuntimeException: 运行时异常!
....
1、通过FileSystemXmlApplicationContext(path)实现beans.xml的BeanDefinition信息的定位、载入和注册,并完成容器的初始化;
2、通过BeanFacorty获取Bean对象;
3、调用方法,并执行符合切面表达式的通知。