Spring AOP基于动态代理实现,支持两种代理方式:JDK动态代理和CGLIB动态代理。如果目标对象实现了接口,Spring AOP会使用JDK动态代理;如果目标对象没有实现接口,Spring AOP会使用CGLIB动态代理。在AOP代理对象中,对目标对象进行拦截的逻辑被称为“切面”。
Spring AOP的配置方法分为两种:基于XML配置和基于注解配置。
- 基于XML配置
在XML配置文件中,定义切面、切点和通知,如下所示:
<!-- 定义切面 -->
<bean id="myAspect" class="com.example.MyAspect"></bean>
<!-- 定义切点 -->
<aop:pointcut id="myPointcut" expression="execution(* com.example.service.*.*(..))"/>
<!-- 定义通知 -->
<aop:aspect id="myAdvice" ref="myAspect">
<aop:before method="before" pointcut-ref="myPointcut"/>
<aop:after-returning method="afterReturning" pointcut-ref="myPointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="myPointcut"/>
</aop:aspect>
在这个例子中,切面由MyAspect类实现,切点由execution表达式指定,通知由before、afterReturning和afterThrowing三个方法组成。
- 基于注解配置
在基于注解配置的方式中,可以使用注解来定义切面、切点和通知,如下所示:
// 定义切面
@Aspect
@Component
public class MyAspect {
// 定义切点
@Pointcut("execution(* com.example.service.*.*(..))")
public void myPointcut() {}
// 定义前置通知
@Before("myPointcut()")
public void before() {}
// 定义后置通知
@AfterReturning("myPointcut()")
public void afterReturning() {}
// 定义异常通知
@AfterThrowing("myPointcut()")
public void afterThrowing() {}
}
在这个例子中,切面由@Aspect注解标记,切点由@Pointcut注解标记,通知由@Before、@AfterReturning和@AfterThrowing三个注解标记。
无论使用哪种方式,配置完成后,需要将切面和目标对象交给Spring容器管理,然后通过Spring容器获取代理对象来调用目标方法。例如,在XML配置文件中,可以将代理对象声明为一个bean:
<bean id="userServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="userService"/>
<property name="interceptorNames">
<list>
<value>myAdvice</value>
</list>
</property>
</bean>
在这个例子中,userService是目标对象,userServiceProxy是代理对象,通过ProxyFactoryBean将两者关联起来,使用myAdvice作为切面来拦截userService的方法。