Search code examples
javaspringaopaspectjspring-aop

Spring AOP use AspectJ to works or what?


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?


Solution

  • 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,