【发布时间】:2015-10-08 15:34:17
【问题描述】:
我有这个属性类:
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("some")
public class SomeProperties {
private List<String> stuff;
public List<String> getStuff() {
return stuff;
}
public void setStuff(List<String> stuff) {
this.stuff = stuff;
}
}
我在 @Configuration 类中启用配置属性,如下所示:
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(SomeProperties.class)
public class SomeAutoConfiguration {
}
在同一个类(“SomeAutoConfiguration”)中,我想根据 SomeProperties 中的列表属性是否为空来创建另一个 bean。我想我可以将@ConditionalExpression 与以下 SpEl 一起使用:
@Bean
@ConditionalOnExpression("!(#someProperties.stuff?.isEmpty()?:true)")
public Object someBean(final SomeProperties someProperties) {
return new Object();
}
SpEl 是正确的,但我无法控制包含我的属性的 bean。使用上面的表达式我遇到了
EL1007E:(pos 43): 找不到属性或字段“stuff” 空
并试图通过它的名字来获取 bean,比如
@Bean
@ConditionalOnExpression("!(@'some.CONFIGURATION_PROPERTIES'.stuff?.isEmpty()?:true)")
public Object someBean(final SomeProperties someProperties) {
return new Object();
}
结束
NoSuchBeanDefinitionException:没有定义名为“some.CONFIGURATION_PROPERTIES”的bean
有什么想法吗?我已经尝试在另一个类中启用 ConfigurationProperties,但这也没有用。
【问题讨论】:
标签: java spring spring-boot spring-el