Reputation: 1334
I wanted to ask what is the best way to configure MongoClientOptions for MongoClient bean using spring boot. For instance I want to set readPreference to secondary. For some of mongodb connection configuration I can provide settings using application.yml file and 'spring.data.mongodb' properties. Is there a way to specify it also as a property, or it has to be done with java configuration? Could you redirect me to some decent examples?
Upvotes: 2
Views: 3211
Reputation: 1998
Looking over the autoconfig classes in the boot repo is where I usually start:
If you look at MongoAutoConfiguration.java, you can see that it will attempt to autowire a bean of type MongoClientOptions. If you just create a @Bean of this class in your application, I believe it will use that configuration. As far as I can tell, only a few of the configuration properties are currently configurable in the application.yml (see the MongoProperties.java class for the full set).
I can't say this is the "best" way, but I suspect it is what the author intended up to this point. I could definitely see an enhancement being made to the configuration properties that allows more of the client options to be set via properties (and that would feel more "booty" IMO).
Upvotes: 2