Skip to main content

Aspect in Spring AOP

Please refer to spring-framework under org.springframework.mylearntest package for code related to this article (from official source spring-test module).

Aspect is the conceptual entity of AOP that encapsulates cross-cutting concern logic in the system. Usually, Aspect can contain multiple Pointcuts and related Advice definitions.

Advisor represents Aspect in Spring, but unlike normal Aspect, Advisor usually only holds one Pointcut and one Advice. Theoretically, Aspect definition can have multiple Pointcuts and multiple Advices, so Advisor is a special kind of Aspect.

PointcutAdvisor


Actually, org.springframework.aop.PointcutAdvisor is the real definition of Advisor having one Pointcut and one Advice, most Advisor implementations are under PointcutAdvisor.

DefaultPointcutAdvisor

<beans>
<bean id="pointcut"
class="org.springframework.mylearntest.aop.pointcut.selfdefinepointcut.QueryMethodPointcut"/>
<bean id="advice" class="org.springframework.mylearntest.aop.advice.perclass.DiscountMethodInterceptor"/>

<bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="pointcut" ref="pointcut"/>
<property name="advice" ref="advice"/>
</bean>
</beans>

NameMatchMethodPointcutAdvisor

This class internally holds a NameMatchMethodPointcut type Pointcut instance. When setting method names to be intercepted via setMappedName and setMappedNames methods exposed by NameMatchMethodPointcutAdvisor, it is actually operating on the NameMatchMethodPointcut instance inside NameMatchMethodPointcutAdvisor.

Advice advice = new DiscountMethodInterceptor();
NameMatchMethodPointcutAdvisor advisor = new NameMatchMethodPointcutAdvisor(advice);
advisor.setMappedName("invoke");

RegexpMethodPointcutAdvisor

Can only set corresponding Pointcut for it via regular expression, internally holds an instance of AbstractRegexpMethodPointcut. AbstractRegexpMethodPointcut has two implementation classes, Perl5RegexpMethodPointcutAdvisor and JdkRegexpMethodPointcut. Defaults to using JdkRegexpMethodPointcut. If forced to use Perl5RegexpMethodPointcutAdvisor, then can be achieved via RegexpMethodPointcutAdvisor's setPerl5(boolean).

DefaultBeanFactoryPointcutAdvisor

DefaultBeanFactoryPointcutAdvisor itself is bound to BeanFactory. To use DefaultBeanFactoryPointcutAdvisor, need to bind to Spring IoC container. Associate corresponding Advice via beanName of Advice registered in container. Only after corresponding Pointcut matches successfully, instantiate corresponding Advice, reducing coupling between Advisor and Advice during container startup phase.

IntroductionAdvisor

IntroductionAdvisor can only be applied to class-level interception, can only use Introduction type Advice, and cannot use arbitrary types of Pointcut like PointcutAdvisor, as well as almost any type of Advice.

IntroductionAdvisor Class Structure

Order

Most of the time, there will be multiple cross-cutting concern points, so there will be multiple Advisors in system implementation. When Pointcuts of some Advisors match the same Joinpoint, cross-cutting logic of multiple Advices will be executed at this same Joinpoint. Once there is priority dependency among these Advice logics that need to be executed at same Joinpoint, we need to intervene.

When Spring handles multiple Advisors at same Joinpoint, it will execute them according to specified order priority. Smaller order number means higher priority, higher priority ones are executed first. (By default, Spring will apply them according to their declaration order, the first declared one has smallest order number but highest priority, followed by others)

Each Advisor implementation class actually has implemented Order interface. When using, we can directly specify it.

<beans>
<bean id="permissionAuthAdvisor" class="...PermissionAuthAdvisor">
<property name="order" value="1"/>
...
</bean>

<bean id="exceptionBarrierAdvisor" class="...ExceptionBarrierAdvisor">
<property name="order" value="0"/>
...
</bean>
</beans>
Agreement
The code part of this work is licensed under Apache License 2.0 . You may freely modify and redistribute the code, and use it for commercial purposes, provided that you comply with the license. However, you are required to:
  • Attribution: Retain the original author's signature and code source information in the original and derivative code.
  • Preserve License: Retain the Apache 2.0 license file in the original and derivative code.
The documentation part of this work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License . You may freely share, including copying and distributing this work in any medium or format, and freely adapt, remix, transform, and build upon the material. However, you are required to:
  • Attribution: Give appropriate credit, provide a link to the license, and indicate if changes were made.
  • NonCommercial: You may not use the material for commercial purposes. For commercial use, please contact the author.
  • ShareAlike: If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.