0

我正在尝试根据部署的“功能”启用特定的 bean,例如休息接口、消息使用者、索引器、存档器和管理门户。在某些情况下,应用程序应该具有所有、部分或一种“功能”,例如 local、dev 和 qa 应该具有所有功能,但在暂存和生产中,功能应该被隔离,以便它们可以提高性能,像内存,线程等...

为此,我根据通过命令行传入的功能设置了自定义配置。我正在使用 ConfigurationProperties 来确定每个“功能”是否应该可用。我有一个自定义配置:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "com.example.config.functionality")
public class FunctionalityConfig {

    public static final Logger LOGGER = LoggerFactory.getLogger(FunctionalityConfig.class);

    private boolean restInterface;

    private boolean messageConsumer;

    private boolean adminInterface;

    private boolean indexing;

    private boolean archive;

    public void setRestInterface(final boolean restInterface) {
        this.restInterface = restInterface;
    }

    public boolean isRestInterface() {
        return restInterface;
    }

    public void setMessageConsumer(final boolean messageConsumer) {
        this.messageConsumer = messageConsumer;
    }

    public boolean isMessageConsumer() {
        return messageConsumer;
    }

    ...
}

然后我有一个自定义注释:

...

/**
 *
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@ConditionalOnExpression("#{ functionalityConfig.isRestInterface }")
public @interface ConditionalOnRestInterface {
}

但是当我将它添加到这样的 bean 定义中时:

@Component
@ConditionalOnRestInterface
public class RestInterface implements InitializingBean {

    private static final Logger LOGGER = LoggerFactory.getLogger(RestInterface.class);

    public void afterPropertiesSet() throws Exception {
        LOGGER.info("Rest Interface is available.");
    }
}

我收到以下错误:Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'functionalityConfig' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?

如果我摆脱@ConditionalOnExpression注释,一切正常,另外:

在 Application 类中,我有以下几行:

    @Value("#{functionalityConfig.restInterface}")
    public boolean restInterface;

他们完美地工作。我试图弄清楚为什么@ConditionalOnExpression没有捡起它。我什@EnableConfigurationProperties(FunctionalityConfig.class)至在应用程序中添加了注释,但没有更改异常。

4

0 回答 0