【问题标题】:Spring Data - SpelEvaluationException - Property or field cannot be found on nullSpring Data - SpelEvaluationException - 在 null 上找不到属性或字段
【发布时间】:2018-02-06 15:34:51
【问题描述】:

我正在尝试使用 Spring 表达式语言从 Spring Data ElasticSearch 动态配置注释。我尝试的解决方案(thisthis)在我的情况下会产生以下错误:

Caused by: SpelEvaluationException: EL1007E: Property or field
'DbCreatorIndexNameConfig' cannot be found on null

有问题的注释是:

@ComponentScan(basePackageClasses = DbCreatorIndexNameConfig.class)
@Document(indexName = "#{DbCreatorIndexNameConfig.indexName()}", type = "video", 
shards = 1, replicas = 0)
public class Video implements EsModel {
//...

有问题的 bean:

@Configuration("DbCreatorIndexNameConfig")
@ComponentScan(basePackageClasses = Video.class)
public class DbCreatorIndexNameConfig {

    @Value("video_default")
    public String indexName;

    @Bean
    public String indexName() {
        return indexName;
    }

    public void setIndexName(String indexName) {
        this.indexName = indexName;
    }
}

注意事项:

  • 我已确定,该 bean 已通过 new AnnotationConfigApplicationContext(EsSpringTemplate.class, DbCreatorIndexNameConfig.class); 连接到应用程序上下文中

  • 我已确保 Spring 知道所需的 bean。他们出现在 annotationConfigApplicationContext.getBeanDefinitionNames()

  • indexName 必须是一个常数。因此,似乎只能使用 SpEL

非常感谢任何想法!谢谢!

【问题讨论】:

    标签: spring spring-data spring-data-elasticsearch


    【解决方案1】:

    修改如下:-你必须使用'@'来访问spel中的bean

    @Document(indexName = "#{@DbCreatorIndexNameConfig.indexName()}", type = "video", 
    shards = 1, replicas = 0)
    

    【讨论】:

    • 如果它没有解决问题,请告诉我。如果是,请将其标记为答案。
    • 谢谢,我注意到了这一点,但结果是另一个例外:SpelEvaluationException: EL1057E: No bean resolver registered in the context to resolve access to bean 'DbCreatorIndexNameConfig'
    • 我不知道这听起来是否荒谬,但您是否尝试过仅使用@indexName?
    • 是的,在所有变体中。索引名称,@索引名称,索引名称(),@索引名称()。甚至尝试了构造函数:@Bean public DbCreatorIndexNameConfig dbCreatorIndexNameConfig() ...
    猜你喜欢
    • 2014-05-22
    • 2023-03-11
    • 2018-03-31
    • 2018-08-07
    • 2016-11-16
    • 1970-01-01
    • 2018-05-04
    • 2018-01-09
    • 2019-04-27
    相关资源
    最近更新 更多