What are benefits of using Aspectj in Spring config, if aspectj can be used alone

210 views Asked by At

I looked at many Aspectj tutorials in web and most of them are: Aspectj config with Spring beans. Per my understanding, if I am using

javaagent:./src/main/resources/aspectjweaver.jar and if I created aop.xml, aspectj weaving will work for all classes and objects (including those managed by Spring).

Why do I need to enable weaving in Spring? (like in this tutorial). What's the benefit of doing:

<!-- this switches on the load-time weaving -->
    <context:load-time-weaver/>
1

There are 1 answers

0
José Carlos On BEST ANSWER

AspectJ does not require Spring. You can use aspectJ in your applications and benefit from the AOP paradigm.

Spring makes things a bit easier providing instrumentation to perform load-time-weaving easily, detecting Sun's GlassFish, Oracle's OC4J, Spring's VM agent and any ClassLoader supported by Spring's ReflectiveLoadTimeWeaver.

For example, in case of Tomcat, Spring offers TomcatInstrumentableClassLoader which adds instrumentation to loaded classes without the need to use a VM-wide agent.

On the other hand, spring provides aspectJ integration which goes beyond the scope of your question. But basically allows you to handle non managed spring beans in many ways (dependency injection, transactions...).