Q1. What are the main differences between Spring AOP and AspectJ?
Spring AOP is proxy-based, supports only method execution joinpoints, simpler configuration, and works with Spring beans. AspectJ is a full-fledged AOP framework that supports all joinpoints (constructor, field, etc.), can weave at compile/load time, and offers more power but requires additional setup.
Q2. When would you choose Spring AOP over AspectJ?
Choose Spring AOP when you need basic AOP features like method interception for transactions, security, or logging, and you are already using Spring. It's simpler and sufficient for most enterprise use cases. AspectJ is needed for more complex scenarios like field interception or performance-critical weaving.
Q3. Can Spring AOP and AspectJ be used together?
Yes, Spring can integrate with AspectJ using load-time weaving or compile-time weaving. You can use AspectJ aspects alongside Spring AOP. Spring provides @EnableAspectJAutoProxy for Spring AOP, and also supports @EnableLoadTimeWeaving for AspectJ LTW.
Q4. What are the limitations of Spring AOP?
Spring AOP only works on Spring beans, only method execution joinpoints, and cannot advise final methods or classes (CGLIB can proxy final classes but not final methods). Also, self-invocation (calling another method within the same class) bypasses the proxy, so advice doesn't apply.
Q5. How does AspectJ achieve weaving?
AspectJ weaves aspects directly into the bytecode. Compile-time weaving modifies source or compiled classes. Load-time weaving uses a Java agent to modify classes as they are loaded. This results in better performance and no proxy limitations.
