How can I run Spring with MongoDB disabled, but still installed?

后端 未结 2 876
时光取名叫无心
时光取名叫无心 2021-01-14 10:24

I was reading through this Git issue: https://github.com/spring-projects/spring-boot/issues/7589 with regards to Java Spring boot and am trying to figure out a way to bypass

2条回答
  •  醉酒成梦
    2021-01-14 10:35

    If your application behaves in such a way that MongoDB is optional, you have several options.

    If you are migrating an existing application, the easiest from a start would be to exclude the auto-configuration and create the infrastructure yourself. Not in the way you've indicated as returning null from a @Bean method is quite nasty. Rather you could have some service that could lazily create the client and you could update your optional usages of MongoDB to go through that service. The service would be created regardless but would only create the underlying infrastructure if necessary.

    The other option is to use a profile. If the main use case is that MongoDB is available then create a application-nomongo.properties (something like that) where you would exclude the auto-configuration using the spring.autoconfigure.exclude property. When the application starts without mongo, you can enable the nomongo profile and the auto-configuration will backoff. When it's not enabled, the Mongo bean will be created by Spring Boot.

提交回复
热议问题