在@Configuration bean 中的 SpEL 表达式中引用 ConfigurationProperties Beans [英] Referring to ConfigurationProperties Beans in SpEL expression in @Configuration bean

查看:49
本文介绍了在@Configuration bean 中的 SpEL 表达式中引用 ConfigurationProperties Beans的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个属性类:

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 类中启用配置属性,如下所示:

and I enable configuration properties in an @Configuration class like so:

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 一起使用:

In the same class ("SomeAutoConfiguration") I want to create another bean depending if the list attribute in SomeProperties is empty or not. I thought i could use @ConditionalExpression with the following SpEl:

@Bean
@ConditionalOnExpression("!(#someProperties.stuff?.isEmpty()?:true)")   
    public Object someBean(final SomeProperties someProperties) {
    return new Object();
}    

SpEl 是正确的,但我无法获得包含我的属性的 bean.使用上面的表达式,我遇到了

The SpEl is correct, but i don't manage to get a hold of the bean containing my properties. Using the above expression i run into

EL1007E:(pos 43):无法在以下位置找到属性或字段东西"空

EL1007E:(pos 43): Property or field 'stuff' cannot be found on null

并试图通过它的名字来获取 bean

And trying to get the bean through its name like

@Bean
@ConditionalOnExpression("!(@'some.CONFIGURATION_PROPERTIES'.stuff?.isEmpty()?:true)")  
    public Object someBean(final SomeProperties someProperties) {
    return new Object();
} 

结束

NoSuchBeanDefinitionException: 没有定义名为some.CONFIGURATION_PROPERTIES"的 bean

NoSuchBeanDefinitionException: No bean named 'some.CONFIGURATION_PROPERTIES' is defined

有什么想法吗?我已经尝试在另一个类中启用 ConfigurationProperties ,但也不起作用.

Any ideas? I already tried enabling ConfigurationProperties in another class but that didn't work either.

推荐答案

我认为您面临的问题是 @Conditions@Configuration 类时被评估被解析,因此不能保证 SomeProperties bean 已被定义.即使已定义,您也可能不希望它过早初始化,因此我建议采用不同的方法.

I think the problem that you're facing is that @Conditions are evaluated when @Configuration classes are parsed, so there isn't any guarantee that the SomeProperties bean has been defined. Even if it were defined, you probably don't want it to be initialized early so I'd recommend a different approach.

您可以尝试 @ConditionalOnPropety,这是 Spring Boot 在有条件地希望启用基于属性的自动配置时在内部使用的注释.如果这不够灵活,您可以创建自己的 Condition 并直接访问 Environment 以判断属性值是否为空.如果你想支持灵活绑定,你可以使用 RelaxedPropertyResolver.这是一个例子.

You could try @ConditionalOnPropety, that's the annotation that is used internally by Spring Boot whenever it conditionally wants to enable auto-configuration based on a property. If that's not quite flexible enough you can create your own Condition and access the Environment directly to tell if the property value is empty. If you want to support flexible binding you can use RelaxedPropertyResolver. Here's an example.

这篇关于在@Configuration bean 中的 SpEL 表达式中引用 ConfigurationProperties Beans的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆