0

I have a model class which corresponds to a mongo document:

@Document(collection="GRADE_KN")
public class Grade {

    @Id
    private String id;

    private String name;
...

}

As you can see, I have hard coded document name as "GRADE_KN". I wanted to move this to property file.

For example:

In application.properties:

mongo.collection.name=GRADE_KN

Generally, we use @Value annotation to access property. But it doesn't seem to work in this case.

But how to access property with @Document annotation.

Please clarify.

Omkar Shetkar
  • 3,488
  • 5
  • 35
  • 50

1 Answers1

1

I faced the same issue, and apparently it cannot be done. The @Document and other annotations are almost done at the same as the properties are resolved.

So, what we can do is, Have a super class, with all the common attributes that required for other models.

And then for all the subclasses models, assign the @Document and specific collection name as required.

Bottom line, the collection name cannot be assigned dynamically.

Hope this helps. Cheers !!!

  • Thanks. That helped. Got it working by using SpEL. Reason for having dynamic collection name is to have same module logic in a different context. – Omkar Shetkar Nov 04 '18 at 08:34