8

I am studying Spring AOP and I have the following doubt.

From what I know there are 2 ways to implement AOP behavior into a Java application that are:

  1. AspectJ: that is the first original AOP technology that uses byte code modification for aspect weaving.

  2. Spring AOP: Java-based AOP framework with AspectJ integration that uses dynamic proxies for aspect weaving.

My doubts are: what exactly means that Spring AOP is a AOP framework with AspectJ integration? So it use in turn AspectJ? or what?

The second doubt is related to the Spring configuration of Spring AOP, I know that I can do it in these way:

1) Using Java configuration class:

@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages=“com.example”)
public class AspectConfig {
    ...
}

2) Using XML:

<beans>
    <aop:aspectj-autoproxy />
    <context:component-scan base-package=“com.example” />
</beans>

So, in both configuration it seems that Spring AOP use AspectJ because in these configuration I have: @EnableAspectJAutoProxy and

What it exactly means?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

2 Answers2

6

This might answer your question - it's an excerpt from mvn dependency:tree for a project that uses spring-aop:

[INFO] |  +- org.springframework:spring-aop:jar:3.2.3.RELEASE:compile
[INFO] |  |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- org.springframework:spring-aspects:jar:3.2.3.RELEASE:compile
[INFO] |  |  +- org.aspectj:aspectjweaver:jar:1.7.2:compile

As you can see, the Spring dependency transitively includes the AspectJ weaver.

That being said, the documentation states that

Spring 2.0 introduces a simpler and more powerful way of writing custom aspects using either a schema-based approach or the @AspectJ annotation style. Both of these styles offer fully typed advice and use of the AspectJ pointcut language, while still using Spring AOP for weaving.

Cheers,

Hearen
  • 7,420
  • 4
  • 53
  • 63
Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
  • 2
    Note that starting with spring-aop 3.3.0, the spring-aop artifact no longer depends on aopalliance:aopalliance:1.0, instead bundling (possibly modified) org.aopalliance classes directly. This will result in multiple versions of org.aopalliance classes on the classpath when combining with other libraries that use aopalliance, notably com.google.inject:guice. – ctrueden May 11 '20 at 21:07
3

At the very begining (until 1.2 version), Spring used to use AOP Alliance as the framework to provide AOP support. Then, Spring changed and started to use AspectJ for that.

The main difference between AspectJ and Spring AOP is that, firstly, Spring AOP provides a subset of features AspectJ provides and also, Spring AOP uses dynamic proxies as the strategy to implement it, while AspectJ enhances the bytecodes of a compiled class, with their specific compiler (you need to run a post-compile process to get your compiles classes ready to go).

In other words, Spring AOP uses AspectJ engine behind the scenes to provide AOP, what lets you use the power of that robust framework in a simpler way. But beware Spring AOP does not provide all AspectJ features

Take a look at this post for more info

Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
Gervasio Amy
  • 285
  • 2
  • 8