1

I want to conditionally create an Aspect depending on a value read from the properties file. (It is related to profiling, that's why the intended selective use).

I have tried to create a class that implements Condition and therefore the matches() method that would allow me to perform the validation necessary and set the output to the corresponding Boolean, enabling or disabling it.

@Aspect 
@Conditional(MyCondition.class)
public class MyAspect {
...
pointCuts, methods and etc...
...
}

Thing is: The Aspect is instantiated by Spring anyway apparently it does not respect the @Conditional annotation output.

Are there any caveats that I am missing here?

Versions:

Spring version: 4.1.4.RELEASE

AspectJ version: 1.7.3

(The project dependency tree is a bit 'complicated' so updating libs has to be taken with a grain of salt :) )

Alex Ciocan
  • 2,272
  • 14
  • 20
Neill Lima
  • 331
  • 3
  • 11
  • 1
    It is an `@Aspect` and not an `@Component`.. So it probably isn't scanned, and you might not even have annotation processing present. – M. Deinum Feb 14 '17 at 12:13

1 Answers1

2

You could manage to achive a conditional Aspect Behaviour if you register your aspect as a component, as described in the @EnableAspectJAutoProxy configuration.

From the docs:

Enables support for handling components marked with AspectJ's @Aspect annotation, similar to functionality found in Spring's XML element. To be used on @Configuration

Note that @Aspect beans may be component-scanned like any other. Simply mark the aspect with both @Aspect and @Component:

So your problem should be solved by just adding @Component to your bean and setting up a proper component-scan if needed.

For example:

@Aspect
@Component 
@Conditional(MyCondition.class)
public class MyAspect {
...
pointCuts, methods and etc...
...
}
Alex Ciocan
  • 2,272
  • 14
  • 20