AspectJ:Java 社区里最完整最流行的 AOP 框架.
在 Spring2.0 以上版本中, 可以使用基于 AspectJ 注解或基于 XML 配置的 AOP
要在 Spring 应用中使用 AspectJ 注解, 必须在 classpath 下包含 AspectJ 类库:
aopalliance.jar、aspectj.weaver.jar 和 spring-aspects.jar
将 aop Schema 添加到 <beans> 根元素中.
要在 Spring IOC 容器中启用 AspectJ 注解支持, 只要在 Bean 配置文件中定义一个空的 XML 元素 <aop:aspectj-autoproxy>
当 Spring IOC 容器侦测到 Bean 配置文件中的 <aop:aspectj-autoproxy> 元素时, 会自动为与 AspectJ 切面匹配的 Bean 创建代理.
要在 Spring 中声明 AspectJ 切面, 只需要在 IOC 容器中将切面声明为 Bean 实例. 当在 Spring IOC 容器中
初始化 AspectJ 切面之后, Spring IOC 容器就会为那些与 AspectJ 切面相匹配的 Bean 创建代理.
在 AspectJ 注解中, 切面只是一个带有 @Aspect 注解的 Java 类.
通知是标注有某种注解的简单的 Java 方法.
AspectJ 支持 5 种类型的通知注解:
@Before: 前置通知, 在方法执行之前执行
@After: 后置通知, 在方法执行之后执行
@AfterRunning: 返回通知, 在方法返回结果之后执行
@AfterThrowing: 异常通知, 在方法抛出异常之后
@Around: 环绕通知, 围绕着方法执行
···········································code···········································
(applicationcontext。xml)···························································
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="spring.aop.impl"></context:component-scan>
<!-- 使AspectJ 注解起作用: 自动为匹配的类生成代理对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
(LoggingAspect。java)·······························································
package spring.aop.impl;
import java.util.Arrays;
import java.util.List;
import javax.sql.rowset.Joinable;
import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
// 把这个类声明为一个切面:需要把类放入Ioc容器中,再声明为一个切面
@Aspect //声明为一个切面
@Component //放入Ioc容器
public class LoggingAspect {
//声明该方法是一个前置通知,再目标方法开始之前执行
//若需要使用于接口的所有方法,则再接口后加.* ArithmeticCalculator.*(int, int))
@Before("execution(public int spring.aop.impl.ArithmeticCalculator.add(int, int))")
public void beforeMethod(JoinPoint joinpoint) {
String methodName = joinpoint.getSignature().getName();
List<Object> args = Arrays.asList(joinpoint.getArgs());
System.out.println("the method "+ methodName +" begin..." + args);
}
//后置通知:再方法执行后(无论是否发生异常),执行的通知。
//在后置通知中还不能访问目标的执行结果
@After("execution(public int spring.aop.impl.*.*(..))")//包下的所有类,所有方法,任意参数
public void afterMethod(JoinPoint joinpoint) {
String methodName = joinpoint.getSignature().getName();
List<Object> args = Arrays.asList(joinpoint.getArgs());
System.out.println("the method "+ methodName +" end with ..." + args);
}
//返回通知:在方法 正常 结束时执行的代码
//返回通知是可以访问到方法的返回值的
@AfterReturning(value = "execution(public int spring.aop.impl.*.*(..))", returning = "result")
public void afterReturning(JoinPoint joinpoint, Object result) {
String methodName = joinpoint.getSignature().getName();
List<Object> args = Arrays.asList(joinpoint.getArgs());
System.out.println("the method "+ methodName+ " run with" +args+ "return with" + result);
}
//异常通知:在方法出现异常时会执行的通知
//可以访问到异常对象,可指定异常类型,当出现这种类型的异常时再执行代码---Exception e-- or -- NullPointException e --
@AfterThrowing(value = "execution(public int spring.aop.impl.*.*(..))", throwing = "e")
public void afterThrowing(JoinPoint joinpoint, Exception e) {
String methodName = joinpoint.getSignature().getName();
System.out.println("the method" + methodName+ "occurs exception: with" + e);
}
/*
* 环绕通知需要携带 proceedingJoinPoint 类型的参数
* 环绕通知类似于动态代理的全过程:ProceedingJoinPoint 类型的参数可以决定是否执行目标代码
* 且环绕通知必须有返回值,返回值即为目标方法的返回值
*/
@Around(value = "execution(public int spring.aop.impl.*.*(..))")
public Object aroundMethod(ProceedingJoinPoint pjd) throws Throwable {
Object result = null;
//执行目标方法
try {
//前置通知
System.out.println("the message in around method--before main method runing");
result = pjd.proceed();
//返回通知
System.out.println("the message in around method--after main method run");
}catch(Exception e){
//异常通知
System.out.println("the occurs exception in around method : " + e);
}
//后置通知。。。
return result;
}
}