Spring AOP与代理类的执行顺序是什么

本篇内容介绍了“Spring AOP与代理类的执行顺序是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

关于 Spring AOP和Aspectj的关系,两个都实现了切面编程,Spring AOP更多地是为了Spring框架本身服务的,而Aspectj具有更强大、更完善的切面功能,我们在写业务时一般使用AspectJ。不过他们的概念、原理都差不多。

Spring AOP说:

the Spring Framework’s AOP functionality is normally used in conjunction with the Spring IoC container. Aspects are configured using normal bean definition syntax (although this allows powerful “autoproxying” capabilities): this is a crucial difference from other AOP implementations. There are some things you cannot do easily or efficiently with Spring AOP, such as advise very fine-grained objects (such as domain objects typically): AspectJ is the best choice in such cases. However, our experience is that Spring AOP provides an excellent solution to most problems in enterprise Java applications that are amenable to AOP

Spring AOP currently supports only method execution join points (advising the execution of methods on Spring beans). Field interception is not implemented, although support for field interception could be added without breaking the core Spring AOP APIs. If you need to advise field access and update join points, consider a language such as AspectJ.

Spring AOP will never strive to compete with AspectJ to provide a comprehensive AOP solution

可参考 Spring AOP vs AspectJ

AspectJ项目中提供了@AspecJ注解,Spring interprets the same annotations @Aspect as AspectJ 5

Spring AOP提供了定义切面的两种方式

  • schema-based approach : XML-based format 通常不用了

  • @AspectJ annotation style:注解形式,起这个名字是因为借鉴了AspectJ项目,其实用的Spring AOP的注解是@Aspect没有J

注解案例:

@Aspect
@Component 
public class NotVeryUsefulAspect {
	@Pointcut("execution(* transfer(..))")// the pointcut expression
	private void anyOldTransfer() {}// the pointcut signature
	@Before("execution(* com.xyz.myapp.dao.*.*(..))")
    public void doAccessCheck() {
        // ...
    }
 	@AfterReturning(
        pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",
        returning="retVal")
    public void doAccessCheck(Object retVal) {
        // ...
    }
	@AfterThrowing(
        pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",
        throwing="ex")
    public void doRecoveryActions(DataAccessException ex) {
        // ...
    }
	@After("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")
    public void doReleaseLock() {
        // ...
    }
	@Around("com.xyz.myapp.SystemArchitecture.businessService()")
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
        // start stopwatch
        Object retVal = pjp.proceed();
        // stop stopwatch
        return retVal;
    }
}

AOP概念

  • Aspect:cut cross multiple classes. Spring AOP通过 schema-based approach 或 @AspectJ annotation style 两种方式定义切面。

  • Join Point:程序执行点,通常代表一个方法的执行。

  • Advice:Aspect中针对不同Join Point执行的操作,包括 around before after 三种advice。很多AOP框架将advice建模成interceptor,在Join Point外层维护一个 chain of interceptors。

  • Pointcut:匹配 Join Points的规则,如@PointCut(“pointcut expression”),满足point expression的将进行切面。

  • Target object:被切面的对象。

  • AOP proxy:代理,Spring Framework通常使用JDK 动态代理或CGLIB动态代理。

  • Weaving:linking aspects with other application types or objects to create an advised object. 发生在编译、加载或运行时。

Type of advice

  • before : @Before

  • after returning : @AfterReturning

  • after throwing : @AfterThrowing

  • after (finally) : @After

  • around : @Around

代理顺序

当一个target object有多个Spring AOP代理时,代理类执行的顺序有时很重要,比如分布式锁代理和事务代理的顺序,分布式锁必须包裹住事务。

先下结论:

Spring通过接口或注解来判断代理的顺序,顺序级别越低,代理越靠内层。顺序级别获取步骤如下:

  • 是否实现org.springframework.core.Ordered接口

  • 是否注解了org.springframework.core.annotation.Order注解

  • 是否注解了javax.annotation.Priority注解

  • 如果都没有,默认最低优先级LOWEST_PRECEDENCE = Integer.MAX_VALUE,如@Transanctional注解就是最低级别

优先级定义范围在Integer.MIN_VALUEInteger.MAX_VALUE(参见Orderd接口)。越小,优先级越高,越在外层先执行。

注解方式生成代理类是通过BeanPostProcessor实现的,由AspectJAwareAdvisorAutoProxyCreator完成,该类实现了BeanPostProcessor

注意到该类有一个Comparator类字段。最终优先级由AnnotationAwareOrderComparator来判断:

public class AnnotationAwareOrderComparator extends OrderComparator {
	/**
	 * Shared default instance of {@code AnnotationAwareOrderComparator}.
	 */
	public static final AnnotationAwareOrderComparator INSTANCE = new AnnotationAwareOrderComparator();
		@Override
	@Nullable
	protected Integer findOrder(Object obj) {
		// 父类,判断是否实现了Orderd接口
		// (obj instanceof Ordered ? ((Ordered) obj).getOrder() : null)
		Integer order = super.findOrder(obj);
		if (order != null) {
			return order;
		}
		// 没有实现则继续搜寻@Order和@Priority注解
		// 如果返回null,表示找不到,会返回父类执行getOrder方法返回Ordered.LOWEST_PRECEDENCE
		return findOrderFromAnnotation(obj);
	}
	@Nullable
	private Integer findOrderFromAnnotation(Object obj) {
		...
		// 找@Order和@Priority注解
		Integer order = OrderUtils.getOrderFromAnnotations(element, annotations);
		// 如果该类没有注解,递归找被代理类
		if (order == null && obj instanceof DecoratingProxy) {
			return findOrderFromAnnotation(((DecoratingProxy) obj).getDecoratedClass());
		}
		// 被代理类有则返回优先级,没有则返回null
		return order;
	}
}

“Spring AOP与代理类的执行顺序是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注云搜网网站,小编将为大家输出更多高质量的实用文章!


【AD】美国洛杉矶/香港/日本VPS推荐,回程电信CN2 GIA线路,延迟低、稳定性高、免费备份_搬瓦工

【AD】炭云:36元/年/1GB内存/20GB SSD空间/500GB流量/5Gbps端口/KVM/香港/国际线路LUMEN