主要术语
JoinPoint(连接点):一个要被拦截的方法
Pointcut(切点):在execution表达式里面声明的一个或多个方法被拦截方法的集合
advice (增强):应用在被拦截方法中的代码或者说函数,如,切面类中被@before(….)等注释了的成员函数
advisor:负责声明“建议”在什么时候被执行,比如说,在被拦截方法执行前@before(…) ,@afterReturnning(…)等
增强类型
• 前置增强
org.springframework.aop.BeforeAdvice 代表前置增强,因为spring只支持方法级的增强,所以MethodBeforeAdvice 是目前可用前置增强,表示在目标方法执行前实施增强。
• 后置增强
org.springframework.aop.AfterAdvice 代表后增强,表示目标方法在执行后实施增强
• 环绕增强
org.springframework.aop.MethodInterceptor 代表环绕增强,表示目标方法执行前后实施增强
• 异常抛出增强
org.springframework.aop.ThrowsAdvice 代表抛出异常增强,表示目标方法抛出异常后实施增强
• 引介增强
org.springframework.aop.IntroductionInterceptor 代表引介增强,表示在目标类中添加一些新的方法和属性
实战
AOP开发实战常用的有三种方法:1、基于@Aspect 注解2、基于Schema(xml)3、基于SpringAPI。下面我们先以第三种方式来对上面的概念进行应用:
先写最简单的接口与实现类:
package com.demo.spring.test;
public interface IHelloWorld {
public void execute();
}
package com.demo.spring.test;
public class HelloWorld implements IHelloWorld {
@Override
public void execute() {
System.out.println("Hello World!");
}
}
下面我们已后置增强为例:
引入jar包:aspectjweaver-1.6.9.jar、commons-logging.jar、spring.jar,请下载
package com.demo.spring.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
/**
* 后置通知类
* @author dandan
*
*/
public class MyAfterAdvice implements AfterReturningAdvice{
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.print("end!");
}
}
前置增强实现MethodBeforeAdvice,环绕增强实现MethodInterceptor
这仅仅有了增强类是不可以的,因为现在增强类和目标类之间还没有任何关系,我们需要在applicationContext.xml中进行配置,才可以对hellworld进行后置增强:
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 目标类 -->
<bean id="HelloWorld" class="com.demo.spring.test.HelloWorld"></bean>
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.demo.spring.test.IHelloWorld</value>
</property>
<property name="target" ref="HelloWorld"></property>
<property name="interceptorNames">
<list>
<value>
myAdvisor
</value>
</list>
</property>
</bean>
<!-- 所有增强类 -->
<bean id="MyAfterAdvice" class="com.demo.spring.aop.MyAfterAdvice"></bean>
<bean id="MyAroundAdvice" class="com.demo.spring.aop.MyAroundAdvice"></bean>
<bean id="MyBeforeAdvice" class="com.demo.spring.aop.MyBeforeAdvice"></bean>
<!-- 指定对哪些接口进行后置增强 -->
<bean id="myAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="MyAfterAdvice"></ref>
</property>
<property name="patterns">
<list>
<value>
com\.demo\.spring\.test\.IHelloWorld\.execute
</value>
</list>
</property>
</bean>
</beans>