6

My question relates to Spring's AspectJ mode and especially how to enable it for:

  1. Transaction management
  2. Caching

1) I noticed that in order to enable the AspectJ mode for transaction management, I only had to do the following:

@Configuration
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)

2) Whereas in order to use AspectJ mode for caching it seems one has to:

-Put the following jar into Tomcat's lib directory: org.springframework:spring-instrument-tomcat -Add the following line in Tomcat's server.xml:

<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>

-Add the following configuration:

@Configuration
@EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED)
public class LoadTimeWeavingConfiguration implements LoadTimeWeavingConfigurer {
    @Override
    public LoadTimeWeaver getLoadTimeWeaver() {
        return new ReflectiveLoadTimeWeaver();
    }
}

-to finally be able to use the AspectJ mode as follows:

@Configuration
@EnableCaching(mode = AdviceMode.ASPECTJ)

Is the above right? If so why is AspectJ-mode caching different from AspectJ-mode transaction support?

balteo
  • 23,602
  • 63
  • 219
  • 412

1 Answers1

3

The extra configuration you listed for the @EnableCaching case is not any more needed than in the case of @EnableTransactionManagement. If you choose mode = AdviceMode.ASPECTJ it just means that it will use AspectJ instead of CGLIB proxies for the transaction management / cache functionality. If you have compile-time weaving enabled with spring-aspects-<version>.jar listed as an aspect library, it should work out of the box (given all other required transaction management / cache configuration related beans are available in the application context). If you're not using compile-time weaving but choose instead to go with load-time weaving, having -javaagent:/path/to/aspectjweaver-<version>.jar on the command line as a JVM argument is enough. The ReflectiveLoadTimeWeaver and TomcatInstrumentableClassLoader are only required in case no compile-time weaving is used in your build and a load-time weaving agent is not present in the VM, and you still want to have load-time weaving via classloading.

Nándor Előd Fekete
  • 6,988
  • 1
  • 22
  • 47
  • Could you please tell us where did you find this document? @Nándor Előd Fekete – binglong li May 19 '22 at 11:35
  • @binglongli which document? – Nándor Előd Fekete May 19 '22 at 12:00
  • The comment you said. I mean I can not find what you said at any document on internet. – binglong li May 20 '22 at 02:57
  • @binglongli This is not a quotation from a document, rather my own acquired knowledge from many years using AspectJ in different contexts, usually with Spring. You can probably find all the above information in smaller pieces scattered around the official Spring documentation. Also note that this answer was written in 2016, so it's valid for the Spring versions available around that time. Things might have changed or remained the same since then. – Nándor Előd Fekete May 20 '22 at 11:45