I've successfully enabled AspectJ's LoadTimeWeaving in my Spring project by annotating the configuration class with @EnableLoadTimeWeaving and adding an aop.xml.
However, I want to make it config-driven such that by default it is off and is only enabled when some configuration property is set. To that end, I see @EnableLoadTimeWeaving takes an attribute aspectjWeaving which can be either ENABLED, DISABLED, or AUTODETECT (also the default).
Can we somehow set the value of the aspectjWeaving attribute by reading some config from a properties file? Or, is there any other way to make it conditionally enable/disable?
It's not a Spring Boot application.
Disclaimer: This answer is independent of Spring. It is just about ApsectJ. But it is possible to do this in a Spring or SpringBoot project as well.
If it is weaving itself that you want to control and not what happens inside the advice, then there are the following possible ways:
1. Don't pass the Java agent parameter
Though this may sound trivial as an answer, it is safe and powerful. This will turn off all load time weaving, which is what we want sometimes.
(Just for reference, when we want to weave, we pass
-javaagent:<path-to-aspectjweaver-1.9.6.jar>as JVM parameter.)2. Add excludes in
aop.xmlIf it is specific sets of classes that you want to exclude from weaving, then exclude them in the
<weaver>tag. You may refer to the exact syntax here: Eclipse AspectJ docs.3. Control at the level of pointcuts
You may also add
if()to your pointcuts and skip the weaving using some condition, say a Java system property. Here is an example that skips all test code if there is a system propertyskip.testsset toyorY.4. Control inside the advice
(This is not what you are asking for, but I am adding here just for completion; it may help someone else who has similar needs.)
In this method, allow all weaving to happen, but execute advised code only conditionally (say, based on a configuration).