0

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!

ss1
  • 1,009
  • 15
  • 32

1 Answers1

1

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

@Document(indexName = "#{@DbCreatorIndexNameConfig.indexName()}", type = "video", 
shards = 1, replicas = 0)
ArslanAnjum
  • 1,674
  • 2
  • 17
  • 31
  • 2
    let me know if it didnt solved the issue. If it did, please mark it as answer. – ArslanAnjum Aug 29 '17 at 09:32
  • Thanks, I noticed this, but the result is another exception: `SpelEvaluationException: EL1057E: No bean resolver registered in the context to resolve access to bean 'DbCreatorIndexNameConfig'` – ss1 Aug 29 '17 at 09:59
  • 1
    I dont know if it sounds absurd or not but have you tried using @indexName only? – ArslanAnjum Aug 29 '17 at 10:24
  • yes, in all variants. indexName, @indexName, indexName(), @indexName(). Even tried a constructor: `@Bean public DbCreatorIndexNameConfig dbCreatorIndexNameConfig() ...` – ss1 Aug 29 '17 at 10:31
  • stay the same: `SpelEvaluationException: EL1057E: No bean resolver registered in the context to resolve access to bean 'indexName'` - When I use @indexName() it becomes: `SpelParseException: EL1041E: After parsing a valid expression, there is still more data in the expression: 'lparen(()'` – ss1 Aug 29 '17 at 10:39
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153124/discussion-between-arslananjum-and-rapstacke). – ArslanAnjum Aug 29 '17 at 10:40