问题描述
我有这个属性类:
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): 在 null 上找不到属性或字段“东西”
并试图通过它的名字来获取豆子
@Bean
@ConditionalOnExpression("!(@'some.CONFIGURATION_PROPERTIES'.stuff?.isEmpty()?:true)")
public Object someBean(final SomeProperties someProperties) {
return new Object();
}
结束于
NoSuchBeanDefinitionException:未定义名为“some.CONFIGURATION_PROPERTIES”的 bean
有任何想法吗? 我已经尝试在另一个类中启用 ConfigurationProperties 但这也不起作用。
1楼
我认为您面临的问题是在解析@Configuration
类时会评估@Conditions
,因此不能保证SomeProperties
bean 已被定义。
即使它已定义,您可能也不希望它过早初始化,因此我建议采用不同的方法。
您可以尝试@ConditionalOnPropety
,这是 Spring Boot 在有条件地希望基于属性启用自动配置时在内部使用的注释。
如果这不够灵活,您可以创建自己的Condition
并直接访问Environment
以判断属性值是否为空。
如果你想支持灵活绑定,你可以使用RelaxedPropertyResolver
。
。
2楼
为了补充@PhilWebb 对 Spring Boot 2+ 的回答, RelaxedPropertyResolver
已被删除,取而代之的是名为Binder
的更强大的替代方案。
这是一个非常简单的例子:
@Configuration
@AutoConfigureBefore(JacksonAutoConfiguration.class)
@ConditionalOnMissingBean(ObjectMapper.class)
@Conditional(SpringJacksonPropertiesMissing.class)
public class ObjectMapperConfiguration {
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper()
.disable(FAIL_ON_UNKNOWN_PROPERTIES)
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
static class SpringJacksonPropertiesMissing extends SpringBootCondition {
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
return new ConditionOutcome(hasJacksonPropertiesDefined(context),
"Spring Jackson property based configuration missing");
}
private boolean hasJackonPropertiesDefined(ConditionContext context) {
return Binder.get(context.getEnvironment())
.bind(ConfigurationPropertyName.of("spring.jackson"),
Bindable.of(Map.class))
.orElse(Collections.emptyMap())
.isEmpty();
}
}
}
免责声明:此代码用于逐步淘汰有关 jackson 对象映射器的一些不良做法,以便将某些代码转换为。
3楼
根据我提出了一个抽象条件:
public abstract class ConfigurationPropertyCondition<T> extends SpringBootCondition {
private final Class<T> propertiesClass;
private final Supplier<T> constructor;
private final String prefix;
private final Predicate<T> predicate;
public ConfigurationPropertyCondition(
Class<T> propertiesClass, Supplier<T> constructor, String prefix, Predicate<T> predicate) {
this.propertiesClass = propertiesClass;
this.constructor = constructor;
this.prefix = prefix;
this.predicate = predicate;
}
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
return new ConditionOutcome(predicate.test(properties(context)), (String) null);
}
private T properties(ConditionContext context) {
return Binder.get(context.getEnvironment())
.bind(ConfigurationPropertyName.of(prefix), Bindable.of(propertiesClass))
.orElseGet(constructor);
}
}
现在我可以做这样的事情:
...
@ConfigurationProperties(prefix = MY_FEATURE_SETTINGS)
@Bean
public MyFeatureProperties myFeatureProperties() {
return new MyFeatureProperties();
}
...
@Conditional(MyFeatureConfiguration.MyFeatureEnabled.class)
@Configuration
public class MyFeatureConfiguration {
...
static class MyFeatureEnabled extends ConfigurationPropertyCondition<MyFeatureProperties> {
public MyFeatureEnabled() {
super(MyFeatureProperties.class, MyFeatureProperties::new, MY_FEATURE_SETTINGS, MyFeatureProperties::isEnabled);
}
}
}