Skip to main content

AspectJ Weaving Methods

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

Two Ways to Weave Aspect

Suppose we have a target object Foo, there are two ways to weave Aspect definition into this target object class, to achieve interception of Joinpoints conforming to Pointcut definition.

Foo
public class Foo {
public void method1() {
System.out.println("method1 executed");
}

public void method2() {
System.out.println("method2 executed");
}
}

Programmatic Weaving

Implemented via AspectJProxyFactory
public class Test4AspectJProxyFactory {
public static void main(String[] args) {
AspectJProxyFactory weaver = new AspectJProxyFactory();
weaver.setProxyTargetClass(true);
weaver.setTarget(new Foo());
weaver.addAspect(PerformanceTraceAspect.class);
Object proxy = weaver.getProxy();
((Foo)proxy).method1();
((Foo)proxy).method2();
}
}

Weaving via Auto-Proxy

For @AspectJ style AOP, Spring AOP specifically provides an AutoProxyCreator implementation class for automatic proxying, to avoid excessive coding and configuration work. It is an extension class based on AbstractAdvisorAutoProxyCreator.

Like AutoProxyCreator, we just need to register AnnotationAwareAspectJAutoProxyCreator in IoC container configuration file.

xml configuration
<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-2.5.xsd

http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- Equivalent to line above-->
<!--<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator">
<property name="proxyTargetClass" value="true"/>
</bean>-->

<bean id="performanceAspect" class="org.springframework.mylearntest.aop2.aspectj.PerformanceTraceAspect"/>

<bean id="target" class="org.springframework.mylearntest.aop2.aspectj.Foo"/>
</beans>
TestAutoAspectJ
public class Test4AutoAspectJ {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("annotationawareaspectJautoproxycreator\\annotationawareaspectJautoproxycreator.xml");
Object proxy = context.getBean("target");
((Foo)proxy).method1();
((Foo)proxy).method2();
}
}

If target is injected as dependency object into other bean definitions, then what dependency subject object holds is also proxied target object.

Tip

When using @AspectJ style AOP, should try to use automatic proxy support in container. Usually, programmatic weaving operations are only used for testing puproses. In process of use, you will find that actually there are differences between these two ways, some behaviors are not unified.

Using @Aspect style AOP needs to introduce aspectjweaver.jar and aspectjrt.jar.

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.