Spring Data MongoDB Repository with custom collection name

后端 未结 4 1059
别那么骄傲
别那么骄傲 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:34

    You can solve this problem by just using SPeL:

    @Document(collection = "#{environment.getProperty('mongo.event.collection')}")
    public class EventData implements Serializable {
        ...
    }
    

    Update Spring 5.x:

    Since Spring 5.x or so you need an additional @ before environment:

    @Document(collection = "#{@environment.getProperty('mongo.event.collection')}")
    public class EventData implements Serializable {
        ...
    }
    

提交回复
热议问题