java Spring Boot - @RestController 的 @ConditionalOnExpression
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26633523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot - @ConditionalOnExpression for @RestController
提问by ranweiz
I want to enable/disable a @RestController
based on configuration, in order achieve it i'm trying to use the @ConditionalOnExpression
annotation.
我想@RestController
根据配置启用/禁用一个,为了实现它,我正在尝试使用@ConditionalOnExpression
注释。
Using a static hardcoded value is working just fine:
使用静态硬编码值工作得很好:
@RestController
@ConditionalOnExpression("true")
public class MyRestController {
@RequestMapping("/hi")
public String hi() {
return "hi";
}
}
Yet, using a dynamic property value within a SpEL expression always results in notloading the @RestController
:
然而,在 SpEL 表达式中使用动态属性值总是会导致不加载@RestController
:
@RestController
@ConditionalOnExpression("${my.rest.controller.enabled:false}")
public class MyRestController { ... }
Would really appreciate any ideas/best practices to resolve this.
非常感谢任何解决此问题的想法/最佳实践。
回答by Marcello de Sales
To verify that a property in Spring Cloud Config is not provided, I used the following expression:
为了验证未提供 Spring Cloud Config 中的属性,我使用了以下表达式:
/**
* @return The Jms Template for sending the messages to the queue.
*/
@Bean(name = "jmsTemplate")
@ConditionalOnExpression("!'${jsk.messaging.jms.queue}'.isEmpty()")
public JmsTemplate jmsTemplateForQueues() {
JmsTemplate jmsTemplate = new JmsTemplate();
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(activeMQConnectionFactory());
jmsTemplate.setConnectionFactory(cachingConnectionFactory);
return jmsTemplate;
}
回答by Sploink
The spring expression language requires an expression to be evaluated. I've found that using :false to specify the ConditionalOnExpression doesn't evaluate properly for some reason, so I can suggest a couple of ways to work around this problem.
spring 表达式语言需要对表达式进行求值。我发现使用 :false 来指定 ConditionalOnExpression 由于某种原因不能正确评估,所以我可以建议几种方法来解决这个问题。
@ConditionalOnExpression("${my.rest.controller.enabled}==false")
// or
@ConditionalExpression("!${my.rest.controller.enabled}")
回答by Shrikant Salgar
Best way to use ConditionalOnPropertyannotation for property based conditions.
对基于属性的条件使用ConditionalOnProperty注释的最佳方法。
Here is example :
这是示例:
@RestController
@ConditionalOnProperty(prefix="my.rest.controller", name="enabled", havingValue = "true")
public class MyRestController { ... }
回答by Tomasz Rebizant
I think that expression like
@ConditionalOnExpression("${my.rest.controller.enabled:false}")
我认为这个表达就像
@ConditionalOnExpression("${my.rest.controller.enabled:false}")
is very missleading. The ':false' part is telling that if there is no my.rest.controller.enabled property, whole expression should be evaluated to 'false'
非常具有误导性。':false' 部分说明如果没有 my.rest.controller.enabled 属性,则整个表达式应评估为 'false'
回答by Sunil Dabburi
I agree with @Sploink. You can try using
我同意@Sploink。您可以尝试使用
@ConditionalOnExpression("#{${my.rest.controller.enabled}==true}")
@ConditionalOnExpression("#{${my.rest.controller.enabled}==true}")