AndreaNobili
AndreaNobili

Reputation: 42957

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?

Upvotes: 8

Views: 1502

Answers (2)

Anders R. Bystrup
Anders R. Bystrup

Reputation: 16050

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,

Upvotes: 6

Gervasio Amy
Gervasio Amy

Reputation: 295

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

Upvotes: 3

Related Questions