Spring Data - SpelEvaluationException - Property or field cannot be found on null

1k Views Asked by At

I'm trying to use Spring Expression Language to configure an annotation from Spring Data ElasticSearch dynamically. The solutions I tried (this and this) produce the following error in my case:

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

The annotation in question is:

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

The bean in question:

@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;
    }
}

Notes:

  • I have made sure, that the bean is wired into the application context via new AnnotationConfigApplicationContext(EsSpringTemplate.class, DbCreatorIndexNameConfig.class);

  • I have made sure that Spring knows the needed beans. They appear in annotationConfigApplicationContext.getBeanDefinitionNames().

  • indexName must be a constant. Therefor it seems only possible to use SpEL

Any idea is highly appreciated! Thanks!

1

There are 1 best solutions below

6
On

Modify it as follows:- You have to use '@' to access a bean in spel

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