Home > Blockchain >  How to enable/disable load time weaving based on a config property
How to enable/disable load time weaving based on a config property

Time:11-25

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.

CodePudding user response:

Use classical XML configuration instead of annotations, then it is "config-driven", if what you mean by this term is a file which you do not need to compile.

According to the Spring manual, simply use

<beans>
    <context:load-time-weaver/>
</beans>

CodePudding user response:

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.xml

If 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.

<aspectj>
    <weaver>
        <exclude within="test..*" />
    </weaver>
    <aspects>
        ...
    </aspects>
</aspectj>

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 property skip.tests set to y or Y.

@Pointcut( "execution( * test..*(..) ) && if()" )
public static boolean allTestCode(){
    String instr = System.getProperty( "skip.tests", "n" );
    return instr.equalsIgnoreCase( "Y" );
}

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).

@Around( "allTestCode()" )
public Object aroundAllTestCode( ProceedingJoinPoint thisJoinPoint ){
    /* Config controlled execution */
    if( System.getProperty( "advice.enabled" ).equalsIgnoreCase( "Y" ) ) applyAdvice();
    
    return thisJoinPoint.proceed();
}
  • Related