0

I have the need of enable/disable a @Aspect on a class in a Spring (non boot) application.

Spring version is: 4.1.6.RELEASE

I have a .properties file (that we already use successfully in other point of the application, for example to select the log4j2 configuration file) with the property aspect.enabled=true|false and I tried using the @ConditionalOnExpression annotation to enable|disable it.

This is my try:

application.properties file:

aspect.enabled=true

Class code:

@Configuration
@PropertySource(ignoreResourceNotFound = true, value = { "file:${catalina.home}/conf/application.properties" })
@ConditionalOnExpression("'${aspect.enabled}'=='true'")
@Aspect
@Component
public class TimingProfilerProduction {

    @Value("${aspect.enabled}")
    public String aspect;

With this configuration the expression is always evaluated to false.

I tried putting a single "true" to see if it works in this simple way:

@Configuration
@PropertySource(ignoreResourceNotFound = true, value = { "file:${catalina.home}/conf/application.properties" })
@ConditionalOnExpression("true")
@Aspect
@Component
public class TimingProfilerProduction {

    @Value("${aspect.enabled}")
    public String aspect;

Of course in this way the @ConditionalOnExpression gets evaluated to true and I can also prove that the aspect class property correctly reads the aspect.enabled property.

Try #3

I tried with a @ConditionalOnProperty:

@Configuration
@PropertySource(ignoreResourceNotFound = true, value = { "file:${catalina.home}/conf/application.properties" })
@ConditionalOnProperty(prefix="aspect", name="enabled", havingValue = "true")

but nothing, always false.

Try #4:

@ConditionalOnExpression("${aspect.enabled}")

or

@ConditionalOnExpression("!${aspect.enabled}")

Gives an error:

Caused by: org.springframework.expression.spel.SpelParseException: EL1041E:(pos 1): After parsing a valid expression, there is still more data in the expression: 'lcurly({)'

Try #5 (with default values):

@ConditionalOnExpression("${aspect.enabled:true}")

always gives true (even with aspect.enabled=false), and accordingly

@ConditionalOnExpression("${aspect.enabled:false}")

always gives false

5
  • 1
    Have you tried @ConditionalOnProperty? Commented Feb 5, 2019 at 21:05
  • Yes, @GrzegorzOledzki, I tried it but nothing (Try #3) Commented Feb 6, 2019 at 11:34
  • 1
    @ConditionalOnProperty(value = "aspect.enabled", matchIfMissing = false, havingValue = "true") have you tried like this? Commented Feb 6, 2019 at 11:38
  • @Ermintar is right, Please check the example here Commented Feb 6, 2019 at 11:39
  • No i didn't with that exact wording, so I will and update the question accordingly. Thank you so much Commented Feb 6, 2019 at 11:41

1 Answer 1

2

Instead of:

@ConditionalOnExpression("'${aspect.enabled}' == 'true'")

Try:

@ConditionalOnExpression("${aspect.enabled} == true")
Sign up to request clarification or add additional context in comments.

1 Comment

Gives error when the property is not present

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.