Spring Data MongoDB Repository with custom collection name

后端 未结 4 1063
别那么骄傲
别那么骄傲 2021-01-06 01:18

I am using Spring Data for MongoDB and I need to be able to configure collection at runtime.

My repository is defined as:

@Repository
public interfac         


        
4条回答
  •  一向
    一向 (楼主)
    2021-01-06 01:33

    So, at the end, here is a work around that did the trick. I guess I really don't know how to access data from Spring Properties Configurer using the SPeL expressions.

    In my @Configuration class:

    @Value("${mongo.event.collection}")
    private String
        mongoEventCollectionName;
    
    @Bean
    public String mongoEventCollectionName() {
        return
            mongoEventCollectionName;
    }
    

    On my Document:

    @Document(collection = "#{mongoEventCollectionName}")
    

    This, appears to work and properly pick up the name configured in my .properties file, however, I am still not sure why I could not just access the value with $ as I do in the @Value annotation.

提交回复
热议问题