Zen*_*eth 15 spring-boot netflix-eureka spring-cloud-netflix
在创建bean时,我需要检查YAML属性文件是否满足两个条件.我该怎么做,因为@ConditionalOnProperty注释只支持一个属性?
Nav*_*cky 16
使用@ConditionalOnExpression注释和SpEL表达式,如http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html所述.
例:
@Controller
@ConditionalOnExpression("${controller.enabled} and ${some.value} > 10")
public class WebController {
Run Code Online (Sandbox Code Playgroud)
Jos*_*osh 15
从一开始@ConditionalOnProperty就可以检查多个属性.name/value属性是一个数组.
@Configuration
@ConditionalOnProperty({ "property1", "property2" })
protected static class MultiplePropertiesRequiredConfiguration {
@Bean
public String foo() {
return "foo";
}
}
Run Code Online (Sandbox Code Playgroud)
对于具有AND检查的简单布尔属性,您不需要@ConditionalOnExpression.
您可能对AllNestedConditionsSpring Boot 1.3.0中引入的抽象类感兴趣.这允许您创建复合条件,其中您定义的所有条件必须在@Bean您的@Configuration类初始化之前应用.
public class ThisPropertyAndThatProperty extends AllNestedConditions {
@ConditionalOnProperty("this.property")
@Bean
public ThisPropertyBean thisProperty() {
}
@ConditionalOnProperty("that.property")
@Bean
public ThatPropertyBean thatProperty() {
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以这样注释你的@Configuration:
@Conditional({ThisPropertyAndThatProperty.class}
@Configuration
Run Code Online (Sandbox Code Playgroud)
小智 5
@ConditionalOnExpression("#{${path.to.property.one:true} and ${path.to.property.two:true}}")
Run Code Online (Sandbox Code Playgroud)
如果未找到属性,这两个 true 值都是默认值。
| 归档时间: |
|
| 查看次数: |
15833 次 |
| 最近记录: |