0

I implement my aspect class in Java style:

@Aspect
public class SomeAspect {
  @Pointcut("...")
  public void somePointcut() {}

  @Around("somePointcut()")
  public ...
}

I am new to AspectJ. I want to know if there a convenient/centralized way to control the effectiveness of the Pointcut so I can enable/disable them based on some configuration at compile time.

tkruse
  • 10,222
  • 7
  • 53
  • 80
David S.
  • 10,578
  • 12
  • 62
  • 104

1 Answers1

1

During runtime you can just use an if() pointcut, but I know this is not what you want.

For compile time it depends on how you build your project. I am going to assume you use Maven and the AspectJ Maven Plugin.

Let us further assume that you configure the plugin to run in the process-sources phase like this:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.8</version>
    <configuration>
        <!--<showWeaveInfo>true</showWeaveInfo>-->
        <source>${java.source-target.version}</source>
        <target>${java.source-target.version}</target>
        <Xlint>ignore</Xlint>
        <complianceLevel>${java.source-target.version}</complianceLevel>
        <encoding>${project.build.sourceEncoding}</encoding>
        <!--<verbose>true</verbose>-->
        <!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn>-->
    </configuration>
    <executions>
        <execution>
            <!-- IMPORTANT -->
            <phase>process-sources</phase>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
    </dependencies>
</plugin>

Then just add a Maven profile - let us call it skip-aspectj which overwrites the phase to none via <phase/>:

<profiles>
    <profile>
        <id>skip-aspectj</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase/>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

Now if you run your Maven build

  • normally, the aspects are compiled and applied,
  • with mvn -P skip-aspectj ... they will be inactive and the whole AspectJ Maven execution just skipped.
kriegaex
  • 63,017
  • 15
  • 111
  • 202