快速入门
① 导入 AOP 相关坐标
1 |
<!--导入spring的context坐标,context依赖aop--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.5.RELEASE</version> </dependency> <!-- aspectj的织入 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency> |
② 创建目标接口和目标类(内部有切点)
1 |
//接口类 public interface TargetInterface { public void method(); } //目标角色 public class Target implements TargetInterface{ @Override public void method() { System.out.println("Target running......."); } } |
③ 创建切面类(内部有增强方法)
1 |
public class MyAspect { //前置增强 public void before(){ System.out.println("MyAspect before running..........."); } } |
④ 在 applicationContext.xml 中配置目标类和切面类的bean
1 |
<!--配置目标类--> <bean id="target" class="com.project.aop.Target"></bean> <!--配置切面类--> <bean id="myAspect" class="com.project.aop.MyAspect"></bean> |
⑤ 在 applicationContext.xml 中配置织入关系
导入aop命名空间
1 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> |
配置切点表达式和前置增强的织入关系
1 |
<aop:config> <!--引用myAspect的Bean为切面对象--> <aop:aspect ref="myAspect"> <!--配置Target的method方法执行时要进行myAspect的before方法前置增强--> <aop:before method="before" pointcut="execution(public void com.project.aop.Target.method())"> </aop:before> </aop:aspect> </aop:config> |
⑥ 测试代码
1 |
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class test { @Autowired private TargetInterface target; @Test public void test1(){ target.method(); } } |
运行截图
XML 配置 AOP 详解
切点表达式(excution)写法
- 语法:execution([修饰符] 返回值类型 包名.类名.方法名(参数))
- 访问修饰符可以省略:execution(
publicvoid com.project.aop.Target.method()) - 返回值类型、包名、类名、方法名可以使用星号* 代表任意
execution(void com.project.aop.Target.*(..))
execution(* com.project.aop.*.*(..))
execution(* com.project.aop..*.*(..))
-
包名与类名之间一个点 . 代表当前包下的类,两个点 .. 表示当前包及其子包下的类execution(* *..*.*(..))
-
参数列表可以使用两个点 .. 表示任意个数,任意类型的参数列表
通知(advice)类型
- 语法:<aop:通知类型 method=“切面类中方法名” pointcut=“切点表达式”/>
名称 标签 解释 前置通知 <aop:before>
指定增强的方法在切入点方法之前执行。
后置通知 <aop:after-returning>
指定增强的方法在切入点方法之后执行。
环绕通知 <aop:around>
指定增强的方法在切入点方法之前和之后都
执行一次。
异常抛出通知 <aop:throwing>
指定增强的方法在出现异常时执行。
最终通知 <aop:after>
无论增强方式执行是否有异常都会执行。
-
切点表达式的抽取: 当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替 pointcut 属性来引用抽 取后的切点表达式。
1 |
<aop:config> <!--引用myAspect的Bean为切面对象--> <aop:aspect ref="myAspect"> <!--抽取切点表达式--> <aop:pointcut id="myPointcut" expression="execution(* com.project.aop.*.*(..))"/> <aop:before method="before" pointcut-ref="myPointcut"></aop:before> </aop:aspect> </aop:config> |