Spring Data-SpelEvaluationException-在null上找不到属性或字段



我正在尝试使用Spring Expression Language来动态配置Spring Data ElasticSearch中的注释。我尝试的解决方案(这个和这个(在我的情况下产生了以下错误:

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 {
//...

有问题的豆子:

@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知道需要的豆子。它们出现在CCD_ 2。

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

任何想法都将不胜感激!谢谢

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

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

最新更新