一、AspectJ 框架简介
AspectJ 是一个基于 Java 语言的 AOP 框架。在 Spring 2.0 以后,新增了对 AspectJ 框架的支持。在 Spring 框架中建议使用 AspectJ 框架开发 AOP。
1、 AspectJ 框架中的通知类型
2、 Spring 整合 AspectJ 框架所依赖的 Jar 包
2.1、 AspectJ 框架 jar 包
aspectjweaver-1.9.5.jar
2.2、 Spring 框架 jar 包
spring-beans-5.2.7.RELEASE.jar
spring-context-5.2.7.RELEASE.jar
spring-core-5.2.7.RELEASE.jar
spring-expression-5.2.7.RELEASE.jar
spring-aop-5.2.7.RELEASE.jar
spring-aspects-5.2.7.RELEASE.jar
commons-logging-1.2.jar
3、 AspectJ 框架配置 AOP 方式
通过 XML 文件配置 AOP
- 通过 AspectJ 配置方式
- 通过 Spring 的 Schema_based 方式
通过注解配置 AOP
二、AspectJ 框架的使用
在 AspectJ 框架中使用 xml 文件中配置 AOP 的方式:
- AspectJ 配置方式
- Schema-based 配置方式
1、 AspectJ 配置方式
AspectJ 配置方式是指使用 AspectJ 框架的配置方式来配置切面。在使用 AspectJ 配置切面时,切面不需要实现一些特定的接口。
1.1、 创建切面
public class MyAspect {
//前置通知 对目标对象的封装
public void myBefore(JoinPoint joinPoint) {
//joinPoint.getTarget(); 获取目标对象
//joinPoint.getSignature().getName(); 获取目标方法名
//joinPoint.getArgs(); 获取目标方法参数列表
//joinPoint.getThis(); 获取代理对象
System.out.println("Before "+joinPoint.getSignature().getName());
}
//后置通知
public void myAfterReturning(JoinPoint joinPoint){
System.out.println("After " +joinPoint.getSignature().getName());
}
//环绕通知
public Object myAround(ProceedingJoinPoint proceedingJoinPoint)throws Throwable{
System.out.println("Around Before "+proceedingJoinPoint.getSignature().getName());
Object obj = proceedingJoinPoint.proceed();
System.out.println("Around After "+proceedingJoinPoint.getSignature().getName());
return obj;
}
//异常通知类型
public void myAfterThrowing(Exception e){
System.out.println("Exception " +e);
}
//最终通知
public void myAfter(){
System.out.println("最终通知");
}
}
1.2、 Execution 表达式
Execution 是 AspectJ 框架中的一种表达式,用于配置切点。
基本语法格式为:
execution(<修饰符模式>?<返回类型模式><方法名模式>(<参数模式>)<异常模式>?)
其中<修饰符模式>与<异常模式>为可选。
execution(public * com.bjsxt.service….(…))
说明:
-
示例 1:
execution(public * com.bjsxt.service.impl.Student.test())
切点是 com.bjsxt.service.impl 包中 Student 类中所有 public 修饰的无参数的 test方法。 -
示例 2:
execution(* com.bjsxt.service.impl.Student.test(String,int))
切点是 com.bjsxt.service.impl 包中 Student 类中参数类型是 String,int 类型的 test方法。 -
示例 3:
execution(* com.bjsxt.service.impl.Student.test(…))
切点是 com.bjsxt.service.impl 包中 Student 类中任意参数的 test 方法。 -
示例 4
execution(* com.bjsxt.service..(…))
切点是 com.bjsxt.service 包中所有接口中任意参数的所有方法。
1.3、 使用 AspectJ 方式配置切面
1、创建目标对象
public interface UsersService {
void addUsers(String username);
}
public class UsersServiceImpl implements UsersService {
@Override
public void addUsers(String username) {
System.out.println("AddUsers "+username);
}
}
2、开启 aop 命名空间
<?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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
3、配置切面
<!-- 配置目标对象-->
<bean id="usersService" class="com.bjsxt.aspectj.service.impl.UsersServiceImpl"/>
<!-- 配置切面对象-->
<bean id="myAspect" class="com.bjsxt.aspectj.aop.MyAspect"/>
<!-- 配置切面-->
<aop:config>
<aop:aspect ref="myAspect">
<!-- 配置切点-->
<aop:pointcut id="myPointcut" expression="execution(* com.bjsxt.aspectj.service.*.*(..))"/>
<!-- 前置通知-->
<aop:before method="myBefore" pointcut-ref="myPointcut"/>
<!-- 后置通知-->
<aop:after-returning method