0

I want to auto-configure a @Service only if Mongo was configured.

What's the appropriate way to do it?

My thought is to use:

@ConditionalOnBean(MongoTemplate.class)

Is there a better @Conditional method I should use instead?

IsaacLevon
  • 2,260
  • 4
  • 41
  • 83
  • that is not a bad idea, another way to do it would be with active profiles, but your way seems simpler. – Tom Elias Aug 30 '21 at 12:05

1 Answers1

1

Yes, you can use Conditional on property having specific value. Here is simple annotation:

@ConditionalOnProperty(value = "database.type", havingValue = "MONGO", matchIfMissing = true)

You can use the annotation for all beans related with the specified DB for example or create config that is conditional on property

xyz
  • 812
  • 9
  • 18