![](/img/trans.png)
[英]Can I use @ConditionalOnExpression to check for an annotation's property value?
[英]@ConditionalOnExpression not working but @Value working
我需要在Spring(非启动)应用程序的类上启用/禁用@Aspect。
春季版本是:4.1.6.RELEASE
我有一个具有属性aspect.enabled=true|false
的.properties文件( 我们已经在应用程序的其他位置成功使用了它,例如,选择log4j2配置文件 ),我尝试使用@ConditionalOnExpression
批注来启用|禁用它。
这是我的尝试:
application.properties文件:
aspect.enabled=true
班级代码:
@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;
使用此配置,表达式始终被评估为false 。
我尝试放入一个“ true”以查看它是否可以以下简单方式工作:
@Configuration
@PropertySource(ignoreResourceNotFound = true, value = { "file:${catalina.home}/conf/application.properties" })
@ConditionalOnExpression("true")
@Aspect
@Component
public class TimingProfilerProduction {
@Value("${aspect.enabled}")
public String aspect;
当然,在这样的@ConditionalOnExpression被评估为真,我也可以证明方面的类属性正确读取aspect.enabled财产。
尝试#3
我尝试了@ConditionalOnProperty :
@Configuration
@PropertySource(ignoreResourceNotFound = true, value = { "file:${catalina.home}/conf/application.properties" })
@ConditionalOnProperty(prefix="aspect", name="enabled", havingValue = "true")
但没有,总是假的 。
尝试#4:
@ConditionalOnExpression("${aspect.enabled}")
要么
@ConditionalOnExpression("!${aspect.enabled}")
给出一个错误:
Caused by: org.springframework.expression.spel.SpelParseException: EL1041E:(pos 1): After parsing a valid expression, there is still more data in the expression: 'lcurly({)'
尝试#5(使用默认值):
@ConditionalOnExpression("${aspect.enabled:true}")
始终为true (即使使用aspect.enabled=false
),因此
@ConditionalOnExpression("${aspect.enabled:false}")
总是给假
代替:
@ConditionalOnExpression("'${aspect.enabled}' == 'true'")
尝试:
@ConditionalOnExpression("${aspect.enabled} == true")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.